Skip to content

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.

Run these once when setting up a new machine or repository.

CommandWhat 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 --listShow all current config values
git initInitialize 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
Terminal window
# First-time machine setup
git config --global user.name "Alex Rivera"
git config --global user.email "alex@example.com"
# Start a new project
git init
# Copy an existing repo
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git my-folder

The core daily workflow — check status, stage changes, and record a commit.

CommandWhat it doesKey flags
git statusShow 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 -pInteractively stage chunks of changes
git commit -m "message"Commit staged changes with a message
git commit --amendRewrite the most recent commit (message or content)Only amend unpushed commits
git diffShow unstaged changes--staged to see staged changes
Terminal window
# Check what's changed
git status
# Stage specific files or everything
git add index.html styles.css
git add .
# Review staged diff before committing
git diff --staged
# Commit
git 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"

Browse and search the commit history.

CommandWhat it doesKey flags
git logShow commit history--oneline, --graph, --all, -n <number>
git log --onelineCompact one-line format
git log --oneline --graph --allVisual 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
Terminal window
# Compact history
git log --oneline
# Visual branch graph
git log --oneline --graph --all
# See last 5 commits
git log -n 5 --oneline
# Inspect a specific commit
git show a3f2c1d
# Compare branches
git diff main..feat/new-nav

Create, switch, and integrate branches.

CommandWhat it doesKey flags
git branchList 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 branchAvoid on shared branches
Terminal window
# Create and switch in one step
git switch -c feat/user-profile
# List all branches
git branch -a
# Merge a feature branch into main
git switch main
git merge feat/user-profile
# Delete the branch after merging
git branch -d feat/user-profile

Prefer git switch over git checkout for branch operations — it has a cleaner interface and was introduced specifically for switching branches.

Undo mistakes at different stages of the workflow.

CommandWhat it doesSafe?
git restore <file>Discard unstaged changes in a fileSafe — 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 commitSafe — preserves history
git reset --soft HEAD~1Undo the last commit, keep changes stagedSafe if not pushed
git reset --mixed HEAD~1Undo the last commit, keep changes unstagedSafe if not pushed
git reset --hard HEAD~1Undo the last commit and discard all changesDestructive — data loss
git reset --hard <commit>Reset to a specific commit, discard everything afterDestructive — data loss
Terminal window
# Discard changes to a file you haven't staged yet
git restore index.html
# Unstage a file (undo git add)
git restore --staged index.html
# Undo the last commit but keep the changes
git reset --soft HEAD~1
# Safely undo a pushed commit by creating a new one
git revert a3f2c1d

Use 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.

Work with remote repositories, sync changes, and manage tags and stashes.

CommandWhat it doesKey flags
git remote -vList remotes with their URLs
git remote add <name> <url>Add a remote
git remote remove <name>Remove a remote
git fetchDownload remote changes without merging--all fetch all remotes
git pullFetch and merge remote changes into current branch--rebase rebase instead of merge
git pushPush 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-leaseForce push only if no one else has pushedSafer than --force
Terminal window
# Connect a local repo to GitHub
git remote add origin https://github.com/user/repo.git
# Push for the first time and set upstream
git push -u origin main
# Get remote changes without merging
git fetch
# Fetch and merge in one step
git pull

Never 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.

CommandWhat it does
git stashSave uncommitted changes and clean the working tree
git stash push -m "message"Save with a descriptive label
git stash listList all stashes
git stash popApply 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
Terminal window
# Stash work-in-progress to switch branches quickly
git stash push -m "wip: half-finished nav styles"
# Come back later
git stash pop
CommandWhat it does
git tagList 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 --tagsPush all tags to the remote
Terminal window
# Tag a release
git tag -a v1.0.0 -m "Initial public release"
git push origin v1.0.0