Last active
June 19, 2018 09:57
-
-
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`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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