Git Cheat Sheet
Setup & Config
git config --global user.name "Name"
Set your commit author name globally.
git config --global user.email "you@example.com"
Set your commit author email globally.
git init
Turn the current directory into a new git repository.
git clone <url>
Copy a remote repository to your machine.
Basics
git status
Show changed/staged/untracked files.
git add <file>
Stage a file for the next commit.
git add .
Stage all changes in the current directory.
git commit -m "message"
Commit staged changes with a message.
git commit -am "message"
Stage all tracked-file changes and commit in one step.
git diff
Show unstaged changes.
git diff --staged
Show staged changes not yet committed.
Branching & Merging
git branch
List local branches.
git branch <name>
Create a new branch.
git checkout <branch>
Switch to a branch.
git checkout -b <branch>
Create a branch and switch to it in one step.
git switch <branch>
Switch to a branch (newer alternative to checkout).
git merge <branch>
Merge the named branch into the current branch.
git branch -d <branch>
Delete a branch (only if already merged).
git branch -D <branch>
Force-delete a branch, even if unmerged.
Undoing Changes
git restore <file>
Discard uncommitted changes to a file.
git restore --staged <file>
Unstage a file without discarding its changes.
git reset --soft HEAD~1
Undo the last commit, keep changes staged.
git reset --mixed HEAD~1
Undo the last commit, keep changes unstaged.
git reset --hard HEAD~1
Undo the last commit and discard its changes entirely.
git revert <commit>
Create a new commit that undoes a previous commit — safe for shared history.
git commit --amend
Edit the message (or contents) of the most recent commit.
Remote
git remote -v
List configured remotes and their URLs.
git remote add origin <url>
Add a remote named "origin".
git fetch
Download remote history without merging it in.
git pull
Fetch and merge the remote branch into the current one.
git pull --rebase
Fetch and rebase the current branch onto the remote branch.
git push
Upload local commits to the remote.
git push -u origin <branch>
Push a branch and set it to track the remote branch.
git push origin --delete <branch>
Delete a branch on the remote.
Stash
git stash
Temporarily shelve uncommitted changes.
git stash pop
Reapply the most recent stash and remove it from the stash list.
git stash list
List all stashed changesets.
git stash drop
Delete the most recent stash without applying it.
Inspecting History
git log
Show commit history.
git log --oneline --graph --all
Compact, graphical view of all branches' history.
git show <commit>
Show the changes introduced by a specific commit.
git blame <file>
Show who last changed each line of a file.
Rebase & Advanced
git rebase <branch>
Replay the current branch's commits on top of another branch.
git rebase -i HEAD~3
Interactively edit/squash/reorder the last 3 commits.
git cherry-pick <commit>
Apply a specific commit from another branch onto the current one.
git bisect start
Begin a binary search for the commit that introduced a bug.
Tags
git tag
List tags.
git tag v1.0.0
Create a lightweight tag on the current commit.
git tag -a v1.0.0 -m "message"
Create an annotated tag with a message.
git push origin v1.0.0
Push a single tag to the remote.
git push origin --tags
Push all local tags to the remote.
No commands match your search.
Advertisement