Let’s initialize a new project.
$ cd project
$ git init
Initialized empty Git repository in /Users/zakh_eecs/_work/LearnGIT/git_add/project/.git/
$ echo "New Project" > ReadMe.txt
$ git add ReadMe.txt
$ git commit -m "Initial Commit"
[master (root-commit) 47b9af1] Initial Commit
1 file changed, 1 insertion(+)
create mode 100644 ReadMe.txt
In this project, we have added a ReadMe.txt file. We used the “git add” command to add the ReadMe.txt. The add command is not only for adding files. It also adds any file modification. For this tutorial, we will only add and delete files to keep it simple. But think of the add command as adding changes to the staging area. Then, you have to use the commit command to make the changes official.
When you are dealing with a lot of files and folders, it’s difficult to individually add each change. So you can use the following commands:
$ git add -A
Let’s look at how the two commands behave:
$ git add .
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>…" to unstage)
new file: a.txt
new file: b.txt
new file: c.txt
$ git commit -m "Add a.txt, b.txt, c.txt"
[master 9ca90fc] Add a.txt, b.txt, c.txt
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
create mode 100644 b.txt
create mode 100644 c.txt
$ git add -A
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>…" to unstage)
new file: x.txt
new file: y.txt
new file: z.txt
$ git commit -m "Add x.txt, y.txt, z.txt"
[master 8af8c12] Add x.txt, y.txt, z.txt
3 files changed, 0 insertions(+), 0 deletions(–)
create mode 100644 x.txt
create mode 100644 y.txt
create mode 100644 z.txt
Both options seem to work the same.
To investigate further, let’s create a situation where we add something at the root level of the working directory and then add more files in a folder:
$ mkdir new
$ cd new
$ touch m.txt n.txt o.txt
$ git add .
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>…" to unstage)
new file: m.txt
new file: n.txt
new file: o.txt
Untracked files:
(use "git add <file>…" to include in what will be committed)
../1.txt
Notice Git didn’t add the 1.txt file in the higher level folder.
If we created a folder called nested with d.txt file and use the git add. command again, we see that o.txt has been added but 1.txt is not added yet.
$ touch nested/d.txt
$ git add .
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>…" to unstage)
new file: m.txt
new file: n.txt
new file: nested/d.txt
new file: o.txt
Untracked files:
(use "git add <file>…" to include in what will be committed)
../1.txt
Now let’s use the git add -A command:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>…" to unstage)
new file: ../1.txt
new file: m.txt
new file: n.txt
new file: nested/d.txt
new file: o.txt
Now, 1.txt in the folder has been added to the staging area.
Here’s how the folders look
|–1.txt
|–ReadMe.txt
|–a.txt
|–b.txt
|–c.txt
|–x.txt
|–y.txt
|–z.txt
`– new
|–m.txt
|–n.txt
|–o.txt
`– nested
|–d.txt
So, when you are using “git add .” command, it will add all the changes from that level. But when you use “git add -A” option it will look for modifications throughout the module and add them.
Conclusion
Git add command provide powerful ways to add modified files. You can use your codes natural directory hierarchy to control what gets added.