Skip to content

Instantly share code, notes, and snippets.

@nicolasrouanne
Last active November 6, 2019 10:25
Show Gist options
  • Save nicolasrouanne/cfd46a1f160b21a1de34a0d1e3b00986 to your computer and use it in GitHub Desktop.
Save nicolasrouanne/cfd46a1f160b21a1de34a0d1e3b00986 to your computer and use it in GitHub Desktop.
Git Pro cheatsheet

Git Pro cheatsheet

Rewriting history

git commit --amend # edit last commit
git commit --amend --no-edit # edit last commit w/o changing commit message

Undoing git stuff

When you make a mistake (e.g. a hard reset to a previous commit, losing important work), you can always undo your changes made with git, even when it seems irrevocaly lost

# hard reset to `HEAD~1`, removing the last commit and all its content
$ git reset --hard HEAD~1

Using git reflog you can see all the operations that were performed with git, inclunding the hard reset. You can also go back before this seemingly unrecoverable state

$ git reflog
caa1d2605 (HEAD -> feat/advances) HEAD@{0}: reset: moving to HEAD~1
5f21c1713 (origin/feat/advances) HEAD@{1}: checkout: moving from master to feat/advances
6c41ebcea (master) HEAD@{2}: checkout: moving from feat/advances to master

$ git reset --hard 5f21c1713

This "undo" is also "undoable", as it appears in the git reflog as well

$ git reflog
5f21c1713 (HEAD -> feat/advances, origin/feat/advances) HEAD@{0}: reset: moving to HEAD@{1}
caa1d2605 HEAD@{1}: reset: moving to HEAD~1
5f21c1713 (HEAD -> feat/advances, origin/feat/advances) HEAD@{2}: checkout: moving from master to feat/advances
6c41ebcea (master) HEAD@{3}: checkout: moving from feat/advances to master

$ git reset --hard caa1d2605 # you just "undid the undo", back to step 1 ;)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment