Git (Version Control) - Adding a file to Git using the git add command

If you are not familiar with Git, check out our getting started article. These examples are based on Linux.

Let's say the root directory of your master Git repository is /var/git. Move into the root directory.

cd /var/git


Create a new file.

touch foo.txt


Add the new file to Git.

git add foo.txt


The git status command will now show that foo.txt is ready to be committed.

git status
. . .
Changes to be committed:
. . .
  new file: foo.txt


You would then commit the new file.


Adding multiple files in a directory

Let's say the "test" directory contains two (or more) files.



Adding the test directory will add every file below the test directory.

git add test


Adding directories

Directories are not tracked in git. For example, let say you create a directory named fooBar.

mkdir fooBar


And add the fooBar directory.

git add fooBar


The git status command will not contain "fooBar". This is because git tracks files, not directories.

git status

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter 30f61 in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |