Git-Commit: Reuse Commit Messages
$ git commit --reuse-message <commit>
$ git commit -C <commit>
I use the above commands to reuse messages from other commits in the next commit. Any symbolic reference (the HEAD pointer, branch names, tags, hashes, or ancestors) can replace the angle brackets placeholder. In this tutorial, I use the long forms of git commands and options for clarity. You might guess it is not how I do it in my terminal shell. 😉
$ git commit --no-edit --amend
$ git commit --reuse-message HEAD --amend
The two previous commands produce the same outcome. This comparison is to set the --reuse-message
as a generalization of the more common option --no-edit
. The latter applies only to the current commit or the HEAD pointer, whereas --reuse-message
may apply to the next one. For instance, removing the --amend
option from the second command causes the creation of another commit with the same message as its precedent. But doing the same on the first command throws an error.
$ git commit --reedit-message <commit>
$ git commit -c <commit>
It extends the --reuse-message
option and allows us to modify the reused message. Notice that the option abbreviation is the lowercase c, which differs from the cap C for message reuse only.
An Example Situation
I have just edited and staged a file in a commit during an interactive rebase on the main
branch below.
I can execute the git-rebase-continue command immediately, but I do not want git
to invite me to amend the commit message from my text editor. So, I execute the git-commit-amend first.
$ git commit --no-edit --amend
The commit messages of the previous HEAD and the current HEAD are the same <feat: Add custom icon>
as shown in the image below.
I continue rebasing and get a merge conflict that I resolve and stage. This is the history state at this step.
I can once more execute git-rebase-continue and discard the window that pops up to signify that I am not changing the commit message. Instead, I prefer to execute the command below.
$ git commit --reuse-message main^
Then, the current commit message <refactor: Wait for the link target>
is the same as the parent of the tip of the main branch, as the image below shows.
Comments
Post a Comment