218

I was trying to edit an old commit message as explained here.

The thing is that now, when I try to run rebase -i HEAD~5 it says interactive rebase already started.

So then I try: git rebase --continue but got this error:

error: Ref refs/heads/master is at 7c1645b447a8ea86ee143dd08400710c419b945b but expected c7577b53d05c91026b9906b6d29c1cf44117d6ba
fatal: Cannot lock the ref 'refs/heads/master'.

Any ideas?

2
  • 4
    Users are told to add contents for question here, and not in links. One of the reasons is that those links become invalid sooner or later. The link schacon.github.com/history.html no longer works.
    – U. Windl
    Jun 25, 2021 at 17:02
  • The link does work (again?), though. Apr 7, 2022 at 7:13

6 Answers 6

182

It says:

When you save and exit the editor, it will rewind you back to that last commit in that list and drop you on the command line with the following message:

$ git rebase -i HEAD~3
Stopped at 7482e0d... updated the gemspec to hopefully work better
You can amend the commit now, with

It does not mean:

type again git rebase -i HEAD~3

Try to not typing git rebase -i HEAD~3 when exiting the editor, and it should work fine.
(otherwise, in your particular situation, a git rebase -i --abort might be needed to reset everything and allow you to try again)


As Dave Vogt mentions in the comments, git rebase --continue is for going to the next task in the rebasing process, after you've amended the first commit.

Also, Gregg Lind mentions in his answer the reword command of git rebase:

By replacing the command "pick" with the command "edit", you can tell git rebase to stop after applying that commit, so that you can edit the files and/or the commit message, amend the commit, and continue rebasing.

If you just want to edit the commit message for a commit, replace the command "pick" with the command "reword", since Git1.6.6 (January 2010).

It does the same thing ‘edit’ does during an interactive rebase, except it only lets you edit the commit message without returning control to the shell. This is extremely useful.
Currently if you want to clean up your commit messages you have to:

$ git rebase -i next

Then set all the commits to ‘edit’. Then on each one:

# Change the message in your editor.
$ git commit --amend
$ git rebase --continue

Using ‘reword’ instead of ‘edit’ lets you skip the git-commit and git-rebase calls.

12
  • 2
    Also, git rebase --continue goes to the next task in the rebasing process, after you've amended the first commit.
    – Dave Vogt
    Aug 23, 2010 at 13:42
  • 1
    Adding the link to github wiki article for changing a commit message
    – Joy
    Nov 18, 2017 at 18:49
  • this does not seem to work if my commits since the one in question all were done with --allow-empty and have no actual diff. Rebase just says "Nothing to do"
    – lucidbrot
    Jan 11, 2021 at 8:43
  • 1
    @lucidbrot What version of Git are you using? git rebase -i can retain empty commits since Git 2.26 (Q1 2020): stackoverflow.com/a/60532362/6309
    – VonC
    Jan 11, 2021 at 9:53
  • @VonC Interesting, thanks! I'm on 2.17. Didn't realize it was out of date - I've got it from the ubuntu apt
    – lucidbrot
    Jan 11, 2021 at 11:50
174

As Gregg Lind suggested, you can use reword to be prompted to only change the commit message (and leave the commit intact otherwise):

git rebase -i HEAD~n

Here, n is the list of last n commits.

For example, if you use git rebase -i HEAD~4, you may see something like this:

pick e459d80 Do xyz
pick 0459045 Do something
pick 90fdeab Do something else
pick facecaf Do abc

Now replace pick with reword for the commits you want to edit the messages of:

pick e459d80 Do xyz
reword 0459045 Do something
reword 90fdeab Do something else
pick facecaf Do abc

Exit the editor after saving the file, and next you will be prompted to edit the messages for the commits you had marked reword, in one file per message. Note that it would've been much simpler to just edit the commit messages when you replaced pick with reword, but doing that has no effect.

Learn more on GitHub's page for Changing a commit message.

4
  • I did that, and then commits from other branches show up in my branch
    – Reza
    Jul 20, 2020 at 19:18
  • @Reza you might have messed up somthing. Try this in some other repo. This solution works perfectly
    – Punit Vara
    Jul 22, 2020 at 3:01
  • What is confusing a bit is the fact that git says "detached HEAD ..." after saving the modified commit message (using reword), but then a git rebase --continue triggers a "fatal: No rebase in progress?". Maybe point that out in the answer, too. Seen in git 2.26.2.
    – U. Windl
    Jun 25, 2021 at 17:17
  • Perfect answer, especially because of the example and link.
    – bit
    Jun 6 at 12:06
69

FWIW, git rebase interactive now has a reword option, which makes this much less painful!

4
  • 6
    When using reword, why doesn't git simply let you edit the commit messages in that file with the list of commits? Instead it will launch the editor with one commit message file per reword line. This is just unnecessary. Even if other actions than pick or reword require launching external commands, reword would not necessitate that. Feb 24, 2019 at 3:10
  • 1
    Could it be you are confusing one-line commit messages with full commit messages (You see the commit message title (summary) only when rebasing)?
    – U. Windl
    Jun 25, 2021 at 17:20
  • @DanDascalescu, that would be convenient, but I can see internal reasons for Git to not support that. For instance, commit messages can be longer than just the summary line at the top. And perhaps because Git supports all sorts of other things in interactive rebase, and adding support for this would be a one-off. I'm not a rebase -i expert though; I barely use it. So just some thoughts.
    – Antrikshy
    Sep 5, 2022 at 21:45
  • There is one good reason. As the accepted answer quotes "it will rewind you back to that last commit", eg change HEAD so that from another session, you can use simple forms of git commands like git show or look at the files as they were at that commit. Better to concoct a cleaner log message.
    – chipfall
    Feb 27 at 17:17
23

Just wanted to provide a different option for this. In my case, I usually work on my individual branches then merge to master, and the individual commits I do to my local are not that important.

Due to a git hook that checks for the appropriate ticket number on Jira but was case sensitive, I was prevented from pushing my code. Also, the commit was done long ago and I didn't want to count how many commits to go back on the rebase.

So what I did was to create a new branch from latest master and squash all commits from problem branch into a single commit on new branch. It was easier for me and I think it's good idea to have it here as future reference.

From latest master:

git checkout -b new-branch

Then

git merge --squash problem-branch
git commit -m "new message" 

Referece: https://github.com/rotati/wiki/wiki/Git:-Combine-all-messy-commits-into-one-commit-before-merging-to-Master-branch

5
  • 1
    Your answer really saved my day :). I was struggling with rebase -i for like about 2 hours and no success. My commit was behind 18 commits, so you can imagine. This was the simpler and handy way I could found without needing to use rebase. Thanks friend! May 25, 2019 at 16:14
  • I'm unsure what happens to the commit history when merging all commits into one. Maybe explain in your answer, maybe showing an example. I tried to understand the manual page's description of the --squash option, but I failed to understand what it actually does.
    – U. Windl
    Jun 25, 2021 at 17:25
  • As I mentioned, for this work-around, you should not be worried about the individual commits, as you will lose track of it and make it as one with a new message. Sorry for the delay to respond
    – silvio
    Oct 16, 2021 at 22:54
  • That's helpful and really easy, but comes with the downside, there are now n number of files in a single commit, and also lost the entire commit history. Dec 23, 2021 at 22:15
  • Yes, on the first 2 paragraphs I explained that I had a very specific use case where I was working on a branch alone for a long time and didnt need the history. Many files changed wasnt a problem as well. cheers
    – silvio
    Mar 29, 2022 at 17:48
22

To change a commit message anywhere in history:

1- git rebase -i <commit_sha> , <commit_sha> is the SHA one commit before the commit to be changed (here: eb232eb6b): enter image description here

2- change pick (default) to reword in the first line (do not edit message itself)

3- save and exit

4- next you'll see the editor again with the old commit message line alone, so edit it and then save and exit enter image description here

and that's it, now the history is modified and a git push --force-with-lease will replace on remote

5
  • this actually worked. followed it throughoutly. thanks!
    – Alex
    Jul 18, 2022 at 14:26
  • Thank you for the screen shots, as those are helpful to understand quickly.
    – Knight017
    Sep 15, 2022 at 14:15
  • You can add --root to the interactive git rebase command (git rebase -i) to allow editing of the first commit. Be careful!
    – Mini
    Sep 29 at 14:50
  • What does --force-with-lease do? Oct 26 at 14:59
  • forces the new history on remote Oct 26 at 19:10
12

Here's a very nice Gist that covers all the possible cases: https://gist.github.com/nepsilon/156387acf9e1e72d48fa35c4fabef0b4

Overview:

git rebase -i HEAD~X
# X is the number of commits to go back
# Move to the line of your commit, change pick into edit,
# then change your commit message:
git commit --amend
# Finish the rebase with:
git rebase --continue
2
  • I think this is better than reword when you want a long commit message, not just one line Jun 25, 2021 at 9:03
  • @ArielGabizon why? With reword you can add a description below the header -- it always opens an editor anyway.
    – Tom Hale
    Jun 27 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.