Git Commands Cheatsheet
Comprehensive Git reference — from setup and staging to branching, remotes, history inspection, and undoing mistakes. Bookmark this page and never forget a command again.
Setup & Config
git initInitialize a new Git repository in the current directorygit clone <url>Clone a remote repository to your local machinegit clone --depth 1 <url>Shallow clone — only the latest commit (faster for large repos)git config --global user.name "Name"Set your name for all repositoriesgit config --global user.email "email"Set your email for all repositoriesgit config --listShow all configuration settingsgit config --global core.editor "code --wait"Set VS Code as default Git editorgit config --global init.defaultBranch mainSet default branch name to maingit config --global alias.co checkoutCreate a shorthand alias (git co → git checkout)git remote -vShow configured remote repositories and their URLsStaging & Commits
git statusShow working tree status — modified, staged, and untracked filesgit add <file>Stage a specific file for the next commitgit add .Stage all changes in the current directory and subdirectoriesgit add -AStage all changes including deletions across the entire repogit add -pInteractively stage hunks — review each change before staginggit commit -m "message"Commit staged changes with an inline messagegit commit -am "message"Stage tracked modified files and commit in one stepgit commit --amendModify the most recent commit (message or contents)git commit --amend --no-editAdd staged changes to the last commit without changing messagegit diffShow unstaged changes in working directorygit diff --stagedShow staged changes ready to be committedgit diff <branch1> <branch2>Compare two branches side by sidegit stashTemporarily save uncommitted changes and clean working treegit stash popApply the most recent stash and remove it from stash listgit stash listShow all saved stashesgit stash apply stash@{n}Apply a specific stash without removing itgit stash drop stash@{n}Delete a specific stash entrygit stash -uStash including untracked filesBranching & Merging
git branchList all local branches (current branch marked with *)git branch -aList all branches including remote-tracking branchesgit branch <name>Create a new branch (does not switch to it)git branch -d <name>Delete a branch (safe — only if fully merged)git branch -D <name>Force delete a branch even if unmergedgit branch -m <old> <new>Rename a branchgit checkout <branch>Switch to an existing branchgit checkout -b <name>Create and switch to a new branch in one stepgit switch <branch>Switch branches (modern alternative to checkout)git switch -c <name>Create and switch to a new branchgit merge <branch>Merge a branch into the current branchgit merge --no-ff <branch>Merge with a merge commit even if fast-forward is possiblegit merge --squash <branch>Squash all branch commits into one before merginggit rebase <branch>Reapply commits on top of another base branchgit rebase -i HEAD~nInteractive rebase — squash, reorder, edit last n commitsgit cherry-pick <hash>Apply a specific commit from another branchRemote Operations
git remote add origin <url>Add a remote repository named origingit remote -vList remote connections with fetch and push URLsgit remote rename <old> <new>Rename a remotegit remote remove <name>Remove a remote connectiongit fetchDownload objects and refs from remote without merginggit fetch --allFetch from all configured remotesgit fetch --pruneFetch and remove remote-tracking branches that no longer existgit pullFetch and merge remote changes into current branchgit pull --rebaseFetch and rebase local commits on top of remote changesgit pushPush commits to the remote tracking branchgit push -u origin <branch>Push and set upstream tracking for the branchgit push --force-with-leaseForce push with safety check (fails if remote has new commits)git push origin --delete <branch>Delete a remote branchgit push --tagsPush all local tags to the remoteHistory & Inspection
git logShow full commit history for the current branchgit log --onelineCondensed log — one line per commitgit log --oneline --graph --allVisual branch topology with ASCII graphgit log --author="name"Filter commits by authorgit log --since="2 weeks ago"Show commits from a relative dategit log -p <file>Show commit history with diffs for a specific filegit log --statShow commit history with file change statisticsgit show <hash>Display details and diff for a specific commitgit blame <file>Show who last modified each line of a filegit bisect startStart binary search to find which commit introduced a buggit bisect good <hash>Mark a commit as good (bug not present)git bisect bad <hash>Mark a commit as bad (bug present)git shortlog -snSummarize commit counts by authorgit reflogShow history of HEAD movements (recovery lifeline)Undoing Changes
git restore <file>Discard unstaged changes in a file (restore from index)git restore --staged <file>Unstage a file (keep working tree changes)git restore --source=HEAD~1 <file>Restore a file from a specific commitgit reset HEAD <file>Unstage a file (legacy equivalent of restore --staged)git reset --soft HEAD~1Undo last commit, keep changes stagedgit reset --mixed HEAD~1Undo last commit, keep changes unstaged (default)git reset --hard HEAD~1Undo last commit and discard all changes permanentlygit reset --hard origin/mainReset branch to match remote exactlygit revert <hash>Create a new commit that undoes a specific commit (safe)git revert HEADRevert the most recent commitgit clean -fdRemove untracked files and directoriesgit clean -nDry run — show what would be removed without deletinggit checkout -- <file>Discard changes in working directory (legacy)git rm --cached <file>Remove file from staging/tracking but keep on diskFAQ
What is the difference between git merge and git rebase?
Both integrate changes from one branch into another. git merge creates a new merge commit preserving the full branch history. git rebase replays your commits on top of the target branch, resulting in a linear history. Use merge for shared/public branches and rebase for cleaning up local feature branches before merging.
How do I undo a commit that has already been pushed?
Use git revert <hash> to create a new commit that reverses the changes. This is the safe approach for shared branches because it doesn't rewrite history. Avoid git reset --hard on pushed commits unless you coordinate with your team and force-push.
What is the difference between git fetch and git pull?
git fetch downloads new commits and refs from the remote but does not modify your working directory or current branch. git pull is essentially git fetch followed by git merge (or git rebase with --rebase). Fetch is safer when you want to review remote changes before integrating them.
How do I resolve a merge conflict?
When Git can't auto-merge, it marks conflicted files with <<<<<<<, =======, and >>>>>>> markers. Open each file, choose which changes to keep (or combine both), remove the markers, then git add the resolved files and git commit. Tools like VS Code highlight conflicts and offer one-click resolution.