Git Quick Reference

Workflows, branching, merging, rebasing, stashing and undoing changes.

Setup & Config

# Identity
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

# Default branch name
git config --global init.defaultBranch main

# Editor
git config --global core.editor "vim"

# Useful aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.lg "log --oneline --graph --all --decorate"

# View all config
git config --list --show-origin

# Initialize new repo
git init
git init --bare repo.git    # bare repo (for servers)

Basic Workflow

CommandDescription
git statusShow working tree status
git add fileStage a file
git add .Stage all changes
git add -pInteractive staging (hunk by hunk)
git commit -m "msg"Commit staged changes
git commit -am "msg"Stage tracked files + commit
git commit --amendModify last commit (message or content)
git diffUnstaged changes
git diff --stagedStaged changes (ready to commit)
git diff HEADAll changes since last commit
git rm fileRemove file from tracking and disk
git rm --cached fileUntrack file (keep on disk)
git mv old newRename/move a tracked file

Branching

CommandDescription
git branchList local branches
git branch -aList all branches (local + remote)
git branch featureCreate a new branch
git checkout featureSwitch to branch
git checkout -b featureCreate and switch in one step
git switch featureSwitch to branch (modern syntax)
git switch -c featureCreate and switch (modern syntax)
git branch -d featureDelete merged branch
git branch -D featureForce-delete unmerged branch
git branch -m old newRename a branch

Merging & Rebasing

# Merge feature into main
git checkout main
git merge feature

# Merge with no-fast-forward (always creates merge commit)
git merge --no-ff feature

# Abort a conflicted merge
git merge --abort

# Rebase current branch onto main
git rebase main

# Interactive rebase — squash, reorder, edit commits
git rebase -i HEAD~5

# Abort a conflicted rebase
git rebase --abort

# Continue after resolving conflicts
git rebase --continue
Golden rule: never rebase commits that have been pushed to a shared branch.

Resolving conflicts

# After a conflict, files contain markers:
# <<<<<<< HEAD
#   your changes
# =======
#   incoming changes
# >>>>>>> feature

# After editing the file to resolve:
git add resolved-file.js
git commit                     # for merge
git rebase --continue          # for rebase

Remote Operations

CommandDescription
git clone urlClone a repository
git clone --depth 1 urlShallow clone (latest commit only)
git remote -vList remotes with URLs
git remote add origin urlAdd remote
git remote set-url origin urlChange remote URL
git fetchDownload remote changes (no merge)
git fetch --pruneFetch and remove stale tracking branches
git pullFetch + merge
git pull --rebaseFetch + rebase (cleaner history)
git pushPush to remote
git push -u origin featurePush and set upstream tracking
git push origin --delete featureDelete remote branch

Stashing

CommandDescription
git stashStash working changes
git stash -m "description"Stash with a message
git stash -uInclude untracked files
git stash listList all stashes
git stash popApply latest stash and remove it
git stash apply stash@{2}Apply specific stash (keep in list)
git stash drop stash@{0}Delete a specific stash
git stash clearDelete all stashes
git stash show -pShow stash diff

Log & History

# Compact one-line log
git log --oneline

# Graph view with branches
git log --oneline --graph --all --decorate

# Log with diff
git log -p

# Last 5 commits
git log -5

# Commits by author
git log --author="Alex"

# Commits in date range
git log --after="2026-01-01" --before="2026-03-01"

# Search commit messages
git log --grep="fix"

# Search code changes (pickaxe)
git log -S "functionName"

# Show specific commit details
git show abc1234

# Who changed each line
git blame file.js

# Short blame with commit dates
git blame -s --date=short file.js

Undoing Changes

ScenarioCommand
Discard unstaged changes in a filegit checkout -- file or git restore file
Unstage a file (keep changes)git reset HEAD file or git restore --staged file
Undo last commit (keep changes staged)git reset --soft HEAD~1
Undo last commit (keep changes unstaged)git reset HEAD~1
Undo last commit (discard changes)git reset --hard HEAD~1
Create a new commit that undoes a previous onegit revert abc1234
Discard all local changesgit checkout -- . or git restore .
Reset to remote stategit fetch origin && git reset --hard origin/main
git reset --hard is destructive. Use git reflog to recover if needed — it tracks HEAD movements for ~30 days.

Tags

# Create lightweight tag
git tag v1.0.0

# Create annotated tag (recommended)
git tag -a v1.0.0 -m "Release version 1.0.0"

# Tag a specific commit
git tag -a v0.9.0 -m "Beta release" abc1234

# List tags
git tag -l
git tag -l "v1.*"

# Show tag details
git show v1.0.0

# Push a tag to remote
git push origin v1.0.0

# Push all tags
git push origin --tags

# Delete local tag
git tag -d v1.0.0

# Delete remote tag
git push origin --delete v1.0.0

.gitignore Patterns

# Dependencies
node_modules/
vendor/
__pycache__/
venv/

# Build output
dist/
build/
*.o
*.pyc

# Environment & secrets
.env
.env.local
*.pem
*.key

# IDE & OS files
.vscode/
.idea/
*.swp
*.swo
.DS_Store
Thumbs.db

# Logs
*.log
logs/

# Coverage & testing
coverage/
.nyc_output/
*.lcov
PatternMatches
*.logAll .log files in any directory
/buildOnly build in repo root
build/Any directory named build
doc/*.txt.txt files directly in doc/
doc/**/*.txt.txt files anywhere under doc/
!important.logNegate — do track this file
# Stop tracking a file already committed
git rm --cached .env
echo ".env" >> .gitignore
git commit -m "Remove .env from tracking"

# Global gitignore (for OS/IDE files)
git config --global core.excludesfile ~/.gitignore_global