In our last article, we explained how to add content to a local git repository. There we used the git add command to add the README.md file to the repository we initialized with git. In this article, we will talk about the git add command in greater detail and will also demonstrate some common options used with the git add command with examples.
What does the ‘git add’ command really do?
This command updates the index using the current content found in the working tree, to prepare the content staged for the next commit. A “working tree” consist of files in the repository that you are currently working on. An “index” is the staging area where new commits are prepared. It acts as the interface between a repository and a working tree.
The index holds a snapshot of the content of the working tree, and it is this snapshot that is taken as the contents of the next commit. Thus after making any changes to the working directory, and before running the commit command, you must use the add command to add any new or modified files to the index. The git add command can be performed multiple times before a commit. It only adds the content of the specified file(s) at the time the add command is run; if you want subsequent changes included in the next commit, then you must run git add again to add the new content to the index.
Example 1: Add new/modified files to the staging area of a git repository
I’ve modified the README.md file that we created in our last article by adding a line to it.
[sahil@linuxnix my_first_repo]$ cat README.md This is a readme file for my first git repository This is a second line
Now if we run the git status command, git will tell us that a file has been modified and git also tells us to stage the file and commit the changes if we wish to save them.
[sahil@linuxnix my_first_repo]$ git status # On branch master # 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 # no changes added to commit (use "git add" and/or "git commit -a")
Git is helpful in the sense that it displays hints about what the user should do next. So let us move the updated file to the staging area with git add and commit the changes using git commit.
[sahil@linuxnix my_first_repo]$ git add README.md [sahil@linuxnix my_first_repo]$ [sahil@linuxnix my_first_repo]$ git commit -m "Added a line to README.md" [master f9849b2] Added a line to README.md 1 file changed, 1 insertion(+) [sahil@linuxnix my_first_repo]$
Notice that this time I used the git commit command with the -m option followed by a description of the commit i.e the comment. If you do not wish to open the editor to add descriptive comments to your commits then consider using the -m option with the git commit command. This is also extremely useful when you would like to use the git commit command inside a script or some other automated process. The git commit output tells us that we modified one file and the modification was the insertion of one line to the file. The plus (+) symbol indicates that a line or content was added to a file.
We’ll now explore some of the commonly used options with the ‘git add’ command with examples.
Example 2: Verbose mode
Using the verbose mode with the ‘git add’ command displays the names of the files being added to the staging area.
Given below is an example.
[sahil@linuxnix my_first_repo]$ git add -v README.md add 'README.md' [sahil@linuxnix my_first_repo]$
Example 3: Glob mode
Similar to the bash shell the git command allows the use of file glob expressions indicated by *.
I created a new file named README1.md in our repository and in doing so we now have two files with the extension .md in our repository.
[sahil@linuxnix my_first_repo]$ ls README1.md README.md
If we want to add both .md files to the staging area, we would use the following command.
[sahil@linuxnix my_first_repo]$ git add -v *md add 'README.md' add 'README1.md'
This adds both files to the staging area.
Example 4: Add all files to the staging area
If we wish to add all files in the repository to the staging area we should use the git add command with the -A option followed by the wildcard character *. Given below is an example.
[sahil@linuxnix my_first_repo]$ ls README1.md README.md test.txt [sahil@linuxnix my_first_repo]$ git add -A *
This is like telling git to add all the changes made to all the files in the repository to the staging area. Here we added all three files in the repository to the staging area to be committed. We can run the git status command to verify.
[sahil@linuxnix my_first_repo]$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: README.md # modified: README1.md # new file: test.txt #
Example 5: Add ignored files
Git allows us to ignore certain files from being committed to the repository. The ignoring of files is managed by using a .gitignore file in the repository. We will talk about ignoring files in a separate article dedicated to it. But if needed we could actually add ignored files to the staging area. For this, we use the git add command with the -f option to allow the forceful addition of otherwise ignored files to the staging area. A syntax example is shown below:
git add -f <file name>
In this article we explored the git add command in greater detail along with examples. We will be continuing our series of articles on git exploring more aspects of this extremely powerful and feature-rich distributed version control system.
Latest posts by Sahil Suri (see all)
- git: What is detached HEAD state? - June 12, 2018
- git checkout command explained - June 11, 2018
- Using HEAD and git commit hash to differentiate between committed file versions - June 8, 2018
- git log command explained with examples - June 7, 2018
- git add command explained with examples - June 6, 2018