Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between "git add -u" and "git add -A"?

Tags:

git

git-add

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?

like image 370
Sheharyar Avatar asked Feb 21 '13 20:02

Sheharyar


People also ask

What's the difference between git add and git add a?

The difference lies in which files get added. git add -A command will add all modified and untracked files in the entire repository. Whereas git add . will only add modified and untracked files in the current directory and any sub-directories. If you are at the root of the repo, they have the same effect.

What does git add u do?

" 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.

What is the difference between git add and git commit?

git add : takes a modified file in your working directory and places the modified version in a staging area. git commit takes everything from the staging area and makes a permanent snapshot of the current state of your repository that is associated with a unique identifier.

Should I always use git add?

The git add command adds new or changed files in your working directory to the Git staging area. git add is an important command - without it, no git commit would ever do anything. Sometimes, git add can have a reputation for being an unnecessary step in development.


2 Answers

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.

like image 69
Sheharyar Avatar answered Oct 15 '22 01:10

Sheharyar


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

git add -u will track modified and deleted files

like image 22
Melvin Avatar answered Oct 15 '22 00:10

Melvin