Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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 '11 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. – Xiè Jìléi Jan 5 '11 at 23:39
up vote 2 down vote accepted
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.

share|improve this answer
git rebase -i shows an editor, and what's the text should I type in? – Xiè Jìléi Jan 5 '11 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 '11 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 '13 at 5:47
@Raphael_ What do you mean? What commands are you suggesting? – RJFalconer Jul 22 '13 at 15:54
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 '13 at 0:56

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.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .