Bookmark this page. Use Ctrl+F (or Cmd+F on Mac) to find what you need. This cheat sheet covers Git commands from basics to advanced workflows.

Last updated: April 2026

Setup and Config

CommandDescription
git config --global user.name "Alex"Set your name (shown in commits)
git config --global user.email "alex@example.com"Set your email
git config --global init.defaultBranch mainSet default branch name
git config --listShow all config settings
git initCreate a new repository
git clone <url>Clone a remote repository
git clone <url> <folder>Clone into a specific folder

Basic Workflow

CommandDescription
git statusShow changed, staged, and untracked files
git add <file>Stage a file for commit
git add .Stage all changes
git add -pStage changes interactively (hunk by hunk)
git commit -m "message"Commit staged changes
git commit -am "message"Stage tracked files and commit (does NOT stage untracked files)
git commit --amendModify the last commit (add files or fix message)
git diffShow unstaged changes
git diff --stagedShow staged changes (ready to commit)
git diff HEADShow all changes (staged + unstaged)
Working Directory → git add → Staging Area → git commit → Repository

Viewing History

CommandDescription
git logShow commit history
git log --onelineOne line per commit
git log --oneline --graph --allVisual branch graph
git log -5Show last 5 commits
git log --author="Alex"Filter by author
git log --since="2026-01-01"Commits after a date
git log -- <file>History of a specific file
git show <commit>Show details of a commit
git blame <file>Show who changed each line

Branching

CommandDescription
git branchList local branches
git branch -aList all branches (local + remote)
git branch <name>Create a new branch
git branch -d <name>Delete a branch (safe — won’t delete unmerged)
git branch -D <name>Force delete a branch
git branch -m <old> <new>Rename a branch
git switch <name>Switch to a branch
git switch -c <name>Create and switch to a new branch
git checkout <name>Switch to a branch (older syntax)
git checkout -b <name>Create and switch (older syntax)

Branch Workflow (ASCII Diagram)

main:     A --- B --- C --- F (merge commit)
               \         /
feature:        D --- E -

Merging

CommandDescription
git merge <branch>Merge branch into current branch
git merge --no-ff <branch>Force a merge commit (no fast-forward)
git merge --squash <branch>Merge all commits as one (does not auto-commit)
git merge --abortCancel a merge in progress

Resolving Conflicts

When Git cannot auto-merge, it marks conflicts in the file:

<<<<<<< HEAD
your changes
=======
their changes
>>>>>>> feature-branch

Fix the conflict, then:

git add <resolved-file>
git commit

Rebasing

CommandDescription
git rebase <branch>Replay current commits on top of branch
git rebase --abortCancel a rebase in progress
git rebase --continueContinue after resolving a conflict
git rebase -i HEAD~3Interactive rebase — squash, edit, reorder last 3 commits

Merge vs Rebase

# Merge — creates a merge commit, preserves history
git checkout main
git merge feature

# Rebase — replays commits, creates linear history
git checkout feature
git rebase main

Rule of thumb: Rebase your local branches. Never rebase branches that others are working on.

Remote Repositories

CommandDescription
git remote -vShow remote URLs
git remote add origin <url>Add a remote
git pushPush to the tracking remote branch
git push -u origin <branch>Push and set upstream tracking
git push origin --delete <branch>Delete a remote branch
git pullFetch + merge from remote
git pull --rebaseFetch + rebase from remote
git pull --ff-onlyPull only if fast-forward is possible (safest)
git fetchDownload changes without merging
git fetch --allFetch from all remotes
git fetch --pruneFetch and remove deleted remote branches

Undo Changes

CommandDescription
git restore <file>Discard unstaged changes in a file
git restore --staged <file>Unstage a file (keep changes)
git reset HEAD~1Undo last commit (keep changes unstaged)
git reset --soft HEAD~1Undo last commit (keep changes staged)
git reset --hard HEAD~1Undo last commit and discard changes
git revert <commit>Create a new commit that undoes a commit
git clean -fdRemove untracked files and directories

When to Use What

SituationCommand
Undo uncommitted file changesgit restore <file>
Unstage a filegit restore --staged <file>
Undo the last commit (keep work)git reset HEAD~1
Undo a commit already pushedgit revert <commit>
Recover a deleted branch/commitgit reflog

Stash

CommandDescription
git stashSave changes and clean working directory
git stash -m "description"Stash with a message
git stash listShow all stashes
git stash popApply latest stash and remove it
git stash applyApply latest stash (keep in stash list)
git stash dropRemove latest stash
git stash clearRemove all stashes
git stash pop "stash@{2}"Apply a specific stash (quote braces in Zsh)

Tags

CommandDescription
git tagList all tags
git tag v1.0.0Create a lightweight tag
git tag -a v1.0.0 -m "Release 1.0"Create an annotated tag
git push origin v1.0.0Push a specific tag
git push origin --tagsPush all tags
git tag -d v1.0.0Delete a local tag

Advanced Commands

CommandDescription
git cherry-pick <commit>Apply a specific commit to current branch
git bisect startStart binary search for a bug
git bisect badMark current commit as bad
git bisect good <commit>Mark a commit as good
git reflogShow history of HEAD movements (recovery tool)
git log -S "text"Find commits that added/removed a string (pickaxe)
git log --diff-filter=D --name-onlyFind deleted files
git worktree add ../temp mainCheck out a branch in a separate folder
git push --force-with-leaseSafer force push (fails if remote changed)
git branch --set-upstream-to=origin/<branch>Set upstream tracking branch

Recovering Lost Commits

# See all recent HEAD movements
git reflog

# Restore to a previous state
git reset --hard HEAD@{3}

# Or create a branch from a lost commit
git branch recovered HEAD@{3}

.gitignore

# Compiled files
*.class
*.o
build/

# IDE files
.idea/
.vscode/
*.iml

# OS files
.DS_Store
Thumbs.db

# Environment files
.env
.env.local

# Dependencies
node_modules/

Check if a file is ignored: git check-ignore -v <file>

Common Workflows

Feature Branch Workflow

git switch -c feature/login
# ... make changes ...
git add .
git commit -m "Add login page"
git push -u origin feature/login
# Create pull request on GitHub
# After merge, clean up:
git switch main
git pull
git branch -d feature/login

Fix a Mistake on the Wrong Branch

# Stash your changes
git stash
# Switch to the correct branch
git switch correct-branch
# Apply stashed changes
git stash pop

Common Mistakes

  1. Committing to the wrong branch — Use git stash, switch branches, then git stash pop. If you already committed, use git reset HEAD~1 to undo the commit (changes stay), then switch branches.

  2. Force pushing to maingit push --force rewrites remote history and can destroy your team’s work. Use git push --force-with-lease instead — it only force-pushes if no one else has pushed since your last fetch.

  3. Forgetting to pull before push — Always git pull (or git pull --rebase) before pushing. Otherwise your push will be rejected if the remote has new commits.