rubo77
rubo77

Reputation: 20845

Amend the second to last commit

If you found a bug in your last commit, you can simply recommit the last commit with

git commit --amend

But if you already committed another commit, how do you resubmit the commit before that one?


Note: I assume, you are aware, that you should only use --amend if you are sure no other developer already used your commit upstream

Upvotes: 46

Views: 39735

Answers (4)

MyTwoCents
MyTwoCents

Reputation: 7624

Well, I landed on this page while searching for the same. Found a better way with many other options

git rebase -i HEAD~2

For VIM Editor :

An editor will open up with the following details

pick 4f4f96f Added git ignore
pick d01e18c Added sample Blog

# Rebase 60e1cd3..d01e18c onto 60e1cd3 (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]

press i, change pick to r or reword, something like below

pick 4f4f96f Added git ignore
r d01e18c Added sample Blog

PRESS esc + : + wq

Another window will open up, change the commit message

PRESS esc + : + wq

For Notepad or Any other Editor:

Edit as a normal text file(pick to r or reword) and save it.

Finally

git push -f 

Upvotes: 30

rubo77
rubo77

Reputation: 20845

In a git GUI you can easily achieve this:

  1. add a temp branch to the newest commit
  2. reset the branch master to the commit before the commit you want to edit
  3. cherry pick all commits from the temp branch one by one to master and edit the messages if desired
  4. push the corrected commit-line with + to force a new push to the already existing branch with

    git push origin +master
    

Upvotes: 0

rubo77
rubo77

Reputation: 20845

@aragaer 's comment is even shorter way of doing the same:

git commit -a --fixup=HEAD^ #(or whatever commit to be fixed)

then

git rebase -i HEAD~3

and you don't need to change anything, so you can just close editor and git will handle the fixup itself.

Note: This doesn't work on my Ubuntu system!

Upvotes: 5

rubo77
rubo77

Reputation: 20845

  1. commit your fixup with

    git commit -a -m "fix commit (this one will be shifted up one line)"

    (the commit message is not important, it will be obsolete once you are finished)

  2. Now the magic:
    rebase the HEAD to the second last commit but edit the commit message before in that way, that you swap the last and the last but one line in the message editor:

    git rebase -i HEAD~3

    The editor will show the last 3 comits like this:

     pick 2a06f16 this was the last commit
     pick 0dbc5ce the last-but-one commit
     pick 2e30418 fix commit (this one will be shifted up one line)
    
     # Rebase 011f3d0..2e30418 onto 011f3d0
     # …
    

    swap the last two lines so the commit message will look for example like this:

     pick 2a06f16 this was the last commit
     fixup 2e30418 fix commit (this one will be shifted up one line)
     pick 0dbc5ce the last-but-one commit
    
     # Rebase 011f3d0..2e30418 onto 011f3d0
     # …
    
  3. Check your log with

    git log HEAD~3
  4. push the corrected commit-line with + to force a new push to the already existing branch with

    git push origin +branchname

Upvotes: 45

Related Questions