Git Cheat Sheet
Last updated
Setup & config
One-time setup so your commits are attributed correctly.
| Command | What it does |
|---|---|
git config --global user.name "Ada" | Set your commit name |
git config --global user.email you@x.com | Set your commit email |
git config --list | Show all current config |
git config --global init.defaultBranch main | Name new repos' first branch main |
git config --global core.editor "code --wait" | Set your default editor |
git --version | Print the installed Git version |
Creating & cloning
Start a new repository or copy an existing one.
| Command | What it does |
|---|---|
git init | Create a new repo in the current folder |
git clone <url> | Copy a remote repo locally |
git clone <url> mydir | Clone into a named folder |
git clone --depth 1 <url> | Shallow clone (latest commit only) |
git clone -b dev <url> | Clone and check out the dev branch |
Staging & committing
Record changes into the project history.
| Command | What it does |
|---|---|
git add file.txt | Stage one file |
git add . | Stage all changes in the current dir |
git add -p | Stage selected chunks interactively |
git commit -m "message" | Commit staged changes |
git commit -am "message" | Stage tracked files and commit |
git commit --amend | Edit the last commit |
git rm file.txt | Remove a file and stage the deletion |
Branching
Work on changes in isolation.
| Command | What it does |
|---|---|
git branch | List local branches |
git branch feature | Create a new branch |
git switch feature | Switch to an existing branch |
git switch -c feature | Create and switch in one step |
git checkout feature | Older way to switch branches |
git branch -d feature | Delete a merged branch |
git branch -m newname | Rename the current branch |
Merging & rebasing
Combine the work from two branches.
| Command | What it does |
|---|---|
git merge feature | Merge feature into the current branch |
git merge --no-ff feature | Always create a merge commit |
git rebase main | Replay your commits on top of main |
git rebase -i HEAD~3 | Interactively edit the last 3 commits |
git rebase --continue | Resume after resolving conflicts |
git rebase --abort | Cancel a rebase in progress |
git cherry-pick <hash> | Apply one commit onto this branch |
Remotes (push, pull, fetch)
Sync your local repo with a remote like GitHub.
| Command | What it does |
|---|---|
git remote -v | List configured remotes |
git remote add origin <url> | Link a remote named origin |
git fetch | Download remote changes without merging |
git pull | Fetch and merge the remote branch |
git push | Upload commits to the remote |
git push -u origin main | Push and set the upstream branch |
git push origin --delete feature | Delete a remote branch |
Inspecting (status, log, diff)
See what changed and what is happening.
| Command | What it does |
|---|---|
git status | Show staged, unstaged, and untracked files |
git log | Show commit history |
git log --oneline --graph | Compact history with a branch graph |
git diff | Unstaged changes vs the index |
git diff --staged | Staged changes vs the last commit |
git show <hash> | Show a single commit's changes |
git blame file.txt | Show who last changed each line |
Undoing changes
Recover from mistakes safely.
| Command | What it does |
|---|---|
git restore file.txt | Discard unstaged changes to a file |
git restore --staged file.txt | Unstage a file (keep changes) |
git reset --soft HEAD~1 | Undo last commit, keep changes staged |
git reset --hard HEAD~1 | Undo last commit and discard changes |
git revert <hash> | Make a new commit that undoes another |
git stash | Shelve changes for later |
git stash pop | Reapply the most recent stash |
Every Git command you reach for, on one page. This Git cheat sheet is a quick reference for everyday version control - configuring Git, staging and committing, branching, merging and rebasing, syncing with remotes, and undoing mistakes.
These are standard Git commands that work the same on Linux, macOS, and Windows. Copy what you need, or try them live in the terminal playground - a real shell in your browser, nothing to install.
Git cheat sheet FAQ
Is this Git cheat sheet free?
What is the difference between merge and rebase?
git merge combines two branches by creating a new merge commit that ties their histories together, preserving the exact history of both. git rebase instead replays your commits one by one on top of another branch, producing a linear history with no merge commit. Merge is safe and non-destructive; rebase makes a cleaner history but rewrites commits, so avoid rebasing branches others have already pulled.How do I undo a commit in Git?
git reset --soft HEAD~1 undoes the last commit but keeps the changes staged, while git reset --hard HEAD~1 discards them entirely. For a commit that is already shared, use git revert <hash> to create a new commit that reverses it without rewriting history.