Git Command Reference
A quick lookup for Git commands grouped by workflow. Each entry covers what the command does, its most-used flags, and a concise example.
Setup & configuration
Section titled “Setup & configuration”Run these once when setting up a new machine or repository.
| Command | What it does |
|---|---|
git config --global user.name "Name" | Set your name for all commits on this machine |
git config --global user.email "email" | Set your email for all commits on this machine |
git config --list | Show all current config values |
git init | Initialize a new Git repository in the current directory |
git clone <url> | Clone a remote repository to your local machine |
git clone <url> <dir> | Clone into a specific directory name |
# First-time machine setupgit config --global user.name "Alex Rivera"git config --global user.email "alex@example.com"
# Start a new projectgit init
# Copy an existing repogit clone https://github.com/user/repo.gitgit clone https://github.com/user/repo.git my-folderStaging & committing
Section titled “Staging & committing”The core daily workflow — check status, stage changes, and record a commit.
| Command | What it does | Key flags |
|---|---|---|
git status | Show working tree status — untracked, modified, staged files | -s short format |
git add <file> | Stage a specific file | — |
git add . | Stage all changes in the current directory | — |
git add -p | Interactively stage chunks of changes | — |
git commit -m "message" | Commit staged changes with a message | — |
git commit --amend | Rewrite the most recent commit (message or content) | Only amend unpushed commits |
git diff | Show unstaged changes | --staged to see staged changes |
# Check what's changedgit status
# Stage specific files or everythinggit add index.html styles.cssgit add .
# Review staged diff before committinggit diff --staged
# Commitgit commit -m "feat: add contact form to homepage"
# Fix the last commit message (before pushing)git commit --amend -m "feat: add contact form with validation"History & inspection
Section titled “History & inspection”Browse and search the commit history.
| Command | What it does | Key flags |
|---|---|---|
git log | Show commit history | --oneline, --graph, --all, -n <number> |
git log --oneline | Compact one-line format | — |
git log --oneline --graph --all | Visual branch graph | — |
git show <commit> | Show changes introduced by a specific commit | — |
git diff <branch1>..<branch2> | Compare two branches | — |
git diff <commit> | Show changes since a specific commit | — |
git blame <file> | Show who last changed each line of a file | — |
# Compact historygit log --oneline
# Visual branch graphgit log --oneline --graph --all
# See last 5 commitsgit log -n 5 --oneline
# Inspect a specific commitgit show a3f2c1d
# Compare branchesgit diff main..feat/new-navBranching & merging
Section titled “Branching & merging”Create, switch, and integrate branches.
| Command | What it does | Key flags |
|---|---|---|
git branch | List local branches | -a all (including remote), -d delete |
git branch <name> | Create a new branch | — |
git branch -d <name> | Delete a branch (safe — refuses if unmerged) | -D force delete |
git switch <name> | Switch to an existing branch | -c <name> create and switch |
git switch -c <name> | Create and switch to a new branch | — |
git checkout <name> | Switch to a branch (older syntax) | -b <name> create and switch |
git merge <branch> | Merge a branch into the current branch | --no-ff preserve merge commit |
git rebase <branch> | Reapply commits on top of another branch | Avoid on shared branches |
# Create and switch in one stepgit switch -c feat/user-profile
# List all branchesgit branch -a
# Merge a feature branch into maingit switch maingit merge feat/user-profile
# Delete the branch after merginggit branch -d feat/user-profilePrefer git switch over git checkout for branch operations — it has a cleaner interface and was introduced specifically for switching branches.
Undoing changes
Section titled “Undoing changes”Undo mistakes at different stages of the workflow.
| Command | What it does | Safe? |
|---|---|---|
git restore <file> | Discard unstaged changes in a file | Safe — only affects working tree |
git restore --staged <file> | Unstage a file (keep changes in working tree) | Safe |
git revert <commit> | Create a new commit that undoes a previous commit | Safe — preserves history |
git reset --soft HEAD~1 | Undo the last commit, keep changes staged | Safe if not pushed |
git reset --mixed HEAD~1 | Undo the last commit, keep changes unstaged | Safe if not pushed |
git reset --hard HEAD~1 | Undo the last commit and discard all changes | Destructive — data loss |
git reset --hard <commit> | Reset to a specific commit, discard everything after | Destructive — data loss |
# Discard changes to a file you haven't staged yetgit restore index.html
# Unstage a file (undo git add)git restore --staged index.html
# Undo the last commit but keep the changesgit reset --soft HEAD~1
# Safely undo a pushed commit by creating a new onegit revert a3f2c1dUse git revert for commits that have already been pushed — it is safe for shared branches because it adds a new commit rather than rewriting history. Never use git reset --hard on pushed commits without coordinating with your team.
Remote operations
Section titled “Remote operations”Work with remote repositories, sync changes, and manage tags and stashes.
Remotes
Section titled “Remotes”| Command | What it does | Key flags |
|---|---|---|
git remote -v | List remotes with their URLs | — |
git remote add <name> <url> | Add a remote | — |
git remote remove <name> | Remove a remote | — |
git fetch | Download remote changes without merging | --all fetch all remotes |
git pull | Fetch and merge remote changes into current branch | --rebase rebase instead of merge |
git push | Push current branch to its remote tracking branch | -u origin <branch> set upstream |
git push -u origin <branch> | Push and set the upstream tracking branch | — |
git push --force-with-lease | Force push only if no one else has pushed | Safer than --force |
# Connect a local repo to GitHubgit remote add origin https://github.com/user/repo.git
# Push for the first time and set upstreamgit push -u origin main
# Get remote changes without merginggit fetch
# Fetch and merge in one stepgit pullNever use git push --force on a shared branch. Use --force-with-lease instead — it refuses to push if someone else has pushed commits you haven’t fetched yet.
| Command | What it does |
|---|---|
git stash | Save uncommitted changes and clean the working tree |
git stash push -m "message" | Save with a descriptive label |
git stash list | List all stashes |
git stash pop | Apply the most recent stash and remove it |
git stash apply stash@{n} | Apply a specific stash without removing it |
git stash drop stash@{n} | Delete a specific stash |
# Stash work-in-progress to switch branches quicklygit stash push -m "wip: half-finished nav styles"
# Come back latergit stash pop| Command | What it does |
|---|---|
git tag | List all tags |
git tag <name> | Create a lightweight tag at HEAD |
git tag -a <name> -m "message" | Create an annotated tag with a message |
git push origin <tag> | Push a single tag to the remote |
git push origin --tags | Push all tags to the remote |
# Tag a releasegit tag -a v1.0.0 -m "Initial public release"git push origin v1.0.0