乐闻世界logo
搜索文章和话题

What is the difference between git add a and git add?

2个答案

1
2

git add -A and git add . behave similarly in most cases, but they can exhibit different behaviors in specific Git versions and contexts. Here are their main differences:

  • git add -A: This command is shorthand for git add --all, which stages all changes in the working directory—including file additions, modifications, and deletions—into the staging area. This operation affects the entire repository.
  • git add .: This command stages new files and modified files in the current directory and its subdirectories but does not stage deleted files. This operation is limited to the current directory and its subdirectories. In newer Git versions (starting from 2.0), git add . and git add -A are nearly equivalent because git add . now includes deleted files. However, git add . is confined to the current directory, whereas git add -A can be executed anywhere in the repository and affects the entire repository. In summary, if you want to include all change types (additions, modifications, deletions) and ensure staging applies to the entire repository, using git add -A is a safer choice. If you only need to stage changes in the current directory and its subdirectories, use git add .. Always verify your Git version's behavior, as it may impact command accuracy.
2024年6月29日 12:07 回复

For Git 2.0, git add -A defaults to: git add . equals git add -A ..

git add <path> is now equivalent to git add -A <path>, so that " git add dir/" will notice paths removed from the directory and record the deletions. In older versions of Git, " git add <path>" ignored deletions.

If you prefer, you can use " git add --ignore-removal <path>" to add only paths that were added or modified. <path>

git add -A is like git add :/ (add all contents in the top git repo folder).

Note that Git 2.7 (November 2015) will allow you to add a folder named " :"!

See Junio C Hamano's commit 29abb33 (October 25, 2015). gitster


Note that starting from Git 2.0 (first or second quarter of 2014), when discussing git add . (the current path in the working tree), you must also use "." in other commands like git add.

This means:

" git add -A ." is equivalent to " git add .; git add -u ."

(Note the extra "." in git add -A and git add -u)

Because git add -A or git add -u operates on the entire working tree (starting from Git 2.0), not just the current path.

These commands operate on the entire tree in Git 2.0 to be consistent with " git commit -a" and other commands. Because there is no mechanism to make " git add -u" behave like " git add -u .", it is important for those accustomed to " git add -u" (without path specification) updating only the index of paths in the current subdirectory to start using the explicit form " git add -u ." as they did before Git 2.0.

When these commands are run without path specification and local changes are made outside the current directory, warnings are issued, because in this case, the behavior in Git 2.0 differs from today's version.

2024年6月29日 12:07 回复

你的答案