Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding when to use git commit -m and git commit -am

Tags:

git

My main question here is whether iti makes sense to just always do git commit -am instead of git add . followed by git commit -m?

I understand that -am indicates that it will add all changes from modified TRACKED files. So in a situation where I did not add any new files, it would make sense to just run git commit -am instead of git add, but would it be considered best practice to just do:

git add .

git commit -am "message"

anyway?

or even instead:

git add -A

git commit -am "message"
like image 374
appsecguy Avatar asked Dec 16 '22 01:12

appsecguy


1 Answers

would it be considered best practice to just do:

git add .

git commit -am "message"

anyway?

No, there is no such "best practice". As long as you do not want to include any untracked files, git add + git commit -m and git commit -am will do exactly the same.

There are two situations where you need to to use git add:

  • if you want to include untracked files, and
  • if you do not want to commit everything that changed

The second point in particular is the reason many people recommend against always using commit -a: After working on code for a while, you often have several different types of changes in your working copy (the bugfix you were working on, some unrelated renaming, some temporary changes for debugging...). commit -a risks putting too much into one commit - in these cases selective use of git add is better.

But if you are certain you want to commit everything you changed, go ahead and use git commit -a.

like image 187
sleske Avatar answered Dec 26 '22 06:12

sleske