Updated June 26, 2026
Git commands cheat sheet
A quick reference to the Git commands you reach for most, grouped by what you are trying to do. Replace placeholders like <branch> and <remote> with your own names.
Setup and config
| Command | What it does |
|---|---|
git init | Create a new repository in the current folder. |
git clone <url> | Copy a remote repository to your machine. |
git config --global user.name "Name" | Set the name attached to your commits. |
git config --global user.email "you@example.com" | Set the email attached to your commits. |
Staging and committing
| Command | What it does |
|---|---|
git status | Show changed, staged, and 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" | Record staged changes with a message. |
git diff | Show unstaged changes line by line. |
git diff --staged | Show changes that are staged for commit. |
git log --oneline | Show a compact history of commits. |
Branching
| Command | What it does |
|---|---|
git branch | List local branches. |
git switch -c <branch> | Create a new branch and switch to it. |
git switch <branch> | Switch to an existing branch. |
git merge <branch> | Merge another branch into the current one. |
git branch -d <branch> | Delete a branch that is already merged. |
Remotes
| Command | What it does |
|---|---|
git remote -v | List configured remotes and their URLs. |
git fetch | Download remote changes without merging them. |
git pull | Fetch and merge changes from the remote branch. |
git push | Upload local commits to the remote branch. |
git push -u origin <branch> | Push a new branch and set it to track the remote. |
Undoing changes
| Command | What it does |
|---|---|
git restore <file> | Discard unstaged changes in a file. |
git restore --staged <file> | Unstage a file but keep its changes. |
git reset --soft HEAD~1 | Undo the last commit but keep changes staged. |
git revert <commit> | Create a new commit that undoes an earlier one. |
git stash | Save uncommitted changes aside and clean the working tree. |
git stash pop | Reapply the most recently stashed changes. |
Reset rewrites history
git reset and force pushes rewrite history. Avoid them on shared branches; prefer git revert, which records an undo as a new commit.References
Questions
What is the difference between git reset and git revert?
git reset moves the branch pointer and can discard commits, rewriting history. git revert leaves history intact and adds a new commit that undoes a previous one, which is safer on shared branches.
What replaced git checkout?
git checkout still works, but newer Git splits its jobs into git switch for changing branches and git restore for discarding file changes, which are clearer.