Git Cheatsheet
Essential Git commands for daily development workflows — from init to advanced history rewriting.
Git is the industry-standard version control system. This cheatsheet covers everyday commands across the full Git workflow: creating and cloning repositories, staging and committing changes, branching and merging, collaborating with remotes, inspecting history, and undoing mistakes.
Commands are grouped by workflow stage so you can find what you need quickly.
Setup & Config
git config --global user.name "Name"git config --global user.email "email"git config --global init.defaultBranch maingit config --global alias.<name> <cmd>Repositories
git initgit clone <url>git clone --depth 1 <url>Staging & Committing
git statusgit add <file>git add .git add -pgit commit -m "<msg>"git commit -am "<msg>"git commit --amendBranching & Merging
git branchgit branch <name>git checkout -b <name>git switch <branch>git merge <branch>git rebase <branch>git branch -d <name>Inspecting & Comparing
git log --onelinegit log --graph --oneline --allgit diffgit diff --stagedgit show <commit>Undoing Changes
git stashgit stash popgit stash listgit reset HEAD <file>git reset --soft HEAD~1git reset --hard HEADgit revert <commit>Remote Collaboration
git remote -vgit remote add <name> <url>git pushgit push -u origin <branch>git push --force-with-leasegit pullgit fetchgit tag <name>git push --tagsGit Cheatsheet
Essential Git commands for daily development workflows — from init to advanced history rewriting.
Git is the industry-standard version control system. This cheatsheet covers everyday commands across the full Git workflow: creating and cloning repositories, staging and committing changes, branching and merging, collaborating with remotes, inspecting history, and undoing mistakes.
Commands are grouped by workflow stage so you can find what you need quickly.
Setup & Config
git config --global user.name "Name" — Set your Git username globally for all commits.git config --global user.email "email" — Set your Git email globally. Matches your GitHub/GitLab account.git config --global init.defaultBranch main — Set the default branch name to main for new repositories.git config --global alias.<name> <cmd> — Create a shorthand alias for a Git command.Repositories
git init — Initialize a new Git repository in the current directory.git clone <url> — Clone an existing remote repository to your local machine.git clone --depth 1 <url> — Shallow clone — only the latest commit history. Faster for large repos.Staging & Committing
git status — Show the current state of the working tree — staged, unstaged, and untracked files.git add <file> — Stage a specific file for the next commit.git add . — Stage all changes (new, modified, deleted) in the current directory.git add -p — Interactively stage chunks of a file. Great for partial commits.git commit -m "<msg>" — Commit staged changes with a descriptive message.git commit -am "<msg>" — Stage all tracked files and commit in one step. Skips untracked files.git commit --amend — Modify the most recent commit — change message or add forgotten files.Branching & Merging
git branch — List all local branches. Current branch is highlighted with an asterisk.git branch <name> — Create a new branch at the current commit.git checkout -b <name> — Create and immediately switch to a new branch.git switch <branch> — Switch to an existing branch (modern alternative to checkout).git merge <branch> — Merge the specified branch into the current branch.git rebase <branch> — Reapply commits from current branch onto the tip of another branch.git branch -d <name> — Delete a branch that has already been merged.Inspecting & Comparing
git log --oneline — Show commit history in compact format — one line per commit.git log --graph --oneline --all — Visualize branch topology with an ASCII graph.git diff — Show unstaged changes in the working tree compared to the index.git diff --staged — Show changes that are staged but not yet committed.git show <commit> — Display the full diff and metadata for a specific commit.Undoing Changes
git stash — Temporarily save uncommitted changes and clean the working directory.git stash pop — Restore the most recently stashed changes and remove them from the stash.git stash list — List all stashes in the stash stack.git reset HEAD <file> — Unstage a file without discarding its changes.git reset --soft HEAD~1 — Undo the last commit but keep changes staged.git reset --hard HEAD — Discard all uncommitted changes permanently. Use with caution.git revert <commit> — Create a new commit that undoes a specific commit. Safe for shared branches.Remote Collaboration
git remote -v — List all configured remote repositories with their URLs.git remote add <name> <url> — Add a new remote repository.git push — Push commits from the current branch to its remote tracking branch.git push -u origin <branch> — Push a new branch to remote and set up tracking.git push --force-with-lease — Force push safely — only if your remote-tracking ref matches. Safer than --force.git pull — Fetch and merge changes from the remote tracking branch.git fetch — Download commits and refs from remote without merging.git tag <name> — Create an annotated tag at the current commit, typically for releases.git push --tags — Push all local tags to the remote repository.