Git add-Learn git commands.
How to use git add
command step by step.
After a developer finishes code changes. The next step is to publish them to the repository. To accomplish this, all modified and new files should be staged for the next commit. The git “add” command creates a staging area with the changed and new contents.
Git commit, which updates the repository by adding a new revision number.
What are the possible options to add changes?
Like other commands, add comes with multiple options. In this section, we will cover all options along with a practical example for each one.
Add individual files and directories (file-path/directory).
The command with the full file path adds the file, and the directory path adds all files in that folder. You can specify multiple files and directories as command-line options.
Add everything to the Staging Area for the next commit (–all option)
This option adds all changes inside the project for the next commit. It includes deleted files, modified files, and new files. Be careful while using this, as it may add some unwanted files, such as swap files, binaries, tag files on Linux, etc.
Add changes only to files that have been tracked (-u).
It adds all the changes to the staging area. A change includes either modified files or deleted files. The untracked files remain untouched.
How to use git add with command example?
With the git status command, you can check modified and untracked files.
The following example shows how to add files individually.
git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: README.md # # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: README.md # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # newFile.txt
The README.md has been modified and newFile.txt is an untracked file.
Add files to Staging Area.
git add README.md newFile.txt
git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: README.md # new file: newFile.txt #
The following shows how to add all files.
With the –all option you can add all changes to the working project.
git add --all