Git Commands Cheat Sheet
The most useful Git commands in one place — setup, everyday commits, branching, remotes, undoing mistakes and stashing — with clear, copy-ready examples.
Git is the version-control system behind almost every modern software project, but most day-to-day work comes down to a couple of dozen commands. This cheat sheet collects the ones you’ll actually use, grouped by task, with short examples you can copy and adapt. Run these in a terminal from inside your project folder, and swap placeholders like <branch>, <file> and <url> for your own values.
Setup & config
Set your identity once per machine, then create or clone repositories as needed.
| Command | What it does |
|---|---|
git config --global user.name "Your Name" | Sets the name attached to your commits |
git config --global user.email "you@example.com" | Sets the email attached to your commits |
git config --global init.defaultBranch main | Makes main the default branch in new repos |
git config --list | Lists all current configuration values |
git init | Starts a new Git repository in the current folder |
git clone <url> | Copies a remote repository to your machine |
git clone <url> <folder> | Clones into a folder name you choose |
Everyday basics
The core loop: check what changed, stage it, commit it, and review the history.
| Command | What it does |
|---|---|
git status | Shows changed, staged and untracked files |
git add <file> | Stages a specific file for the next commit |
git add . | Stages all changes in the current folder and below |
git add -A | Stages all changes across the whole repository |
git commit -m "message" | Commits the staged changes with a message |
git commit -am "message" | Stages tracked files and commits in one step |
git diff | Shows unstaged changes line by line |
git diff --staged | Shows changes that are already staged |
git log | Shows the full commit history |
git log --oneline | Shows a compact, one-line-per-commit history |
Branching & merging
Branches let you work on features in isolation, then combine them back together.
| Command | What it does |
|---|---|
git branch | Lists local branches (current one is marked *) |
git branch <branch> | Creates a new branch without switching to it |
git switch <branch> | Switches to an existing branch |
git switch -c <branch> | Creates a new branch and switches to it |
git checkout <branch> | Older way to switch branches |
git checkout -b <branch> | Older way to create and switch in one step |
git merge <branch> | Merges the named branch into your current branch |
git branch -d <branch> | Deletes a branch that’s already merged |
git branch -D <branch> | Force-deletes a branch even if unmerged |
git rebase <branch> is an alternative to merging: instead of creating a merge commit, it replays your commits on top of another branch for a cleaner, linear history. It rewrites commit history, so avoid rebasing commits you’ve already shared with others.
Working with remotes
Remotes are the shared copies of your repo, usually hosted on GitHub, GitLab or similar.
| Command | What it does |
|---|---|
git remote -v | Lists configured remotes and their URLs |
git remote add origin <url> | Connects your repo to a remote named origin |
git fetch | Downloads new commits from the remote without merging |
git pull | Fetches and merges remote changes into your branch |
git push | Uploads your commits to the remote |
git push -u origin <branch> | Pushes a branch and sets it to track the remote |
git push origin --delete <branch> | Deletes a branch on the remote |
Undoing things
Mistakes happen. These commands roll back changes at different levels — some are safe, some discard work (see the note at the end).
| Command | What it does |
|---|---|
git restore <file> | Discards unstaged changes to a file |
git restore --staged <file> | Unstages a file but keeps your edits |
git reset --soft <commit> | Moves the branch back but keeps changes staged |
git reset --mixed <commit> | Moves the branch back and unstages changes (the default) |
git reset --hard <commit> | Moves the branch back and discards all changes |
git revert <commit> | Creates a new commit that undoes an earlier one |
git commit --amend | Edits the most recent commit’s message or contents |
Stashing
Stashing shelves your work-in-progress so you can switch tasks with a clean working tree.
| Command | What it does |
|---|---|
git stash | Saves uncommitted changes and reverts to a clean tree |
git stash -u | Stashes untracked files as well |
git stash list | Lists all stashed change sets |
git stash pop | Restores the latest stash and removes it from the list |
git stash apply | Restores the latest stash but keeps it in the list |
git stash drop | Deletes the latest stash without restoring it |
Inspecting history
When you need to understand how the code got to its current state.
| Command | What it does |
|---|---|
git log --oneline --graph | Shows a compact history with a branch/merge graph |
git log --oneline --graph --all | Same graph, but for every branch at once |
git show <commit> | Shows the details and changes of one commit |
git blame <file> | Shows who last changed each line of a file |
git diff <commit1> <commit2> | Compares two commits |
A note on safety
A few commands can permanently discard work: git reset --hard throws away uncommitted changes and committed history with no easy undo, and force-pushing (git push --force) can overwrite commits on the remote that teammates rely on. When in doubt, prefer git revert (which is reversible) and use git push --force-with-lease instead of a plain force push so you don’t clobber someone else’s work.