A compact Git reference for everyday work: create branches, sync with remotes, undo mistakes safely, and inspect history—without memorizing the full manual.

Configure identity (once per machine)

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main

Start or clone

git init
git clone https://github.com/org/repo.git
cd repo

Branch and switch

git branch                     # list local branches
git switch -c feature/login  # create and switch (Git 2.23+)
git switch main                # back to main
git branch -d feature/login    # delete merged branch

Stage, commit, push

git status
git add -p                     # stage interactively (best for reviews)
git commit -m "Describe change"
git push -u origin feature/login

Sync with remote

git fetch origin
git pull --rebase origin main # linear history on feature branch
git push

Inspect history

git log --oneline -n 20
git log --graph --oneline --all -n 30
git show HEAD
git blame -L 10,40 path/to/file.php

Undo and fix-ups

git restore path/to/file       # discard unstaged changes (tracked file)
git restore --staged path      # unstage
git commit --amend             # add to last commit (not yet pushed shared branch)
git reset --soft HEAD~1        # undo last commit, keep changes staged

Rule of thumb: avoid rewriting history on branches others have pulled—use revert for shared mainline fixes.

git revert <commit-sha>

Stash when context-switching

git stash push -m "wip"
git stash list
git stash pop

Tags (releases)

git tag -a v1.2.0 -m "Release 1.2.0"
git push origin v1.2.0

Quick safety nets

  • git reflog — find “lost” commits after a bad reset.
  • git clean -fd — remove untracked files/dirs (destructive; check with git clean -fdn first).
  • Before force-pushing: git push --force-with-lease instead of --force when you must rewrite remote history.

Print or bookmark this page; pair it with your team’s branching model (Git Flow, trunk-based, or PR-only) for the full workflow.