Git Commands
Quick reference for common git operations.
Repository Setup
Section titled “Repository Setup”git init
Section titled “git init”git init- init new repo
git clone
Section titled “git clone”git clone <url>- clone repo
Branch Management
Section titled “Branch Management”git branch
Section titled “git branch”git branch- list local branchesgit branch -a- list all (local/remote) branches (-a= all)git branch -d <name>- safe delete, only if merged (-d= delete)- use when: cleaning up after PR merged
git branch -D <name>- force delete, even if unmerged (-D= force delete)- use when: abandoning work you don’t need
git checkout
Section titled “git checkout”git checkout <branch>- switch branchgit checkout -b <name>- create + switch (-b= branch)git checkout -b <name> origin/<name>- create from remote- use when: checking out a branch that exists on remote but not locally
Staging & Committing
Section titled “Staging & Committing”git status
Section titled “git status”git status- show working tree statusgit status -s- short format (-s= short)
git add
Section titled “git add”git add <file>- stage filegit add .- stage all- includes untracked (new) files
git commit
Section titled “git commit”git commit -m "msg"- commit with message (-m= message)git commit --amend- amend last commit- fix message or add forgotten files
- don’t use on already pushed commits
git restore
Section titled “git restore”git restore --staged <file>- unstage filegit restore <file>- discard changes- warning: permanently deletes uncommitted changes
Remote Operations
Section titled “Remote Operations”git remote
Section titled “git remote”git remote -v- list remotes (-v= verbose)git remote add <name> <url>- add remotegit remote remove <name>- remove remotegit remote set-url <name> <url>- change URL
git fetch
Section titled “git fetch”git fetch- fetch from remote- check what’s new, doesn’t merge
- safe to run anytime
git fetch --prune- fetch + remove stale branches- use when: cleaning up after PRs merged
git pull
Section titled “git pull”git pull origin <branch>- fetch + merge from specific branch- can create merge commits
git pull --rebase origin <branch>- fetch + rebase (cleaner linear history)- puts your local commits on top of remote commits
- avoids unnecessary merge commits
- use when: syncing with main before pushing
git push
Section titled “git push”git push -u origin <branch>- push + set upstream (-u= upstream)- use when: first push of a new branch
- after this, just
git pushworks
git push origin --delete <name>- delete remote branch
Stashing
Section titled “Stashing”git stash
Section titled “git stash”git stash- stash changes- temporarily save work without committing
- use when: need to switch branches but not ready to commit
git stash push -m "msg"- stash with message (-m= message)git stash -u- include untracked (-u= untracked)git stash pop- apply + remove- use when: returning to stashed work
git stash apply- apply (keep in list)- use when: applying same stash to multiple branches
git stash list- list stashesgit stash drop- remove latestgit stash clear- remove all
History & Diffs
Section titled “History & Diffs”git log
Section titled “git log”git log- full historygit log --oneline- compactgit log --oneline --graph --all- graph all branches- visualize branch structure
git log -n 5- last 5 commits (-n= number)git log --author="name"- by authorgit log -- <file>- for specific file
git diff
Section titled “git diff”git diff- unstaged changesgit diff --staged- staged changes- see what will be committed
git diff <commit>- since commitgit diff <b1>..<b2>- between branches
git show
Section titled “git show”git show <commit>- commit details
Merging & Rebasing
Section titled “Merging & Rebasing”git merge
Section titled “git merge”git merge <branch>- merge into currentgit merge --no-ff <branch>- merge with commit (--no-ff= no fast-forward)- preserves branch history in graph
git merge --squash <branch>- squash merge- combines all commits into one
- use when: cleaning up messy feature branch history
git merge --abort- abort merge
git rebase
Section titled “git rebase”git rebase <branch>- rebase onto branch- rewrites history, use on local branches only
git rebase --continue- continue after conflictgit rebase --abort- abort rebase
git cherry-pick
Section titled “git cherry-pick”git cherry-pick <commit>- apply commit- use when: need specific commit from another branch
Undoing Changes
Section titled “Undoing Changes”git reset
Section titled “git reset”git reset --soft HEAD~1- undo commit, keep staged (--soft= keep staged)- use when: want to edit commit before recommitting
git reset --mixed HEAD~1- undo commit, unstage (--mixed= unstage)- use when: want to restage selectively
git reset --hard HEAD~1- undo commit, discard all (--hard= discard all)- warning: permanently deletes changes
git reset --hard origin/main- reset to remote- use when: local is messed up, want to match remote exactly
git revert
Section titled “git revert”git revert <commit>- create undo commit- safe for pushed commits (doesn’t rewrite history)
git revert --no-commit <commit>- revert (no commit)
git tag
Section titled “git tag”git tag- list tagsgit tag <name>- lightweight taggit tag -a <name> -m "msg"- annotated tag (-a= annotated,-m= message)- use for releases (includes metadata)
git tag -d <name>- delete local (-d= delete)git push origin <tag>- push taggit push --tags- push all tagsgit push origin --delete <tag>- delete remote tag
Worktree
Section titled “Worktree”git worktree
Section titled “git worktree”git worktree add <path> <branch>- create new worktree for branch- work on multiple branches simultaneously
- use when: need to check another branch without stashing
git worktree list- list all worktreesgit worktree remove <path>- remove worktree
Configuration
Section titled “Configuration”git config
Section titled “git config”git config --global user.name "Name"- set usernamegit config --global user.email "email"- set emailgit config --global init.defaultBranch main- set default branchgit config --list- list settings
Useful Aliases
Section titled “Useful Aliases”git config --global alias.st statusgit config --global alias.co checkoutgit config --global alias.br branchgit config --global alias.ci commitgit config --global alias.lg "log --oneline --graph --all"