← Home

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 directory
git clone <url>Clone a remote repository to your local machine
git 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 repositories
git config --global user.email "email"Set your email for all repositories
git config --listShow all configuration settings
git config --global core.editor "code --wait"Set VS Code as default Git editor
git config --global init.defaultBranch mainSet default branch name to main
git config --global alias.co checkoutCreate a shorthand alias (git co → git checkout)
git remote -vShow configured remote repositories and their URLs

Staging & Commits

git statusShow working tree status — modified, staged, and untracked files
git add <file>Stage a specific file for the next commit
git add .Stage all changes in the current directory and subdirectories
git add -AStage all changes including deletions across the entire repo
git add -pInteractively stage hunks — review each change before staging
git commit -m "message"Commit staged changes with an inline message
git commit -am "message"Stage tracked modified files and commit in one step
git commit --amendModify the most recent commit (message or contents)
git commit --amend --no-editAdd staged changes to the last commit without changing message
git diffShow unstaged changes in working directory
git diff --stagedShow staged changes ready to be committed
git diff <branch1> <branch2>Compare two branches side by side
git stashTemporarily save uncommitted changes and clean working tree
git stash popApply the most recent stash and remove it from stash list
git stash listShow all saved stashes
git stash apply stash@{n}Apply a specific stash without removing it
git stash drop stash@{n}Delete a specific stash entry
git stash -uStash including untracked files

Branching & Merging

git branchList all local branches (current branch marked with *)
git branch -aList all branches including remote-tracking branches
git 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 unmerged
git branch -m <old> <new>Rename a branch
git checkout <branch>Switch to an existing branch
git checkout -b <name>Create and switch to a new branch in one step
git switch <branch>Switch branches (modern alternative to checkout)
git switch -c <name>Create and switch to a new branch
git merge <branch>Merge a branch into the current branch
git merge --no-ff <branch>Merge with a merge commit even if fast-forward is possible
git merge --squash <branch>Squash all branch commits into one before merging
git rebase <branch>Reapply commits on top of another base branch
git rebase -i HEAD~nInteractive rebase — squash, reorder, edit last n commits
git cherry-pick <hash>Apply a specific commit from another branch

Remote Operations

git remote add origin <url>Add a remote repository named origin
git remote -vList remote connections with fetch and push URLs
git remote rename <old> <new>Rename a remote
git remote remove <name>Remove a remote connection
git fetchDownload objects and refs from remote without merging
git fetch --allFetch from all configured remotes
git fetch --pruneFetch and remove remote-tracking branches that no longer exist
git pullFetch and merge remote changes into current branch
git pull --rebaseFetch and rebase local commits on top of remote changes
git pushPush commits to the remote tracking branch
git push -u origin <branch>Push and set upstream tracking for the branch
git push --force-with-leaseForce push with safety check (fails if remote has new commits)
git push origin --delete <branch>Delete a remote branch
git push --tagsPush all local tags to the remote

History & Inspection

git logShow full commit history for the current branch
git log --onelineCondensed log — one line per commit
git log --oneline --graph --allVisual branch topology with ASCII graph
git log --author="name"Filter commits by author
git log --since="2 weeks ago"Show commits from a relative date
git log -p <file>Show commit history with diffs for a specific file
git log --statShow commit history with file change statistics
git show <hash>Display details and diff for a specific commit
git blame <file>Show who last modified each line of a file
git bisect startStart binary search to find which commit introduced a bug
git 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 author
git 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 commit
git reset HEAD <file>Unstage a file (legacy equivalent of restore --staged)
git reset --soft HEAD~1Undo last commit, keep changes staged
git reset --mixed HEAD~1Undo last commit, keep changes unstaged (default)
git reset --hard HEAD~1Undo last commit and discard all changes permanently
git reset --hard origin/mainReset branch to match remote exactly
git revert <hash>Create a new commit that undoes a specific commit (safe)
git revert HEADRevert the most recent commit
git clean -fdRemove untracked files and directories
git clean -nDry run — show what would be removed without deleting
git checkout -- <file>Discard changes in working directory (legacy)
git rm --cached <file>Remove file from staging/tracking but keep on disk

FAQ

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.

Related Resources