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.

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 it works

Git tracks three places: the working tree (your files), the index/staging area (what the next commit will contain) and the commit history. 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.

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.