Git: essential commands
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).
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 repoThe 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.
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 commitsTip: 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.
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 branchInspect
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).
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 linePitfalls
The mistakes that keep coming back:
git pullwith pending changes → conflict or refusal. Commit orgit stashfirst.- Committing on
mainout of habit → makegit switch -ca reflex before coding. - ⚠️
git checkout -- <file>/git restore <file>overwrites your uncommitted changes, with no way back. Check withgit difffirst. - ⚠️
git clean -fdpermanently deletes untracked files. Always rungit clean -nd(dry-run) first. - Committed secrets (
.env, keys) → a proper.gitignorefrom day one is far cheaper than a history purge.
To cleanly undo a mistake, see the git-annuler sheet.
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