Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reword a Git commit by its SHA1 hash?

I have created a commit with a commit message that I want to modify. I have not published the commit, yet, so I can rewrite history safely. I can find using git log, so I know its sha1 hash. How can I quickly edit the commit?

like image 779
Bengt Avatar asked Sep 18 '25 22:09

Bengt


1 Answers

You can checkout the commit in question, amend its message and rebase back to your branch manually:

$ git checkout FIRST_COMMIT_SHA
$ git commit --amend
$ git rebase HEAD THE_BRANCH_YOU_CAME_FROM

This git alias will automate this process:

reword = "!f() { branch=`git symbolic-ref --short HEAD`; git checkout $1; git commit --amend; git checkout $branch; }; f"

To add it to your ~/.gitconfig:

$ git config alias.reword "!f() { branch=`git symbolic-ref --short HEAD`; git checkout $1; git commit --amend; git checkout $branch; }; f"

Then use like this:

$ git reword SHA1_OF_THE_COMMIT_TO_BE_REWORDED

Credits:

  • Answer with the original idea
  • Q&A on getting git's current branch
like image 167
Bengt Avatar answered Sep 20 '25 13:09

Bengt