0

I already knew that git commit -a is short for git add -u && git commit.

So I'm wondering if there is a short for git add . && git commit --amend when we have some file modified after the first commit?


EDIT

Change git add . to git add -u, which are not the same things.

Fangzhen Song
  • 77
  • 1
  • 8
  • 2
    Have you tried `git commit -a --amend` ? – Tim Biegeleisen Jul 04 '21 at 07:59
  • 2
    See also [Git add and commit in one command](https://stackoverflow.com/q/4298960/1256452) and [whats a git command to git add -A and git commit --amend in one line?](https://stackoverflow.com/q/44295238/1256452). – torek Jul 04 '21 at 08:09

2 Answers2

5

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.

torek
  • 448,244
  • 59
  • 642
  • 775
3

git commit -a will automatically git add changes to all tracked files. You can combine this with the --amend flag to get git commit -a -amend.

Mureinik
  • 297,002
  • 52
  • 306
  • 350