38

Is it redundant to run git add . and then git commit -am "commit message"?

Can I just run git add . and then git commit -m "commit message" or, alternatively, just git commit -am "commit message"?

1
  • 6
    I think you should explicitly state your assumption that you're in the top level of your project; git add . and git commit -a will accomplish very different things if you're in a sub directory.
    – user229044
    Sep 2, 2010 at 20:44

5 Answers 5

51

Git add + git commit -m will just commit those files you've added (new and previously tracked), but git commit -am will commit all changes on tracked files, but it doesn't add new files.

1
  • 3
    as stated, git commit -am will not add untracked files, but I thought I'd point out that git add -u will only add (update) currently tracked items as well...
    – johnny
    Aug 16, 2011 at 18:15
31

With Git before version 2.0, it's not redundant to run git commit -am if you deleted any tracked files. See further explanation of the options below.

As long as you're in the root directory, and you want to add all new files, modifications and deletions, you want 3) below.

1) Commit new files and modifications to previously-tracked files, but don't add deletions.

With Git < 2.0:

$ git add .
$ git commit -m

With Git >= 2.0:

$ git add --ignore-removal .
$ git commit -m

2) Commit modifications and deletions to tracked files, but don't add new files.

$ git commit -am

3) Commit new files and all changed tracked files (both modifications and deletions). Either:

With Git < 2.0:

$ git add .
$ git commit -am

...Or:

$ git add -A
$ git commit -m

With Git >= 2.0:

$ git add .
$ git commit -m
3
  • Therefore, is git add . + git commit -am equivalent to git add -A? Apr 26, 2015 at 23:50
  • If you also do a commit after your second alternative, yes. git add . + git commit -am is equivalent to git add -A + git commit -m. I've updated my answer to reflect this.
    – Karl Horky
    Apr 27, 2015 at 13:09
  • git add . does add file deletions since Git 2.0.0: "git add <path>" is the same as "git add -A <path>" now" (from release notes). I updated the post accordingly. Apr 27, 2015 at 18:58
11

Actually, git commit -a is not always redundant, as it also removing files! For example you have two files: One file is tracked the other not. Now you want to delete the tracked (everywhere) and add the not tracked.

So you remove the tracked file. If you then use only git add . and then git commit it will track the new but won't delete the old file in the repository.

So you either execute git rm … or using git commit -a.

1
  • 1
    This is the actual correct answer that answers the question directly.
    – CDR
    Feb 9, 2012 at 15:59
4

I think some of the previous answers did not see the period after git add (your original question and some of the answers have since been edited to make the period more clear).

git add . will add all files in the current directory (and any subdirectories) to the index (except those being ignored by .gitignore).

The -a option to git commit will include all changes to files that are already being tracked by git, even if those changes have not been added to the index yet.

Consequently, if all of the files are already being tracked by git, then the two approaches have the same effect. On the other hand, if new files have been created, then git add .; git commit will add them to the repository, while git commit -a will not.

git add .; git commit -a is indeed redundant. All changes have already been added to the index, so the -a does nothing.

1
  • You just said what I said with more words. When someone says git add it means they add files they want with it, let it be with . or a list of files. The difference is clear anyways, -am will not add any new files.
    – Tuminoid
    Sep 3, 2010 at 5:48
2

The -a switch tells git to stage the modified files for adding - it stands for 'all' not 'add'. If you wan't the content in a new file to be added to git then you must add it using git add before you commit it.

If you have already done a git add . then it has already added all files in the current directory i.e. both new and modified files. Hence you don't need a -a switch to stage content again. So you can rightly just follow it up with a git -m "msg"

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.