Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amending old commit

Tags:

git

I have the commit history as below:

* 8cd26ba 2013-06-26 | history server-side (HEAD, noXHR)
* bffd858 2013-06-25 | popups and modals
* d95c5f4 2013-06-21 | Map update for new interaction
...

And when I've already committed '8cd26ba' I've found a bug in modal mechanism and want to fix it. I've tried to amend 'bffd858' (because fix is related to it) as it described here. I've did the following steps:

  1. typed

    $ git rebase -i bffd858
    
  2. git shows me (in nano)

    pick 6fa566b history server-side
    # Rebase bffd858..6fa566b onto bffd858
    #
    # Commands:
    #  p, pick = use commit
    #  r, reword = use commit, but edit the commit message
    #  e, edit = use commit, but stop for amending
    #  s, squash = use commit, but meld into previous commit
    #  f, fixup = like "squash", but discard this commit's log message
    #  x, exec = run command (the rest of the line) using shell
    #
    # If you remove a line here THAT COMMIT WILL BE LOST.
    # However, if you remove everything, the rebase will be aborted.
    #
    
  3. I've replaced 'pick' with 'edit'

  4. git said me:

    Stopped at 8cd26ba... history server-side
    You can amend the commit now, with
    
        git commit --amend
    
    Once you are satisfied with your changes, run
    
        git rebase --continue
    
  5. I've apply my bug-fix and typed

    $ git commit -a --amend
    
  6. typed

    git rebase --continue
    
  7. And then I've found my bug-fix in '8cd26ba' (last commit)!

What have I done wrong?

like image 703
Roman Bekkiev Avatar asked Feb 17 '23 02:02

Roman Bekkiev


1 Answers

Your error is that when you do a rebase, you want to give the id of the parent of the earliest commit you want to modify. In your case, you wanted to modify bffd858, whose parent is d95c5f4 also known as bffd858^ or bffd858~1 (I prefer that last syntax since it works with shell that do interpret ^ as a special character).

You should instead have done:

$ git rebase --interactive bffd858~1

and changed the file so that it read:

pick bffd858 popups and modals
fixup 6fa566b history server-side
# Rebase bffd858..6fa566b onto bffd858
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

and then saved and closed the file.

Usually the easiest way to apply a bug fix and to correct the history is to:

  1. use git commit --fixup=bffd858 when committing your fix,
  2. use git rebase --interactive --autosquash bffd858~1 to rebase,
  3. save the file one it open, and then wait for the rebase to complete.

Your original commit will then have been patched with the fix.

In your case, you only did a rebase with a single commit that you then amended. The rebase part, just rewinded the history to the point after you submitted your fix (ie. did nothing).

like image 96
Sylvain Defresne Avatar answered Feb 23 '23 17:02

Sylvain Defresne