git Cheatsheet
I recommend aliasing common commands in your .gitconfig. Here’s mine as an example.
Common Commands
[W]------------------+ [S]-------------+
[H]-----+ | Working Copy | | Staging Area |
| HEAD | | Working Tree | <--> | Index |
+------+ | Working Directory | | |
+-------------------+ +--------------+
git status
git log --all --oneline --graph --decorate
git log --all --pretty=fuller --stat --graph --decorate
# diff [H] and [W]
git diff
# diff [W] and [S]
git diff --staged
# Stage entire files [W --> S]
git add -A
git add foobar.txt
# Unstage entire files [S --> W]
git restore --staged foobar.txt
# Revert all unstaged changes + delete all untracked/ignored files
git clean -fdx
# Make a commit
git commit
# Make a commit that replaces the tip of the current branch
git commit --amend
# List all branches (verbose)
git branch -avv
# Checkout an existing local branch `myawesomebranch`.
# It will also guess and create a tracking branch if no local branch exists.
git checkout myawesomebranch
# Create new branch `myawesomebranch`
git branch myawesomebranch
# Download from remote `origin`
git fetch
# `git fetch`, then attempt to fast forward to match remote
git pull
Revision Selection
TODO
Rebase
git rebase origin/master
More complex example:
git rebase --onto origin/master foo bar
“Take the bar branch, figure out the patches since it diverged from the foo branch, and replay those patches in the bar branch as if it was based directly off the origin/master branch instead.”
Squash the last N commits
To squash the last 3 commits:
git rebase -i HEAD~3
In the following screen, leave the top commit as pick, and set all of the remaining as squash.
Revert a single commit
git revert PUT_YOUR_COMMIT_HASH_HERE