DEV Community

Discussion on: Git & GitHub made simple - Undoing commits

Collapse
 
thajeztah profile image
Sebastiaan van Stijn

Nice write-up!

There's also HEAD^ (one commit "ago") HEAD^^ (two commits ago), etc.

If you went back some commits, and want to move "forward" again, use HEAD@{1} ("go back to previous action in history").

What's "history"? You can find a log of everything you did with git reflog. git reflog can be your rescue if you really messed up :D

here's history of "undoing" 3 commits (HEAD^^^) and forward again (HEAD@{1})

git reflog

d6c1b650 (HEAD -> more_cleanups) HEAD@{0}: reset: moving to HEAD@{1}
d0951081 HEAD@{1}: reset: moving to HEAD^^^
d6c1b650 HEAD@{2}: commit: drivers/bridge: dont use types.ParseCIDR() for fixed value
6cef9c81 HEAD@{3}: commit: windows: remove redundant init()
d511c60c (upstream/master, origin/master, origin/HEAD, master) HEAD@{4}: checkout: moving from master to more_cleanups
Enter fullscreen mode Exit fullscreen mode

You can "reset" your state to any of those, using either the commit (e.g. git reset d6c1b650) or the "convenience" references (HEAD, HEAD@{1}, HEAD@{2} etc)

Collapse
 
didof profile image
Francesco Di Donato

Thank you so much for your comment, I appreciate it and it's really informative. I'm planning on next days to edit this post and add the "git revert" command.
With your permission I'd like to add your information (quoting your name of course) 😁

Collapse
 
thajeztah profile image
Sebastiaan van Stijn

Ha! I didn't know you were brothers ☺️

Yes definitely avoid reflog if you can; it's not something you'd use in your daily flow.

Tips I would have;

  • branches are "cheap" when using git; working on some changes and want to try some variations? create a new branch from your current one and try what it looks like. It's easy to remove the branch later
  • working on a merge conflict, and afraid you'll screw up? just create a new branch as backup before you start.

Adding to the "merge conflict" case; if you need to rebase a pull request (and/or fix a merge conflict); GitHub can be your backup; as long as you don't "push", you can always return to the previous state (without using reflog πŸ˜‰)

Feel free to use whatever I wrote in your blog; I enjoyed reading your write up, and hope it helps others get comfortable in the world of Git black magic

Collapse
 
leodido profile image
Leo Di Donato

I was waiting a bit to introduce my little brother to reflog!
There’s a lot of pain and gain there ahahaha

Glad that Sebastian introduced him with this comment! πŸ€—
Tnx

Very informative and well written post, bravo FrΓ