Last updated: September 2026  Β·  11 min read

Git Commands Cheat Sheet: The Complete 2026 Reference

Git's day-to-day commands are simple, add, commit, push, pull. The confusion shows up around the commands that undo or rewrite history, merge versus rebase, reset versus revert, where picking the wrong one can cause real problems on a shared branch.

This page covers the commands you'll use constantly, the two comparisons that trip everyone up, and a searchable lookup near the bottom.

Quick answer: use merge when working on a shared branch, it preserves history safely. Use rebase to clean up your own local commits before sharing them. Use revert on a shared branch to undo a commit safely, and reset only on local, unpushed commits.

Essential Git Commands

CommandWhat it does
git initCreates a new Git repository in the current folder
git clone [url]Copies an existing remote repository to your machine
git statusShows changed files and what's staged for commit
git add [file]Stages a file's changes for the next commit
git commit -m "message"Saves staged changes as a new commit
git pushUploads local commits to the remote repository
git pullDownloads and merges changes from the remote repository
git logShows the commit history

Branching Commands

CommandWhat it does
git branchLists local branches
git branch [name]Creates a new branch
git switch [branch]Switches to a different branch
git checkout -b [name]Creates and switches to a new branch in one step
git merge [branch]Merges the named branch into your current branch

Merge vs Rebase

Both combine changes from one branch into another, but they handle history differently. This matters more once you're collaborating with other people.

git merge

Combines two branches and creates a new merge commit that ties both histories together. Safe on shared branches since it never rewrites commits other people already have.

git rebase

Replays your commits on top of another branch, creating a cleaner, linear history. Rewrites commit history, which is risky if others have already pulled the commits you're rebasing.

Reset vs Revert

Both undo changes, but reset rewrites history while revert adds new history on top. This is the difference that actually matters for anything already pushed and shared.

git reset

Moves your branch pointer backward, effectively erasing commits from history. Safe on local, unpushed work. Risky on a shared branch, since it can wipe out commits others already have.

git revert

Creates a brand new commit that undoes the changes from a previous one, without deleting the original commit. Safe to use on shared branches, since history stays intact for everyone.

Undoing Changes: Full Comparison

SituationCommand
Undo uncommitted changes to a filegit checkout -- [file]
Unstage a file, keep the changesgit reset [file]
Undo the last local, unpushed commitgit reset --soft HEAD~1
Undo a commit that's already pushed and sharedgit revert [commit]
Temporarily save changes without committinggit stash
Restore stashed changesgit stash pop

Search Any Git Command

Type a command or keyword to filter the list below.

CommandWhat it does
git initCreates a new Git repository
git cloneCopies a remote repository locally
git statusShows changed and staged files
git addStages changes for commit
git commitSaves staged changes
git pushUploads commits to remote
git pullDownloads and merges remote changes
git branchLists or creates branches
git switchSwitches to another branch
git mergeCombines branch histories with a merge commit
git rebaseReplays commits on top of another branch
git resetMoves the branch pointer, can erase history
git revertUndoes a commit by adding a new one
git stashTemporarily saves uncommitted changes
git logShows commit history
git diffShows changes between commits or working files
git fetchDownloads remote changes without merging them
git remote -vLists connected remote repositories

No matching command found. Try a different word.

Common Git Workflows, Start to Finish

Individual commands make more sense in the context of the full sequence you'd actually run. Here are three real workflows, command by command.

Starting a feature branch and opening a PR

git checkout main
git pull origin main
git checkout -b feature/add-search-bar
# ... make your changes ...
git add .
git commit -m "Add search bar to header"
git push -u origin feature/add-search-bar
# open a pull request from the pushed branch

Undoing a commit you already pushed

git revert HEAD
git push origin feature/add-search-bar

git revert creates a new commit that undoes the changes, keeping history intact β€” the right choice once a commit has been pushed and others might already have it. Save git reset for commits that only exist on your own machine.

Resolving a merge conflict

git checkout main
git pull origin main
git checkout feature/add-search-bar
git merge main
# Git marks the conflicting lines with <<<<<<< ======= >>>>>>>
# open the file, keep the correct lines, delete the markers
git add .
git commit -m "Merge main into feature/add-search-bar"

The markers Git inserts show both versions of the conflicting section β€” your branch's version and the incoming version β€” separated by =======. Edit the file down to what it should actually say, remove all three marker lines, then stage and commit like normal.

Common Git Mistakes

Using git reset --hard on commits that have already been pushed and pulled by teammates is a fast way to cause real conflicts, since it erases history others are already building on. Revert is the safer tool once anything is shared.

Rebasing a public branch that others have already pulled from is the other classic mistake, it rewrites commit history, which forces everyone else to reconcile their own copy manually. Rebase your own local branches freely, but leave shared branches to merge.

A simple rule: if a commit only exists on your machine, reset and rebase freely. The moment it's pushed and someone else might have it, switch to revert and merge instead.

Want more developer references like this? Check our web tools collection, or our freelance tools page if you're managing client repos. Git config and CI files are usually YAML β€” see our JSON vs YAML vs XML guide if the syntax trips you up. See the official Git documentation for every command's full option list.

Frequently Asked Questions

Is rebase always more dangerous than merge?+

Not inherently, it's dangerous specifically when used on commits others already have. Rebasing your own local, unpushed commits to clean up history before sharing them is a common and safe practice.

How do I undo my last commit without losing my changes?

git reset --soft HEAD~1 undoes the commit but keeps your changes staged, ready to commit again with a different message or additional edits.

What's the difference between git fetch and git pull?

Fetch downloads the latest remote changes but doesn't merge them into your current branch, letting you review first. Pull does both steps at once, fetch and merge together.