← home

Git

git · gh cli · dotfiles
Init & Setup
git init
Init new local repo
git init --bare
Init bare repo (server-side, no working tree)
git clone <url>
Clone repo
git clone --depth=1 <url>
Shallow clone (latest commit only)
git clone <url> --branch <b>
Clone specific branch
git clone <url> .
Clone into current directory
Staging & Status
git status
Working tree status
git status -s
Short / compact status
git add <file>
Stage specific file
git add .
Stage all changes
git add -p
Interactively stage hunks
git add -u
Stage tracked files only (no new files)
git restore --staged <file>
Unstage file (keep changes)
git restore <file>
Discard working tree changes destructive
git diff
Unstaged changes
git diff --staged
Staged changes (vs last commit)
git diff <branch1> <branch2>
Diff between branches
Committing
git commit -m "message"
Commit staged changes
git commit -am "message"
Stage tracked + commit in one step
git commit --amend
Edit last commit message or add files
git commit --amend --no-edit
Amend without changing message
git commit --allow-empty -m "msg"
Empty commit (trigger CI, etc)
git commit -S -m "message"
GPG-signed commit
git show <hash>
Show commit diff + metadata
git show <hash>:<file>
Show file contents at commit
Branches
git branch
List local branches
git branch -a
List local + remote branches
git branch <name>
Create branch (stay on current)
git switch <name>
Switch to branch
git switch -c <name>
Create + switch to new branch
git switch -c <name> origin/<name>
Track remote branch locally
git branch -m <old> <new>
Rename branch
git branch -d <name>
Delete branch (merged only)
git branch -D <name>
Force delete branch destructive
git branch --merged
Branches merged into current
git branch --no-merged
Branches not yet merged
Merge & Rebase
git merge <branch>
Merge branch into current
git merge --no-ff <branch>
Merge with merge commit (no fast-forward)
git merge --squash <branch>
Squash all commits into one staged change
git merge --abort
Abort merge conflict in progress
git rebase <branch>
Rebase current branch onto <branch>
git rebase -i HEAD~<n>
Interactive rebase last n commits
git rebase --onto <new> <old>
Rebase onto different base
git rebase --continue
Continue after resolving conflict
git rebase --abort
Abort rebase in progress
git cherry-pick <hash>
Apply single commit to current branch
git cherry-pick <h1>..<h2>
Apply range of commits
Remote
git remote -v
List remotes with URLs
git remote add origin <url>
Add remote
git remote set-url origin <url>
Change remote URL
git remote rename <old> <new>
Rename remote
git remote remove <name>
Remove remote
git fetch
Fetch all remotes (no merge)
git fetch origin <branch>
Fetch specific branch
git pull
Fetch + merge current branch
git pull --rebase
Fetch + rebase (cleaner history)
git push
Push current branch
git push -u origin <branch>
Push + set upstream tracking
git push --force-with-lease
Safe force push (checks remote state)
git push origin --delete <branch>
Delete remote branch
git push --tags
Push all tags to remote
Log & History
git log
Full commit log
git log --oneline
One line per commit
git log --oneline --graph --all
ASCII branch graph all branches
git log -n 10
Last 10 commits
git log --author="siekman"
Filter by author
git log --since="2 weeks ago"
Commits since date
git log --grep="fix"
Search commit messages
git log -S "search string"
Find commits that added/removed string
git log -- <file>
Log for specific file
git log <branch1>..<branch2>
Commits in branch2 not in branch1
git blame <file>
Line-by-line last editor
git shortlog -sn
Commit count per author
git reflog
All HEAD movements (recovery lifeline)
Undo & Reset
git revert <hash>
New commit that undoes <hash> (safe)
git revert HEAD
Revert last commit
git reset --soft HEAD~1
Undo last commit, keep staged
git reset --mixed HEAD~1
Undo last commit, keep unstaged
git reset --hard HEAD~1
Undo last commit, discard all destructive
git reset --hard origin/main
Reset to remote state destructive
git clean -fd
Remove untracked files + dirs destructive
git clean -fdx
Also remove .gitignored files destructive
git checkout <hash> -- <file>
Restore file from specific commit
Stash
git stash
Stash dirty working tree
git stash push -m "description"
Stash with label
git stash -u
Stash including untracked files
git stash list
List all stashes
git stash pop
Apply latest stash + drop it
git stash apply stash@{2}
Apply specific stash (keep it)
git stash drop stash@{1}
Delete specific stash
git stash clear
Drop all stashes
git stash branch <name>
Create branch from stash
Tags
git tag
List all tags
git tag <name>
Create lightweight tag at HEAD
git tag -a <name> -m "msg"
Annotated tag with message
git tag -a <name> <hash>
Tag a past commit
git push origin <tagname>
Push single tag
git push --tags
Push all tags
git tag -d <name>
Delete local tag
git push origin --delete <tagname>
Delete remote tag
git describe --tags
Describe current commit relative to tag
Config & Aliases
git config --global user.name "name"
Set global username
git config --global user.email "mail"
Set global email
git config --list
Show all config values
git config --global core.editor nvim
Set default editor
git config --global pull.rebase true
Always rebase on pull
git config --global init.defaultBranch main
Default branch name
git config --global alias.st status
Create alias: git st
git config --global alias.lg \
"log --oneline --graph --all"
Create alias: git lg
git config --global commit.gpgsign true
Always GPG-sign commits
git config --global gpg.program gpg2
Set GPG binary
git config --global core.excludesFile \
~/.gitignore_global
Global gitignore
Advanced
git bisect start
Start binary search for bug
git bisect good <hash>
Mark commit as good
git bisect bad
Mark current as bad
git bisect reset
End bisect session
git worktree add <path> <branch>
Checkout branch in separate dir
git worktree list
List worktrees
git worktree remove <path>
Remove worktree
git submodule add <url> <path>
Add submodule
git submodule update --init --recursive
Init + fetch all submodules
git archive --format=zip HEAD > out.zip
Export working tree as zip
git grep "pattern"
Search tracked files
git ls-files
List tracked files
git ls-files -o --exclude-standard
List untracked files
git rev-parse HEAD
Get current commit hash
git rev-parse --abbrev-ref HEAD
Get current branch name
git count-objects -vH
Repo size on disk
git gc --aggressive
Garbage collect + compress repo
GitHub CLI (gh)
gh auth login
Authenticate with GitHub
gh repo create <name> --public
Create new GitHub repo
gh repo clone <owner/repo>
Clone repo via gh
gh repo view --web
Open repo in browser
gh pr create --title "t" --body "b"
Create pull request
gh pr list
List open PRs
gh pr checkout <number>
Checkout PR branch locally
gh pr merge <number> --squash
Merge PR (squash)
gh pr view <number> --web
Open PR in browser
gh issue create
Create issue (interactive)
gh issue list
List open issues
gh issue close <number>
Close issue
gh release create <tag>
Create release from tag
gh release list
List releases
gh workflow list
List GitHub Actions workflows
gh workflow run <name>
Manually trigger workflow
gh run list
List recent workflow runs
gh run view <id> --log
View run logs
gh secret set <name>
Set repo secret (prompts for value)
gh gist create <file>
Create gist from file
gh api <endpoint>
Raw GitHub API call