Back

Explore Courses Blog Tutorials Interview Questions
+7 votes
10 views
in DevOps and Agile by (19.4k points)
edited by

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?

1 Answer

+7 votes
by (27.5k points)
edited by

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.

For better understanding about these commands go through the following crash course on git that will help you to understand git well

 

Related questions

+12 votes
2 answers
+15 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...