Skip to content

Instantly share code, notes, and snippets.

@garybernhardt
Created January 22, 2010 18:15
Show Gist options
  • Save garybernhardt/283992 to your computer and use it in GitHub Desktop.
Save garybernhardt/283992 to your computer and use it in GitHub Desktop.
using the git reflog
# I have a repo with two commits
failbowl:temp(master) grb$ git shortlog
Gary Bernhardt (2):
commit 1
commit 2
# I destroy the second commit
failbowl:temp(master) grb$ git reset --hard HEAD^
HEAD is now at 7454aa7 commit 1
# It's gone
failbowl:temp(master) grb$ git shortlog
Gary Bernhardt (1):
commit 1
# I show the temporal (NOT revision) history of the branch
failbowl:temp(master) grb$ git reflog show master
7454aa7 master@{0}: HEAD^: updating HEAD
a29455f master@{1}: commit: commit 2
# I check out the previous state of the branch (before I removed the second commit)
# Other VCSes can't do this!
failbowl:temp(master) grb$ git checkout master@{1}
Note: moving to 'master@{1}' which isn't a local branch
If you want to create a new branch from this checkout, you may do so
(now or later) by using -b with the checkout command again. Example:
git checkout -b <new_branch_name>
HEAD is now at a29455f... commit 2
# Now the commit is back, but I'm not on a branch (as the warning above said)
failbowl:temp(a29455f...) grb$ git shortlog
Gary Bernhardt (2):
commit 1
commit 2
# I create a branch with the recovered commit
failbowl:temp(a29455f...) grb$ git checkout -b new_branch
Switched to a new branch 'new_branch'
# I can now add to this new branch
failbowl:temp(new_branch) grb$ echo c > file && git add file && git ci -m 'commit 3'
[new_branch_from_history 68058aa] commit 3
1 files changed, 1 insertions(+), 1 deletions(-)
failbowl:temp(new_branch_from_history) grb$ git shortlog
Gary Bernhardt (3):
commit 1
commit 2
commit 3
# Master is still where I left it – truncated to one commit
failbowl:temp(new_branch_from_history) grb$ git checkout master
Switched to branch 'master'
failbowl:temp(master) grb$ git shortlog
Gary Bernhardt (1):
commit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment