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