git add command adds new or changed files in your working directory to the Git staging area.
git add is an important command – without it, no git commit would ever do anything. git add allows you to shape history without changing how you work.When do you use git add?
git add. This step allows you to choose what you are going to commit.
What Does Git Add Do?
git add [filename] selects that file, and moves it to the staging area, marking it for inclusion in the next commit. You can select all files, a directory, specific files, or even specific parts of a file for staging and commit.
This means if you
git add a deleted file, the deletion is staged for commit. The language of “add” when you’re actually “deleting” can be confusing. If you think or use git stage in place of git add, the reality of what is happening may be more clear.git add and git commit go together hand in hand. They don’t work when they aren’t used together. And, they both work best when used thinking of their joint functionality.
How to Use git add
Common usages and options for git add
git add <path>: Stage a specific directory or filegit add -A: Stage all files (that are not listed in the.gitignore) in the entire repositorygit add -p: Interactively stage hunks of changes
Examples of git add
Basic Workflow
git add usually fits into the workflow in the following steps:
Selective Staging
But,git add could also be used to create multiple commits from your changes:
git add All Files
Staging all available files is a popular, though risky, operation. This can save time, but the risks are two-fold:
Deciding to stage all files
If the time is right to stage all files, there are several commands that you can choose from:| Command | New files | Modified files | Deleted files | Files with names beginning with a dot | Current directory | Higher directories |
|---|---|---|---|---|---|---|
git add -A | Yes | Yes | Yes | Yes | Yes | Yes |
git add . | Yes | Yes | Yes | Yes | Yes | No |
git add -u | No | Yes | Yes | Yes | Yes | Yes |
git add A Folder or Specific File
The safest and clearest way to use git add is by designating the specific file or directory to be staged:
git add directory/: Stage all changes to all files within a directory titleddirectorygit add README.md: Stage all changes within theREADME.mdfile
Undo Added Files
Before undoing agit add, you should first be sure that you won’t lose any work. There’s no way to “revert” an add in the same way you can revert a commit, but you can move the files out of the staging area.
To avoid this, first stage all changes, then unstage them together, or commit the changes and reset back before the commit happened.
Using git reset to undo git add
git reset is a flexible and powerful command. One of its many use cases is to move changes out of the staging area.
Ensure no additional changes
Make sure you don’t have any additional changes to the files in question.
Related Commands
git status: Always a good idea, this command shows you what branch you’re on, what files are in the working or staging directory, and any other important informationgit checkout [branch-name]: Switches to the specified branch and updates the working directorygit commit -m "descriptive message": Records file snapshots permanently in version historygit push: Uploads all local branch commits to the remote
Next Steps
Git Commit
Learn how to commit your staged changes
Git Status
Check what files are staged
Git Push
Push your commits to the remote
Git Pull
Update your local branch with remote changes