Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to reuse the previous comment on a git commit?

Tags:

Occasionally I will drop into troubleshooting mode and commit/push a number of small but separate commits with a comment like, "Troubleshooting the <something> during deployment to Heroku." I would like to use the same comment for each commit without having to retype it. Is this possible?

like image 819
Seth Avatar asked Aug 25 '13 12:08

Seth


People also ask

How do I overwrite a previous commit?

If you've already created a fresh commit, you'll want to use git rebase -i to squash your commit on top of the old one. After you've made this change locally, and verified your commit looks the way you want it to, you'll have to git push --force to overwrite history on the Github remote.

How do I revert old commit messages?

To change the most recent commit message, use the git commit --amend command. To change older or multiple commit messages, use git rebase -i HEAD~N .

How do you reuse a commit?

To make it work for multiple commits, just create a temporary commit with your newest changes and then use an interactive rebase to squash the previous commit (containing the good commit message) with the new temporary one, keeping the commit message of the old commit.


2 Answers

From the git-commit(1) command documentation,

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

It's then possible using,

  git commit --reuse-message=HEAD 

Update:

You may also need to use the --reset-author option,

--reset-author When used with -C/-c/--amend options, declare that the authorship of the  resulting commit now belongs of the committer. This also renews the author  timestamp. 
like image 188
Ahmed Siouani Avatar answered Sep 28 '22 03:09

Ahmed Siouani


At first, I answered:

I guess git commit --reuse-message=HEAD does it

Then I thought that's not what you wanted and deleted it. Then life caught up and got AFK for a couple of hours. Anyways, despite an answer having already been accepted, I would have suggested:

$ git config alias.troubleshoot '!troubleshoot() { git add -u && git commit -m "Troubleshooting the $1 during deployment to Heroku."; }; troubleshoot' 

And you use it the following way:

  1. modify existing files
  2. (eventually add untracked files)
  3. git troubleshoot foo

Would commit changes (and eventually new files) with "Troubleshooting the foo during deployment to Heroku." as commit message.

like image 25
Gregory Pakosz Avatar answered Sep 28 '22 02:09

Gregory Pakosz