I already know that git commit -a
is a short for git add . && git commit
Ah, but it isn't. Let's get back to this in a moment.
... [is there] a short for git add . && git commit --amend
No, but git commit -a --amend
comes somewhat close.
Both of these are better expressed, not as git add . && git commit options
, but rather as git add -u && git commit options
. (There's still a difference if you use a separate git add
, but it's not as obvious. I'll describe it lightly in a moment.)
In particular, git add .
tells Git to add all files at the current directory and below. This includes any new (but untracked) files, unless those untracked files are also listed in an exclusion file (.gitignore
or .git/info/exclude
for instance). But git commit -a
won't add new files: it will only update already-tracked files.
(Note: In Git 2.0 and later, git add -u
adds files anywhere in the working tree, not just at the current level and below; use git add -u .
to limit this to the current level and below.)
Now, as to the differences between using a separate git add
and subsequent git commit
, and using the -a
flag to git commit
: there are two that are visible to you:
First, suppose you change your mind about the commit, and abort it entirely. In this case, with git add
, any updates are still staged for commit. You can continue to edit and add files as usual but your existing files, added with a git add
command, are still added. But with git commit -a
, your files are not added-for-commit.
Second, if you have a pre-commit hook, what the pre-commit hook will use as the index is different. This gets particularly complicated if you start using the --only
or --include
flag as well. These details are not appropriate for those relatively new to Git, so we won't cover them any further here: just remember that there is a corner-case here with hooks.