Git Cheatsheet

Branches, commits, merge, rebase, stash, reset, log & remote workflows

Version Control
Contents
⚙️

Setup & Config

git config --global user.name "Name"
git config --global user.email "you@mail.com"
git config --global init.defaultBranch main
git config --global core.editor "vim"

git init                        # new repo
git clone <url>                 # clone
git clone --depth 1 <url>       # shallow
📝

Basic Commands

git status
git add file.txt                # stage
git add .                       # stage all
git add -p                      # interactive staging

git commit -m "msg"
git commit -am "msg"           # add+commit tracked
git commit --amend              # edit last commit

git diff                        # unstaged
git diff --staged               # staged
git diff branch1..branch2
🌿

Branching

git branch                      # list local
git branch -a                   # all
git checkout -b feature         # create+switch
git switch -c feature           # modern
git branch -d feature           # safe delete
git branch -D feature           # force delete
git push origin --delete feat   # delete remote
git branch -m old new           # rename
🔀

Merge & Rebase

git merge feature               # merge
git merge --no-ff feature       # always merge commit
git merge --squash feature      # squash

git rebase main                 # rebase onto main
git rebase -i HEAD~3            # interactive

# Resolve conflicts
git add resolved.txt
git rebase --continue
git rebase --abort

git cherry-pick <hash>          # apply one commit
📦

Stash

git stash                       # save WIP
git stash push -m "msg"
git stash -u                    # include untracked
git stash list
git stash pop                   # apply+remove
git stash apply stash@{2}
git stash drop stash@{0}
git stash clear
📊

Log & History

git log --oneline
git log --graph --oneline --all
git log -n 5
git log --author="Alice"
git log --since="2024-01-01"
git log -- file.txt
git log --grep="fix"
git log -S "funcName"           # code search
git blame file.txt
git show <hash>
🌐

Remote

git remote -v
git remote add origin <url>
git push origin main
git push -u origin feature
git push --force-with-lease
git pull
git pull --rebase
git fetch --prune

Undo & Reset

git restore file.txt            # discard changes
git restore --staged file.txt   # unstage

git reset HEAD~1                # undo commit, keep changes
git reset --soft HEAD~1         # keep staged
git reset --hard HEAD~1         # discard all
git reset --hard origin/main

git revert <hash>               # safe undo
git reflog                      # recover lost
🏷️

Tags

git tag v1.0.0
git tag -a v1.0.0 -m "Rel"
git tag -l "v1.*"
git push origin v1.0.0
git push origin --tags
git tag -d v1.0.0
🚀

Advanced

# Worktree
git worktree add ../fix fix-branch
git worktree list

# Bisect (find bug)
git bisect start
git bisect bad
git bisect good v1.0.0
git bisect reset

# Submodules
git submodule add <url> path/
git submodule update --init --recursive

# Clean
git clean -fd
git clean -fdn                  # dry run