git add . vs git commit -a

Version ControlGit CommitGit AddGit

Version Control Problem Overview


What's the difference between:

  • git add .
  • git commit -a

Should I be doing both, or is that redundant?

Version Control Solutions


Solution 1 - Version Control

git commit -a means almost[*] the same thing as git add -u && git commit.

It's not the same as git add . as this would add untracked files that aren't being ignored, git add -u only stages changes (including deletions) to already tracked files.

[*] There's a subtle difference if you're not at the root directory of your repository. git add -u stages updates to files in the current directory and below, it's equivalent to git add -u . whereas git commit -a stages and commits changes to all tracked files.

Solution 2 - Version Control

git commit -a automatically invokes git add on all files it knows about. You can use git add to select what files to commit. Consult the docs for more info: here

Solution 3 - Version Control

By using the git commit -a switch with the commit command to automatically "add" changes from all known files (i.e. all files that are already listed in the index)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYarinView Question on Stackoverflow
Solution 1 - Version ControlCB BaileyView Answer on Stackoverflow
Solution 2 - Version ControlalternativeView Answer on Stackoverflow
Solution 3 - Version ControlDilipView Answer on Stackoverflow