A quick reference of the most useful Git commands — setup, staging, branching, undoing changes, and working with remotes.
Git is the version-control system behind almost every modern software project. This cheat sheet collects the Git commands you reach for every day, grouped by task so you can find the right one fast.
From your first commit to rebasing and undoing mistakes, keep this page bookmarked as a quick reference. Each command is shown with a short explanation of what it does.
Setup & Configuration
| Command | What it does |
|---|
git config --global user.name "Name" | Set the name attached to your commits. |
git config --global user.email "email" | Set the email attached to your commits. |
git config --list | List all configured settings. |
git config --global init.defaultBranch main | Use 'main' as the default branch name. |
git config --global alias.co checkout | Create a shortcut alias (e.g. git co). |
Creating & Cloning
| Command | What it does |
|---|
git init | Create a new Git repository in the current folder. |
git clone <url> | Clone a remote repository locally. |
git clone <url> <dir> | Clone into a specific directory. |
git clone --depth 1 <url> | Shallow clone with only the latest commit. |
Staging & Committing
| Command | What it does |
|---|
git status | Show changed, staged, and untracked files. |
git add <file> | Stage a specific file for commit. |
git add . | Stage all changes in the current directory. |
git reset <file> | Unstage a file, keeping its changes. |
git commit -m "message" | Commit staged changes with a message. |
git commit -am "message" | Stage tracked files and commit in one step. |
git commit --amend | Modify the most recent commit. |
Branching & Merging
| Command | What it does |
|---|
git branch | List local branches. |
git branch <name> | Create a new branch. |
git switch <name> | Switch to an existing branch. |
git switch -c <name> | Create and switch to a new branch. |
git merge <branch> | Merge a branch into the current one. |
git rebase <branch> | Reapply commits on top of another branch. |
git branch -d <name> | Delete a merged branch. |
git branch -D <name> | Force-delete a branch. |
Remote Repositories
| Command | What it does |
|---|
git remote -v | List configured remotes. |
git remote add origin <url> | Add a remote named 'origin'. |
git fetch | Download objects and refs without merging. |
git pull | Fetch and merge changes from the remote. |
git push | Upload local commits to the remote. |
git push -u origin <branch> | Push and set the upstream branch. |
Inspecting & Comparing
| Command | What it does |
|---|
git log | Show the commit history. |
git log --oneline | Compact one-line-per-commit history. |
git diff | Show unstaged changes. |
git diff --staged | Show staged changes. |
git show <commit> | Show details of a specific commit. |
git blame <file> | Show who last changed each line. |
Undoing Changes
| Command | What it does |
|---|
git restore <file> | Discard unstaged changes to a file. |
git reset --soft HEAD~1 | Undo last commit, keep changes staged. |
git reset --hard HEAD~1 | Undo last commit and discard changes. |
git revert <commit> | Create a new commit that undoes a commit. |
git clean -fd | Remove untracked files and directories. |
Stashing
| Command | What it does |
|---|
git stash | Save uncommitted changes for later. |
git stash list | List all stashes. |
git stash pop | Apply and remove the latest stash. |
git stash apply | Apply the latest stash, keeping it. |
git stash drop | Delete the latest stash. |
Git Commands Cheat Sheet FAQs
What is the difference between git merge and git rebase?
Merge combines two branches and creates a merge commit, preserving history as-is. Rebase moves your commits on top of another branch for a linear history. Avoid rebasing commits you have already pushed and shared.
How do I undo my last commit?
Use git reset --soft HEAD~1 to undo the commit but keep your changes staged, or git reset --hard HEAD~1 to discard the changes entirely. Use git revert to undo a commit that is already pushed.
What is the difference between git fetch and git pull?
git fetch downloads new commits from the remote without changing your working branch. git pull does a fetch and then merges those changes into your current branch.
How do I create and switch to a new branch?
Run git switch -c branch-name to create a branch and switch to it in one step. The older equivalent is git checkout -b branch-name.
How do I discard local changes to a file?
Use git restore file to discard unstaged changes to a file, or git restore --staged file to unstage it while keeping the changes.