All cheat sheets
Cheat Sheet

Git Commands

Quick-reference guide to the most common Git commands — configuration, branching, merging, remote operations, undoing changes, stashing, and inspection.

Git is a distributed version control system that tracks changes to files and enables collaboration through branching, merging, and remote repositories. Every project folder under Git control has a .git directory — the local repository that stores all history.

Note: Commands shown below assume Git ≥ 2.x. Some newer options (like git switch and git restore) were added in Git 2.23.

Configuration

CommandWhat it does
git config --global user.name "Name"Set your name for commits (global)
git config --global user.email "e@mail"Set your email for commits (global)
git config --global init.defaultBranch mainDefault branch name for new repos
git config --global alias.co checkoutCreate a shortcut: git co = git checkout
git config --listShow all current config values
git initCreate a new Git repository in the current directory
git init repo-nameCreate a new repo in a new directory

Working with Changes

CommandWhat it does
git statusShow working tree status (changed, staged, untracked files)
git add fileStage a specific file for the next commit
git add .Stage all changes in the current directory
git add -pStage changes interactively per hunk (partial add)
git commit -m "msg"Commit staged changes with a message
git commit -am "msg"Stage all tracked files + commit (skip git add)
git commit --amendEdit the last commit message or add staged changes to it
git diffShow unstaged changes (working tree vs index)
git diff --stagedShow staged changes (index vs last commit)
git diff main..featureCompare two branches
git rm fileRemove a file from tracking + delete it from disk
git mv old newRename or move a file (stages the change)

Branching & Merging

CommandWhat it does
git branchList local branches (* marks the current one)
git branch -aList all branches (local + remote)
git branch nameCreate a new branch from HEAD
git branch -d nameDelete a branch (safe: prevents unmerged deletion)
git branch -D nameForce-delete a branch (even if unmerged)
git switch nameSwitch to an existing branch (Git 2.23+)
git switch -c nameCreate and switch to a new branch
git checkout nameSwitch to an existing branch (classic syntax)
git checkout -b nameCreate and switch to a new branch (classic)
git merge nameMerge the named branch into the current branch
git merge --no-ff nameMerge with a merge commit even if fast-forward is possible
git rebase nameReapply current branch commits on top of the named branch
git rebase -i HEAD~nInteractive rebase: squash, reword, drop, or reorder last n commits
git cherry-pick <hash>Apply a specific commit from another branch onto the current one

Remote Operations

CommandWhat it does
git clone <url>Clone a remote repository into a new directory
git remote -vList remote repositories and their URLs
git remote add origin <url>Add a remote named "origin"
git fetch originDownload objects and refs from a remote (does NOT merge)
git pullFetch + merge (shortcut: git fetch + git merge)
git pull --rebaseFetch + rebase instead of merge (cleaner history)
git pushPush local commits to the upstream branch
git push -u origin branchPush + set upstream tracking for the first time
git push --force-with-leaseForce push safely (refuses if remote has new commits)
git push origin --delete branchDelete a remote branch

Undoing Changes

CommandWhat it does
git restore fileDiscard unstaged changes in a file (Git 2.23+)
git restore --staged fileUnstage a file (keep changes in working tree)
git reset HEAD~1Undo the last commit, keep changes staged
git reset --soft HEAD~1Undo last commit, keep changes staged (same as above)
git reset --mixed HEAD~1Undo last commit, unstage changes (default mode)
git reset --hard HEAD~1Undo last commit AND discard changes entirely
git revert <hash>Create a new commit that undoes the given commit (safe for public history)
git clean -fdRemove untracked files and directories

Stashing

CommandWhat it does
git stashSave uncommitted changes and revert to clean working tree
git stash push -m "name"Stash with a descriptive message
git stash listList all stashes
git stash popApply the latest stash and remove it from the stash list
git stash applyApply the latest stash without removing it
git stash dropDelete the latest stash
git stash clearDelete all stashes

Inspection & History

CommandWhat it does
git logShow commit history (most recent first)
git log --oneline --graphCompact log with ASCII graph of branches
git log --oneline -n 5Show only the last 5 commits
git log --author="name"Filter commits by author
git log --grep="fix"Search commit messages for "fix"
git show <hash>Show the details of a specific commit (diff + metadata)
git blame fileShow who last modified each line of a file
git shortlog -snSummary of commits per author
git tagList all tags
git tag v1.0.0Create a lightweight tag at HEAD
git describe --tagsShow the nearest tag relative to HEAD

Typical Day Workflow

1
git switch -c feature/login

Create a new feature branch from main

2
git add . && git commit -m "add login form"

Make changes, stage, and commit on the feature branch

3
git push -u origin feature/login

Push the feature branch to GitHub/GitLab

4
git switch main && git pull

Switch back to main and get the latest changes

5
git merge feature/login

Merge the completed feature into main

6
git push

Push the updated main to the remote

Generate a .gitignore for your project →

Choose your tech stack and get a ready-to-use .gitignore file instantly.