40

Okay, so I was searching "How to Remove Manually Deleted files from Git" without actually doing git rm file.txt for each file when I came across "Removing multiple files from a Git repo that have already been deleted from disk".

The two most Up-Voted commands were:

  • git add -u
  • git add -A

Although both of them worked for me, I still can't understand the difference and the solutions on the page don't explain it either. My questions are, how are they differ from each other, and what other git commands can be used to remove files that have been deleted manually from the disk?

Community
  • 1
  • 1
Sheharyar
  • 73,588
  • 21
  • 168
  • 215

2 Answers2

53

Brief Answer:

git add -A is equal to git add . + git add -u


Explanation:

When you do a "git add .", it adds all files (existing, modified and new) to the staging area but it does not remove files that have been deleted from the disk.

"git add -u" only adds currently tracked files (which have been modified) to the staging area and also checks if they have been deleted (if yes, they are removed from staging area). This means that it does not stage new files.

Doing "git add -A" performs both of these steps, that is, stages your entire directory as it is.


Summary:

  • git add -A : Stages Everything
  • git add -u : Stages only Modified Files
  • git add . : Stages everything, without Deleted Files

Read the Documentation for more.

Community
  • 1
  • 1
Sheharyar
  • 73,588
  • 21
  • 168
  • 215
  • 11
    With git version 2.0, `git add .` also records file removals. From the [doc][1]: > "git add " is the same as "git add -A " now, so that > "git add dir/" will notice paths you removed from the directory > and record the removal. [1]: https://github.com/git/git/blob/master/Documentation/RelNotes/2.0.0.txt – user650654 Dec 05 '14 at 18:48
  • @sheharyar this in not entirely correct. 'git add .' merely adds affected files in the current directory and below. '.' is simply a path like any other. 'git add -A' adds all files, even those in directories above the current one – onlinespending Feb 09 '22 at 00:25
13

git add -A will track new, modified and deleted files

git add -u will track modified and deleted files

Melvin
  • 157
  • 3
  • 12