Skip to content

Git Commands

Git command cheat sheet — setup, staging, commits, branches, remotes, history, undo, and stash. Every command developers reach for daily.

A practical reference for the Git commands you use every day. Organized by what you're trying to do.

Git has hundreds of commands and flags but day-to-day work needs only a few dozen. The tables below are grouped by task. git help <command> opens the full manual entry; git <command> --help works the same way.

Setup & Configuration

CommandDescription
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.
git config --global init.defaultBranch main Use "main" as the default branch in new repos.
git config --global core.editor "vim" Set the editor used for commit messages.
git config --list Show every config value Git is using.
git init Create a new Git repo in the current directory.
git clone <url> Clone a remote repo.
git clone <url> <dir> Clone into a specific directory.
git clone --depth 1 <url> Shallow clone — only the latest commit. Faster for big repos.

Stage & Commit

CommandDescription
git status Show working-tree state and what's staged.
git status -s Compact one-line-per-file status.
git add <file> Stage a file.
git add . Stage every change in the current directory and below.
git add -A Stage every change in the entire repo (incl. deletes).
git add -p Interactively stage hunks one at a time.
git diff Show unstaged changes.
git diff --staged Show staged-but-not-committed changes.
git restore <file> Discard unstaged changes to a file.
git restore --staged <file> Unstage a file (keep the changes).
git commit -m "msg" Commit staged changes with a message.
git commit -am "msg" Stage tracked files and commit in one step.
git commit --amend Replace the last commit with a new one (keep or change the message).
git commit --amend --no-edit Add staged changes to the last commit without editing the message.

Branches

CommandDescription
git branch List local branches.
git branch -a List local AND remote-tracking branches.
git branch <name> Create a new branch.
git branch -d <name> Delete a branch (only if merged).
git branch -D <name> Force-delete a branch.
git branch -m <new-name> Rename the current branch.
git switch <name> Switch to an existing branch.
git switch -c <name> Create and switch to a new branch.
git checkout <name> Older verb for switch — still works.
git merge <name> Merge <name> into the current branch.
git merge --no-ff <name> Force a merge commit even when fast-forward is possible.
git merge --abort Abort an in-progress merge.
git rebase <base> Re-apply current branch's commits on top of <base>.
git rebase -i <base> Interactive rebase — edit / squash / reorder commits.
git rebase --abort Abort an in-progress rebase.
git rebase --continue Continue rebasing after resolving conflicts.

Remotes & Sync

CommandDescription
git remote -v List remotes with their URLs.
git remote add <name> <url> Add a new remote.
git remote remove <name> Remove a remote.
git remote set-url <name> <url> Change a remote's URL.
git fetch Download remote changes without merging.
git fetch --all Fetch from every remote.
git pull Fetch and integrate using the selected fast-forward, merge, or rebase behavior.
git pull --rebase Fetch + rebase instead of merge — keeps history linear.
git push Push commits to the upstream branch.
git push -u origin <branch> Push and set upstream tracking, so later plain git push / git pull know where to go. The usual first push of a new branch.
git push --force-with-lease Permit a non-fast-forward update only when the remote ref matches the expected value. This does not certify that the rewrite preserves others' work.
git push origin --delete <branch> Delete a remote branch.

History & Inspection

CommandDescription
git log Show commit history.
git log --oneline Compact one-line-per-commit log.
git log --graph --oneline --all ASCII branch graph of every branch.
git log -p <file> Log including the diff for a specific file.
git log --since="2 weeks ago" Filter commits by date.
git log --author="Jane" Filter commits by author.
git log --grep="bug" Filter commits whose message matches a pattern.
git show <commit> Show a commit's message and diff.
git blame <file> Show who last modified each line.
git diff <a>..<b> Show changes between two commits or branches.
git reflog Show retained local reference updates. Entries expire; this is not a backup of uncommitted files.

Undo & Recovery

CommandDescription
git restore <file> Discard unstaged changes to <file>.
git restore --staged <file> Unstage <file> (keep working-tree changes).
git checkout -- <file> Older form of restore.
git reset HEAD~1 Move HEAD back one commit; keep the changes in your files, unstaged (same as --mixed).
git reset --soft HEAD~1 Move HEAD back one commit; keep changes staged.
git reset --mixed HEAD~1 Move HEAD back one commit; keep changes unstaged. (Default.)
git reset --hard HEAD~1 Move HEAD back one commit; discard changes. Destructive.
git revert <commit> Create a new commit that undoes <commit>. Safe — leaves history intact.
git clean -fd Remove untracked files and directories. Destructive.
git clean -fdn Dry run — show what would be cleaned without actually removing.

Stash

CommandDescription
git stash Save tracked-file changes; untracked and ignored files are excluded by default.
git stash -u Stash including untracked files.
git stash push -m "msg" Stash with a message.
git stash list List all stashes.
git stash pop Apply the stash; remove it only if application succeeds. Conflicts leave it in the stash list.
git stash apply Apply the most recent stash without removing it.
git stash apply stash@{1} Apply a specific stash by index.
git stash drop stash@{0} Delete a specific stash.
git stash clear Delete every stash.

Tags

CommandDescription
git tag List tags.
git tag <name> Create a lightweight tag.
git tag -a <name> -m "msg" Create an annotated tag (recommended for releases).
git push origin <tag> Push a single tag to remote.
git push --tags Push every tag to remote.
git tag -d <name> Delete a local tag.
git push origin --delete <tag> Delete a remote tag.

Git has a sprawling command surface. Most engineers use a small subset every day and learn the rest only when something goes wrong. The reference above is a cheat sheet for the everyday-set; this companion explains the categories and what’s worth knowing in each.

The everyday set

Start with these six commands:

CommandWhat it does
git statusWhat’s changed in the working tree, what’s staged
git add <file>Stage a file (or git add -p for hunk-by-hunk staging)
git commit -m "msg"Create a commit from staged changes
git pushSend commits to the remote
git pullFetch, then integrate according to flags and configuration (fast-forward, merge, or rebase)
git logShow commit history

These are stable, well-documented, and rarely surprising. Master these and you can do most day-to-day work.

Branching and switching

CommandWhat it does
git branchList branches
git branch <name>Create a branch (doesn’t switch to it)
git switch <name>Switch to an existing branch (modern alternative to checkout)
git switch -c <name>Create and switch to a new branch
git merge <branch>Merge another branch into the current one
git rebase <branch>Replay current branch’s commits on top of another
git branch -d <name>Delete a merged branch

git switch and git restore (added in Git 2.23) replaced the overloaded git checkout for most use cases. git checkout still works, but the modern split is cleaner: switch is for branches, restore is for files.

Inspection

CommandWhat it does
git diffShow unstaged changes
git diff --stagedShow staged-but-not-committed changes
git diff <commit>Show changes from a specific commit to working tree
git log --oneline -20Last 20 commits, one line each
git log --graph --all --onelineVisual graph of all branches
git blame <file>Show who last touched each line
git show <commit>Show what a specific commit changed

git log has dozens of options. Two flags worth memorizing: --oneline (compact display) and --graph (ASCII branch visualization).

Undoing things

The category most people learn under stress.

CommandWhat it does
git restore <file>Discard unstaged changes to a file
git restore --staged <file>Unstage (without discarding)
git reset HEAD~1Move HEAD back one commit, keep changes in working tree
git reset --hard HEAD~1Move HEAD back one commit, discard changes (destructive)
git revert <commit>Create a new commit that undoes an old one (safe, public)
git commit --amendReplace the most recent commit (only safe if not pushed)
git stashSave tracked-file changes; use -u to include untracked files
git stash popReapply the most recent stash; remove it only if application succeeds
git reflogShow retained local HEAD updates; entries expire

git reflog records local reference updates. It can help locate a commit after a reset or branch move, but it is not a backup of every commit or of unsaved working files. Entries expire; default expiry is 90 days, or 30 days for entries unreachable from the current tip. Preserve a found commit with a branch before further cleanup.

Remote handling

CommandWhat it does
git fetchDownload remote refs without merging
git pullFetch + integration; flags and configuration choose the method
git pushSend commits to remote
git push -u origin <branch>First push of a new branch (sets upstream)
git remote -vList remotes and their URLs
git remote add <name> <url>Add a remote (e.g., upstream for forks)

fetch is safer than pull: it shows you what’s on the remote without making changes locally. Pull only when you’ve already inspected.

When things go wrong

SymptomFirst-line response
”I committed to the wrong branch”First preserve the commit with git branch rescue-wrong-branch HEAD. Inspect whether it was pushed; with a clean working tree, switch to the intended branch and cherry-pick the commit. Coordinate any cleanup of shared history.
”I committed something I shouldn’t have”git revert <commit> (public history) or git reset (private)
“I lost a branch I needed”git reflog and check out the SHA
”Merge conflict I don’t understand”git status shows conflicted files; edit them, git add, git commit
”I want to abandon a merge in progress”git merge --abort
”I want to abandon a rebase in progress”git rebase --abort
”I rebased and pushed and now my coworker is angry”Stop pushing. Preserve both histories and coordinate recovery; another force-push is not a general repair.

Configuration that matters early

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main      # avoid the master/main rename later
git config --global pull.rebase false            # or "true" if you prefer rebase pulls
git config --global core.editor "code --wait"    # use VS Code as the commit editor

These are examples, not settings every project needs. Omit --global for repository-specific identity or behavior; follow the project’s collaboration rules.

Aliases worth setting

git config --global alias.co "checkout"
git config --global alias.br "branch"
git config --global alias.st "status"
git config --global alias.lg "log --graph --abbrev-commit --decorate --all --pretty=format:'%C(yellow)%h%Creset %C(cyan)%an%Creset %s %C(green)(%cr)%Creset'"

git lg produces a readable, colorful, decorated log graph. Worth memorizing.

Common pitfalls

  • git pull on a branch with uncommitted changes. Sometimes succeeds; sometimes refuses. Always check git status first; stash if needed.
  • Force-pushing shared history. --force-with-lease checks the remote ref against an expected value, but does not guarantee that your rewrite preserves others’ work. Without an explicit expected commit, background fetches can update the tracking ref and undermine that protection. Inspect and coordinate before rewriting shared history.
  • Forgetting to set upstream. git push -u origin <branch> explicitly sets tracking; configurations such as push.autoSetupRemote can do this automatically. Many git push errors mean “the upstream isn’t set.”
  • Merging vs rebasing public history. Rebase rewrites commit hashes. Rebasing commits that other people have already pulled creates a fork in their history. Don’t rebase shared branches without coordination.
  • git reset --hard without checking what’s about to be lost. It is destructive and silent. Preserve tracked and untracked work first; plain git stash does not include untracked files.
  • git checkout <file> to discard. Now git restore <file> — same effect, less ambiguity. Use the modern form.

For other developer references, see vim commands, HTTP status codes, regex cheat sheet.

Sources and scope

Checked September 27, 2026: git pull, reflog, stash, and push / force-with-lease. Recovery depends on what was actually committed or saved.