Git: essential commands

updated 2026-07-01#git#cli#cheatsheet

Getting started

Before your first commit, tell Git who you are — that info is baked into every commit. Then there are two ways to get a repo: create one (init) or grab an existing one (clone).

shell
git config --global user.name "Ton Nom"
git config --global user.email "toi@example.com"

git init                # nouveau dépôt dans le dossier courant / new repo in current dir
git clone <url>         # copie un dépôt distant / copy a remote repo

The daily cycle

90% of your life with Git fits in five commands. The habit to build: git status before and after every operation — it's your compass, it always tells you where you stand and often what to do next.

shell
git status                     # où j'en suis ? / where am I?
git add <fichier>              # stage un fichier / stage a file
git add -p                     # stage morceau par morceau / stage hunk by hunk
git commit -m "message clair"  # enregistre le snapshot / record the snapshot
git pull                       # récupère + intègre le distant / fetch + integrate remote
git push                       # publie tes commits / publish your commits

Tip: git add -p makes you re-read each change before staging it. It's the best way to craft clean commits and catch a forgotten console.log.

Branches

A branch costs nothing: create one per feature or fix. To bring its content back, merge combines histories (faithful log), rebase replays your commits on top of the target (linear log) — just remember: never rebase commits that are already pushed and shared.

shell
git switch -c ma-feature   # crée et bascule / create and switch
git switch main            # revient sur main / back to main
git branch                 # liste les branches / list branches
git merge ma-feature       # fusionne dans la branche courante / merge into current branch
git rebase main            # rejoue tes commits sur main / replay your commits onto main
git branch -d ma-feature   # supprime une branche fusionnée / delete a merged branch

Inspect

Before committing, merging or debugging: look. diff for pending changes, log for history, show for a specific commit, blame to find which commit touched which line (and read its message — not to point fingers).

shell
git log --oneline --graph --all   # historique compact et visuel / compact visual history
git diff                          # modifs non stagées / unstaged changes
git diff --staged                 # modifs stagées / staged changes
git show <commit>                 # détail d'un commit / one commit in detail
git blame <fichier>               # qui a modifié quelle ligne / who changed which line

Pitfalls

The mistakes that keep coming back:

  • git pull with pending changes → conflict or refusal. Commit or git stash first.
  • Committing on main out of habit → make git switch -c a reflex before coding.
  • ⚠️ git checkout -- <file> / git restore <file> overwrites your uncommitted changes, with no way back. Check with git diff first.
  • ⚠️ git clean -fd permanently deletes untracked files. Always run git clean -nd (dry-run) first.
  • Committed secrets (.env, keys) → a proper .gitignore from day one is far cheaper than a history purge.

To cleanly undo a mistake, see the git-annuler sheet.

shell
git stash          # met de côté tes modifs en cours / shelve pending changes
git stash pop      # les récupère / bring them back
git clean -nd      # dry-run : montre ce qui serait supprimé / shows what would be deleted