Git Cheatsheet
Git commands that I use daily
1. Check what changed in the last commit
Let us say, you want to check what you committed in the last commit. You can issue following command:
git diff HEAD^ HEAD
2. Get files changed in the last commit
You can get information about files only which are changed in the last commit
git diff --name-only HEAD~ HEAD
3. Present log in pretty format
Following command provides pretty handy output for git log
git log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate
You can check this for more options in --pretty
4. Deleting branch remotely
You can delete the branch remotely using
git push origin --delete <branch_name>
or
git branch -dr <remote/branch>
5. Un-commit last commit
Sometime you want to undo the last commit
git reset --soft HEAD^
6. Autocorrect git commands
If you make typos in git command, this is for
git config --global help.autocorrect 1
7. Switching branches like `cd -` command
You can switch back and forth using following command
git checkout -
8. Print the name of the upstream branch
Check where branches are coming from
git branch -vv
9. Rename a branch
git branch -m <old_branch_name> <new_branch_name>
10. Time machine for Git
Very useful command when you mess up something. You can check this blog for more details.
git reflog
11. Undo last commit and bring everything to staging area
git reset --soft HEAD^
12. Undo last commit and reset everything
git reset --hard HEAD^
13. Use last commit message to amend
git commit -C HEAD --amend
14. Create new branch with stash changes
git stash branch <branch_name>
Hope this will help you in your daily work.