I want to change a word in commit HEAD^2, how to do it?

EDIT These commits have not been pushed yet. So it doesn't shown as pick xxxxxx in the interactive editor.

  • I had a mistake in my answer. You should rebase onto your branch's parent, not the branch itself. Else you'll get a screen with just "noop" in the commit list. Most of the time your parent will just be your master branch. "git rebase -i master"
    – RJFalconer
    Jan 5, 2011 at 18:17
  • Thank you, I have successfully done. I'm working on branch master, and the command is git rebase -i origin, here origin refers to origin/master.
    – Lenik
    Jan 5, 2011 at 23:39

2 Answers 2

git rebase -i <name your branch's parent, probably "master">

Vim will open and you will see something like this;

pick ba95551 Commit_message_1
pick 3e357b5 Commit_message_for_commit_I_want_to_edit
pick 5eee414 Commit_message_3
pick c9a1f38 Commit_message_4
  1. Position cursor (with mouse/arrow keys/or the commands "h" "j" "k" "l) to the start of the line for the commit you want to edit.
  2. Press "Insert" to enter edit mode
  3. Type "e" for "edit"
  4. Delete the word "pick" ("esc" "d" "w" for "delete word", or just hit delete a few times)

You should see something like this;

pick ba95551 Commit_message_1
e 3e357b5 Commit_message_for_commit_I_want_to_edit
pick 5eee414 Commit_message_3
pick c9a1f38 Commit_message_4

Press "Esc" to enter command mode. Type ":wq" to write, quit. Hit enter.

Git will now reapply your commits up to that point. Then you're free to "edit previous commit" as you would normally:

git commit --amend


git gui

Interactive rebase also allows you to re-order and remove commits of your choice. You should be aware that re-writing history like this will affect those who have pulled from you. See Recovering from upstream rebase.

  • git rebase -i shows an editor, and what's the text should I type in?
    – Lenik
    Jan 5, 2011 at 10:11
  • The editor is vim. I've updated my answer with the vim commands you'll need. Hope this helps!
    – RJFalconer
    Jan 5, 2011 at 18:11
  • Way easier to do if you just have the commit hash, or reference it as HEAD~N, because git won't have to rebase all your commits.
    – rmobis
    Jul 21, 2013 at 5:47
  • git rebase -i HEAD~N will only rebase up to the n-th commit. Or even git rebase -i {CommitHash}^. I found it useful because I had almost 4k commits, and when I tried git rebase -i origin, it did try to rebase all of them. Needless to say it was gonna take hours.
    – rmobis
    Jul 23, 2013 at 0:56
  • Ah I see. Yes, that's a good plan. (Although wow, 4k commits on a branch!)
    – RJFalconer
    Jul 23, 2013 at 8:06

If you literally want to edit HEAD^<n>, then the way to do just that would be to run:

git rebase -i HEAD^<n+1>`

The first commit in the list should be HEAD^n; change its command from pick to edit.

When it returns you to a prompt, make your amendments, commit them, and run git rebase --continue.

Note: If you only wanted to change the contents of that specific commit, you may want/need to edit the next commit (HEAD^<n-1>) as well, to de-apply any changes you made. Perhaps the easiest way to do that is to tag that commit first, then fix the rebased version by doing git checkout <tag_name> . from the repository root.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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