Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer 1

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

or

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. –  Raphael_ 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. –  Raphael_ Jul 23 '13 at 0:56
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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