How to undo in Git
One of the best parts in version control is undo. This is a quick collection of undo commands in git.
Unstage a staged file
If you staged a file in mistake, you can unstage it using reset HEAD
:
# unstage file from staging area
$ git reset HEAD <file>
Unmodify a modified file
You made some unsatisfied changes in a file and want to discard it.
You can use checkout --
command:
# discard all changes in a file (not staged)
$ git checkout -- <file>
# discard all modified files in workspac
$ git clean
Change last commit
If you forget to add some files or mess up your commit messages, use --amend
option to edit your last commit:
# stage files you forgotten
git add <forgotten_file>
# amend the last commit
git commit --amend
This command takes your staging area and uses it for the commit.
The amend
commit will replaces your last commit.
Redo last commit
If you made a unwanted commit, you can overwrite it using git reset HEAD^
command like it never happens:
# reset your unwanted commit
$ git reset --soft HEAD^
# edit and stage your new commit
$ git add <files>
# overwrite the unwanted commit
$ git commit -c ORIG_HEAD
Git Configs
edit ~/.gitconfig
file:
[alias]
unstage = reset HEAD
uncommit = reset --soft HEAD^
amend = commit --amend