All References

Git Commands Cheatsheet

Essential git commands for everyday development — setup, branching, merging, rebasing, stashing, and working with remotes.

Setup & Config

Command Description
git config --global user.name "Name"Set global author name
git config --global user.email "you@example.com"Set global author email
git config --global core.editor vimSet default editor
git config --listList all config settings
git config --global alias.st statusCreate a command alias

Getting Started

Command Description
git initInitialize a new repository in the current directory
git init <dir>Initialize a new repository in <dir>
git clone <url>Clone a remote repository locally
git clone <url> <dir>Clone into a specific directory name
git clone --depth 1 <url>Shallow clone — only latest snapshot

Stage & Snapshot

Command Description
git statusShow working tree status
git add <file>Stage a specific file
git add .Stage all changes in current directory
git add -pInteractively stage hunks
git commit -m "message"Commit staged changes with a message
git commit --amendModify the most recent commit (message or files)
git diffDiff unstaged changes
git diff --stagedDiff staged changes vs last commit
git stashStash uncommitted changes
git stash popApply and remove the latest stash
git stash listList all stashes
git reset HEAD <file>Unstage a file (keep changes)

Branch & Merge

Command Description
git branchList local branches
git branch <name>Create a new branch
git branch -d <name>Delete a merged branch
git branch -D <name>Force-delete a branch
git checkout <branch>Switch to a branch
git checkout -b <branch>Create and switch to a new branch
git switch <branch>Switch branches (modern syntax)
git switch -c <branch>Create and switch (modern syntax)
git merge <branch>Merge <branch> into current branch
git merge --no-ff <branch>Merge, always creating a merge commit
git rebase <branch>Rebase current branch onto <branch>
git rebase -i HEAD~3Interactive rebase last 3 commits
git cherry-pick <sha>Apply a commit from another branch

Remote

Command Description
git remote -vList remotes with URLs
git remote add origin <url>Add a remote named origin
git fetchDownload changes without merging
git pullFetch and merge remote changes
git pull --rebaseFetch and rebase instead of merge
git pushPush current branch to remote
git push -u origin <branch>Push and set upstream tracking
git push --force-with-leaseSafer force push (fails if remote changed)

Inspection

Command Description
git logShow commit history
git log --oneline --graphCompact graph of commit history
git log -pShow commits with diffs
git show <sha>Show a specific commit
git blame <file>Show who last changed each line
git bisect startStart binary search for a bug-introducing commit

Undo

Command Description
git revert <sha>Create a new commit that undoes <sha> (safe)
git reset --soft HEAD~1Undo last commit, keep changes staged
git reset --mixed HEAD~1Undo last commit, keep changes unstaged
git reset --hard HEAD~1Undo last commit and discard all changes
git clean -fdRemove untracked files and directories
git restore <file>Discard working directory changes to a file

Tags

Command Description
git tagList 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 to remote
git push --tagsPush all tags to remote
git tag -d v1.0.0Delete a local tag

Frequently Asked Questions

What is the difference between git merge and git rebase?

Both integrate changes from one branch into another, but they do so differently. git merge creates a new "merge commit" that joins the two branch histories, preserving the exact history of both branches. git rebase moves the commits of your branch on top of another branch, rewriting commit SHAs to produce a linear history. Rebase results in a cleaner log but rewrites history — never rebase commits that have been pushed to a shared branch.

How do I undo the last commit in git?

It depends on what you want to keep. Use git reset --soft HEAD~1 to undo the commit but keep your changes staged (ready to re-commit). Use git reset --mixed HEAD~1 to undo the commit and unstage the changes (files remain modified). Use git reset --hard HEAD~1 to completely discard the commit and all its changes. If the commit has already been pushed, prefer git revert instead, which creates a new undo commit without rewriting history.

What does git stash do?

git stash saves your uncommitted changes (both staged and unstaged) to a temporary stack and reverts your working directory to the last commit. This is useful when you need to quickly switch branches without committing unfinished work. You can later restore the stashed changes with git stash pop (applies and removes the latest stash) or git stash apply (applies but keeps it in the list). You can store multiple stashes and view them with git stash list.