Git Troubleshooting Cheatsheet

Tags : git

Reset your changes
# On a branch
$ git reset --hard origin/master

# A file
$ git checkout -- /path/to/file.ext
Amend Latest Commit
$ git commit --amend -m "New message"
Unstage
$ git reset HEAD -- readme.md

or (to remove the file from git but keep in disk)

$ git rm --cached readme.md
Commit Back to Staged Usefull when commit was done on the wrong branch
$ git reset --soft HEAD^
Revert Commit
$ git revert HEAD --no-edit
Cancel Commit
Cancel latest commit
$ git reset --soft HEAD~1
or
$ git reset --soft HEAD^

Cancel last 3 commits
$ git reset HEAD~3

Back to specific commit
$ git reset --hard c88df7
Force Push
$ git push origin master --force
or
$ git push origin +master
Delete Commit on the Remote
$ git push origin +c88df7^:my_feature

$ git push <repo> +<commit_hash>^:<branch>
Cherrypicking
$ git cherry-pick c88df7