Git Command Reference

Essential git commands with flags and short descriptions.

Searchable reference of the most-used git commands grouped by area — staging, committing, branching, merging, rebasing, remotes and undo — each with common flags and a clear purpose. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

What is the difference between git merge and git rebase?

Merge joins two branches with a merge commit, preserving the exact history of both. Rebase replays your commits on top of another branch, producing a linear history but rewriting commit hashes. Use merge to integrate shared branches and rebase to tidy a local branch before sharing — never rebase commits others have already pulled.

The git commands you actually use, organised

Git has hundreds of commands, but day-to-day work runs on a couple dozen. This reference groups the essentials by area — setup, staging, committing, branching, integrating, remotes and undo — with the common flags and a one-line description for each, so you can find the right command and its key options fast.

How git’s three-location model works

Git tracks three places: the working tree (your files on disk), the index (also called the staging area — what the next commit will contain), and the commit history (the chain of snapshots). Most commands move changes between these:

git add -p              # stage selected hunks into the index
git commit -m "message" # record the index as a commit
git switch -c feature   # create and move onto a new branch
git rebase main         # replay feature commits onto the latest main
git push -u origin feature --force-with-lease

Remote commands (fetch, pull, push) sync that history with a server, and undo commands (reset, revert, restore, reflog) walk it back when something goes wrong.

Command areas at a glance

Staging and committing

CommandWhat it does
git add -pInteractively stage hunks — the best way to make clean commits
git commit -m "msg"Record staged changes as a new commit
git commit --amendRewrite the most recent commit (local only — never on shared branches)
git stash push -m "desc"Save uncommitted work to a temporary stack entry
git stash popRestore the most recent stash entry

Branching and navigating

CommandWhat it does
git switch -c featureCreate and move to a new branch
git switch mainMove to an existing branch
git branch -d featureDelete a merged branch locally
git log --oneline --graphVisualise the commit graph compactly

Integrating changes

CommandWhat it does
git merge featureJoin feature into the current branch with a merge commit
git rebase mainReplay current-branch commits on top of main (rewrites hashes)
git cherry-pick <sha>Apply a single commit from another branch

Remotes and sharing

CommandWhat it does
git fetch originDownload updates without merging
git pull --rebaseFetch and replay local commits on top of the remote
git push -u origin featurePush and set upstream tracking
git push --force-with-leaseForce-push safely — aborts if remote has changed

Undo and recovery

CommandWhat it does
git revert <sha>Create a new commit that reverses a prior one — safe on shared branches
git reset --soft HEAD~1Undo last commit but keep changes staged
git restore <file>Discard unstaged changes to a file
git reflogList every HEAD position — your recovery net for “lost” commits

Tips and notes

  • Stage precisely with git add -p so each commit is a coherent, reviewable change.
  • Prefer git revert on shared branches; reserve git reset --hard for local cleanup you are sure about.
  • Use git push --force-with-lease, never bare --force, after rebasing.
  • Keep git reflog in mind — it is your safety net for recovering commits that a reset or rebase appeared to delete. Reflog entries persist for weeks by default.
  • git switch and git restore are the modern, focused replacements for the overloaded git checkout command — prefer them in new scripts and workflows.