What does -CHEAD mean in git commit?

Viewed 618

On a GitHub documentation page, I saw the following command:

$ git commit --amend -CHEAD
# Amend the previous commit with your change

I've used the command git commit --amend before, but I've yet to come across the -CHEAD argument. What does -CHEAD mean in this context, and how does it differ from --no-edit?

2 Answers

-CHEAD (which I would generally spell as -C HEAD instead) is unnecessary here, but its meaning is simple enough. As the git commit documentation notes, the -C option is short for --reuse-message:

-C <commit>
--reuse-message=<commit>
      Take an existing commit object, and reuse the log message and the authorship information (including the timestamp) when creating the commit.

This is similar to the -c (lowercase) option, with one simple difference:

-c <commit>
--reedit-message=<commit>
      Like -C, but with -c the editor is invoked, so that the user can further edit the commit message.

So -C commit is equivalent to -c commit --no-edit, or, if you prefer, swap the case of the c option and add --edit.

The name HEAD, in all uppercase, works as described in the gitrevisions documentation:

HEAD names the commit on which you based the changes in the working tree

(or equivalently, the currently-checked-out commit). So -C HEAD means to take the commit message from the current commit, without editing it.

With --amend, --no-edit means to take the commit message from the current commit, without editing it, and --edit means to take the commit message from the current commit, but stop to edit it. So this is exactly equivalent for --amend. For other commit options, the -C, -F, or -m option specifies the source of the initial commit message that may either be edited or not-edited as specified.

From the docs for git commit

-C <commit>
--reuse-message=<commit>
Take an existing commit object, and reuse the log message and the authorship information (including the timestamp) when creating the commit.

HEAD is the latest commit in your current history (normally your current branch).

This means, that you use the same commit message as the commit you are amending.

Docs for --no-edit:

Use the selected commit message without launching an editor. For example, git commit --amend --no-edit amends a commit without changing its commit message.

According to those docs, -C will reuse "authorship information (including the timestamp)" while --no-edit will not.

Related