Thursday, July 16, 2009

a couple quick git tips

The other day someone asked in #git how to fix something in the middle of an unsuccessful rebase. So, I thought I'd rebase some of my own code, reorder things so there must be a conflict, and then try to duplicate their question.

Only problem was that there was no error, and I hadn't really paid attention to which commits I reordered. Thankfully, I got a bit of help from a gent named doener.

I'm including a rather long gist showing the process. Basically, git reflog is your friend, and you should play with this at some point in a test repo like I've done here.

Note: anytime you git reset --hard be very sure and confident of what you're doing.

> mkdir undo-rebase
~ > cd !$
cd undo-rebase
~/undo-rebase > git init
Initialized empty Git repository in .git/
~/undo-rebase master > touch one
~/undo-rebase master > ga .
~/undo-rebase master > gc -m "one"
Created initial commit 3dfffad: one
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 one
~/undo-rebase master > touch two
~/undo-rebase master > ga .
~/undo-rebase master > gc -m "two"
Created commit 0c36fd9: two
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 two
~/undo-rebase master > touch three
~/undo-rebase master > ga .
~/undo-rebase master > gc -m "three"
Created commit 6c4a8e8: three
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 three
~/undo-rebase master > gl
...
commit 6c4a8e844de2809b23710fe84080187d7c6c89a6
Author: Jack Dempsey <jack.dempsey@gmail.com>
Date: Thu Jul 16 22:40:00 2009 -0400
three
commit 0c36fd9b1cf3bbc4ad29de5a62e4114465c307b3
Author: Jack Dempsey <jack.dempsey@gmail.com>
Date: Thu Jul 16 22:39:54 2009 -0400
two
commit 3dfffad2749dea6a187a32f7dd20fa4c13db452b
Author: Jack Dempsey <jack.dempsey@gmail.com>
Date: Thu Jul 16 22:39:46 2009 -0400
one
~/undo-rebase master > git rebase -i head^^
...
pick 0c36fd9 two
pick 6c4a8e8 three
# Rebase 3dfffad..6c4a8e8 onto 3dfffad
#
# Commands:
# pick = use commit
# edit = use commit, but stop for amending
# squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
~/undo-rebase master > gl
...
commit 39521f7ed570f31b778b4281072131e72ada4c82
Author: Jack Dempsey <jack.dempsey@gmail.com>
Date: Thu Jul 16 22:39:54 2009 -0400
two
commit e0014a06d12f966bf0d332a0e07cbd8c1ecff21c
Author: Jack Dempsey <jack.dempsey@gmail.com>
Date: Thu Jul 16 22:40:00 2009 -0400
three
commit 3dfffad2749dea6a187a32f7dd20fa4c13db452b
Author: Jack Dempsey <jack.dempsey@gmail.com>
Date: Thu Jul 16 22:39:46 2009 -0400
one
~/undo-rebase master > ZOMG wait wait wait
~/undo-rebase master > git reflog show master@{1}
6c4a8e8... master@{1}: commit: three
0c36fd9... master@{2}: commit: two
3dfffad... master@{3}: commit (initial): one
~/undo-rebase master > git reset 6c4a8e8 --hard
HEAD is now at 6c4a8e8 three
commit 6c4a8e844de2809b23710fe84080187d7c6c89a6
Author: Jack Dempsey <jack.dempsey@gmail.com>
Date: Thu Jul 16 22:40:00 2009 -0400
three
commit 0c36fd9b1cf3bbc4ad29de5a62e4114465c307b3
Author: Jack Dempsey <jack.dempsey@gmail.com>
Date: Thu Jul 16 22:39:54 2009 -0400
two
commit 3dfffad2749dea6a187a32f7dd20fa4c13db452b
Author: Jack Dempsey <jack.dempsey@gmail.com>
Date: Thu Jul 16 22:39:46 2009 -0400
one
Voila!
view raw gistfile1.sh hosted with ❤ by GitHub

No comments: