3

I know how to edit an old commit manually:

$ git log --pretty=format:'%h %s'
    60e5ed9 Second commit
    0fbc8ed First commit
$ git rebase --interactive 0fbc8ed # Going back to 'First commit'
    # * $EDITOR gets fired up *
    # change 'pick 0fbc8ed' to 'edit 0fbc8ed'
$ echo 'Hello Kitteh!' > some_file
$ git add some_file
$ git commit --amend -m 'some message'
$ git rebase --continue # Go back

The problem here:

git rebase --interactive fires up an editor, which is kinda bad for scripting purpose. Is there any way to overcome this, i.e. directly passing edit 0fbc8ed to the git rebase command?

Is it idiotic what I'm attempting or is there maybe a clearer, alternative way to do this?

There is a similar question, but in my case I want to change pick to edit:

How can I automatically accept what git rebase --interactive presents to me?

Community
  • 1
  • 1
Sahib
  • 223
  • 1
  • 10
  • possible duplicate of [How can I easily fixup a past commit?](http://stackoverflow.com/questions/3103589/how-can-i-easily-fixup-a-past-commit) – CharlesB Apr 24 '12 at 16:05

2 Answers2

2

This is a job for "git filter-branch" and the option "--commit-filter". Look at the manual page here exists a example section.

silvio
  • 2,174
  • 20
  • 36
  • 1
    `git commit --fixup` plus `git rebase -i --autosquash` is much better – CharlesB Apr 24 '12 at 16:06
  • yea you are right, but --fixup prepend on your message a "fixup!" - or? and you can only change the subject line of a commit? – silvio Apr 24 '12 at 16:09
  • 4
    It is `git commit --fixup `, and then `rebase -i --autosquash ~1` squashes the fixup commit with the one you're fixing, and the magic happens – CharlesB Apr 24 '12 at 16:10
  • 1
    This looks promising. But `rebase -i` still spawns an editor? But I guess I can set `EDITOR` to `/bin/true` to auto-accept it Edit: Yupp, this works. Thanks a lot! – Sahib Apr 24 '12 at 16:20
2

You can do the same thing without an interactive rebase:

git branch some_temporary_name # to save your initial place
git reset --hard commit_you_want_to_change
...edit...
git commit --amend -m 'some message'
git rebase your_initial_branch_name some_temporary_name
git checkout your_initial_branch_name
git merge some_temporary_name # This will be a ff since you rebased
git branch -d some_temporary_name # remove unneeded branch
antlersoft
  • 14,636
  • 4
  • 35
  • 55