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 vim | Set default editor |
git config --list | List all config settings |
git config --global alias.st status | Create a command alias |
Getting Started
| Command | Description |
|---|---|
git init | Initialize 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 status | Show working tree status |
git add <file> | Stage a specific file |
git add . | Stage all changes in current directory |
git add -p | Interactively stage hunks |
git commit -m "message" | Commit staged changes with a message |
git commit --amend | Modify the most recent commit (message or files) |
git diff | Diff unstaged changes |
git diff --staged | Diff staged changes vs last commit |
git stash | Stash uncommitted changes |
git stash pop | Apply and remove the latest stash |
git stash list | List all stashes |
git reset HEAD <file> | Unstage a file (keep changes) |
Branch & Merge
| Command | Description |
|---|---|
git branch | List 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~3 | Interactive rebase last 3 commits |
git cherry-pick <sha> | Apply a commit from another branch |
Remote
| Command | Description |
|---|---|
git remote -v | List remotes with URLs |
git remote add origin <url> | Add a remote named origin |
git fetch | Download changes without merging |
git pull | Fetch and merge remote changes |
git pull --rebase | Fetch and rebase instead of merge |
git push | Push current branch to remote |
git push -u origin <branch> | Push and set upstream tracking |
git push --force-with-lease | Safer force push (fails if remote changed) |
Inspection
| Command | Description |
|---|---|
git log | Show commit history |
git log --oneline --graph | Compact graph of commit history |
git log -p | Show commits with diffs |
git show <sha> | Show a specific commit |
git blame <file> | Show who last changed each line |
git bisect start | Start 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~1 | Undo last commit, keep changes staged |
git reset --mixed HEAD~1 | Undo last commit, keep changes unstaged |
git reset --hard HEAD~1 | Undo last commit and discard all changes |
git clean -fd | Remove untracked files and directories |
git restore <file> | Discard working directory changes to a file |
Tags
| Command | Description |
|---|---|
git tag | List tags |
git tag v1.0.0 | Create a lightweight tag |
git tag -a v1.0.0 -m "Release 1.0" | Create an annotated tag |
git push origin v1.0.0 | Push a specific tag to remote |
git push --tags | Push all tags to remote |
git tag -d v1.0.0 | Delete 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.