Skip to content

Instantly share code, notes, and snippets.

@searls
Last active June 19, 2018 09:57
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save searls/2601d92a14e36aadb933 to your computer and use it in GitHub Desktop.
Save searls/2601d92a14e36aadb933 to your computer and use it in GitHub Desktop.
git undo script -- obviously git lacks a real undo feature, but sometimes in a moment frustration it can be worrying to not have the most common commands in mind after one makes a mistake. Just add a file named `git-undo` to a directory on your PATH and then run it with `git undo`
#!/usr/bin/env bash -e
cat <<TEXT
Git has no undo feature, but maybe these will help:
===================================================
## Unstage work
Unstage a file
--------------
git reset HEAD <file>
## Uncommit work (leaving changes in working directory):
Undo the last commit
--------------------
git reset --soft HEAD^1
Undo all commits back to the state of the remote master branch
--------------------------------------------------------------
git reset --soft origin/master
## Amend a commit
Change the message
------------------
git commit --amend -m 'new message'
Add more changes to the commit (without changing the commit message)
--------------------------------------------------------------------
git add <file>
git commit --amend --no-edit
## Discard uncommitted changes
Discard all uncommitted changes in your working directory
---------------------------------------------------------
git reset --hard HEAD
Discard uncommitted changes to a file
-------------------------------------
git checkout HEAD <file>
## Discard committed changes
Reset the current branch's HEAD to a previous commit
----------------------------------------------------
git reset --hard <commit>
Reset the current branch's HEAD to origin/master
------------------------------------------------
git reset --hard origin/master
## Recovering work after a hard reset
Restore work after you've done a `git reset --hard`
---------------------------------------------------
$ git reflog
1a75c1d... HEAD@{0}: reset --hard HEAD^: updating HEAD
f6e5064... HEAD@{1}: commit: <some commit message>
$ git reset --hard HEAD@{1}
TEXT
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment