Configuration
$git config --global user.name 'Mouctar Barry'
$git config --global user.email '[email protected]'
$git config --global init.defaultBranch main
$git config --list
Afficher toute la configuration
Workflow quotidien
$git status
Voir les fichiers modifiés, stagés et non-trackés
$git add fichier.ts
Stager un fichier spécifique
$git add -p
Stager interactivement (par hunk)
$git commit -m 'feat(api): add user authentication'
$git push origin main
$git pull --rebase origin main
Pull en rebasant (évite les merge commits)
Branches
$git branch
Lister les branches locales
$git branch -a
Lister toutes les branches (locales + remote)
$git checkout -b feat/new-feature
Créer et switcher sur une nouvelle branche
$git switch feat/new-feature
Syntaxe moderne pour changer de branche
$git switch -c feat/new-feature
Créer et switcher (syntaxe moderne)
$git branch -d feat/old-feature
Supprimer une branche mergée
$git push origin --delete feat/old-feature
Supprimer une branche remote
Merge vs Rebase
Merge
main: A --- B --- C --- M
\ /
feature: D --- E
$git checkout main && git merge feat/new-feature
Rebase
Avant: main: A --- B --- C
feature: D --- E
Apres: main: A --- B --- C
feature: D' --- E'
$git checkout feat/new-feature && git rebase main
Regle d'or : ne jamais rebase une branche publique/partagée.
Stash
$git stash
Sauvegarder les modifications en cours
$git stash push -m 'wip: auth flow'
Stash avec un message descriptif
$git stash list
stash@{0}: On feat/auth: wip: auth flow
stash@{1}: WIP on main: abc1234 fix typo$git stash pop
Restaurer et supprimer le dernier stash
$git stash apply stash@{1}
Restaurer un stash spécifique sans le supprimer
$git stash drop stash@{0}
Supprimer un stash
Historique
$git log --oneline -10
Les 10 derniers commits, format compact
$git log --oneline --graph --all
Visualiser l'arbre des branches
$git log --author='Mouctar' --since='1 week ago'
$git diff
Voir les modifications non stagées
$git diff --staged
Voir les modifications stagées
$git diff main..feat/new-feature
Comparer deux branches
$git blame fichier.ts
Voir qui a modifié chaque ligne
$git show abc1234
Details d'un commit spécifique
Annuler des changements
$git restore fichier.ts
Annuler les modifications non stagées d'un fichier
$git restore --staged fichier.ts
Unstage un fichier (garder les modifications)
$git revert abc1234
Créer un nouveau commit qui annule un commit
$git reset --soft HEAD~1
Annuler le dernier commit, garder les changements stagés
$git reset --mixed HEAD~1
Annuler le dernier commit, garder les changements non stagés
Tags
$git tag v1.0.0
Tag léger
$git tag -a v1.0.0 -m 'Release 1.0.0'
Tag annoté (recommandé)
$git push origin v1.0.0
$git push origin --tags
Pusher tous les tags
Cherry-pick
$git cherry-pick abc1234
Appliquer un commit spécifique sur la branche courante
$git cherry-pick abc1234 def5678
Cherry-pick de plusieurs commits
Bonnes pratiques
- Conventional Commits :
type(scope): description - Branches courtes : merger rapidement, éviter les branches longues
- Pull --rebase : garder un historique linéaire
- Ne jamais force push sur main
- .gitignore : ignorer
node_modules/,.env,dist/,.next/