Git add all: Difference between “git add -A” and “git add .”

761    Asked by AbdulRehman in Salesforce , Asked on Feb 23, 2021

For example, how could I commit only 15 lines out of 30 lines that have been changed in a file?

The command git add [--all|-A] appears to be identical to git add .. Is this correct? If not, how do they differ?

Answered by Abdul Rehman

git add -A : It will stage all changes

git add . : It will stage new files and modifications, without deletions

git add -u: It will stage modifications and deletions, without new files

Let me elaborate it a bit:

git add -A is equivalent to git add . and git add -u

git add . will look at the working tree. Then it will add all those paths to the staged changes if they are either changed or are new and not ignored. But it will not stage any 'rm' actions.

git add -u will look at all the already tracked files. Then it will stage the changes to those files if they are different or if they have been removed. It will not add any new files, it will only stage changes to already tracked files.

Here's a practical explanation to help you understand the differences better:

git init

echo Change me > change-me

echo Delete me > delete-me

git add change-me delete-me

git commit -m initial

echo OK >> change-me

rm delete-me

echo Add me > add-me

git status

#Changed but not updated:

#modified: change-me

#deleted: delete-me

#Untracked files:

#add-me

git add .

git status

#Changes to be committed:

#new file: add-me

#modified: change-me

#Changed but not updated:

#deleted: delete-me

git reset

git add -u

git status

#Changes to be committed:

#modified: change-me

#deleted: delete-me

#Untracked files:

#add-me

git reset

git add -A

git status

#Changes to be committed:

#new file: add-me

#modified: change-me

#deleted: delete-me

Note: For Git version 2.x, output for git add . and git status will be different.



Your Answer

Interviews

Parent Categories