🌿 Git
Commandes essentielles pour la gestion de versions, branches, et collaboration.
Git est le système de contrôle de version le plus utilisé. Maîtriser ses commandes permet de gérer efficacement l’historique du code et le travail en équipe.
Configuration
Configuration initiale
# Identité (obligatoire)
git config --global user.name "Mon Nom"
git config --global user.email "mon@email.com"
# Éditeur par défaut
git config --global core.editor "vim"
git config --global core.editor "code --wait" # VSCode
# Branche par défaut
git config --global init.defaultBranch main
# Afficher la config
git config --list
git config --global --listAlias utiles
# Créer des alias
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.lg "log --oneline --graph --all"
# Utilisation
git st # équivalent à git status
git lg # log joliBases
Initialiser un repo
# Nouveau projet
git init
# Cloner un repo existant
git clone https://github.com/user/repo.git
# Cloner dans un dossier spécifique
git clone https://github.com/user/repo.git mon-dossier
# Cloner une branche spécifique
git clone -b develop https://github.com/user/repo.git
# Clone superficiel (sans historique complet)
git clone --depth 1 https://github.com/user/repo.gitWorkflow de base
# Voir le statut
git status
git status -s # format court
# Ajouter des fichiers
git add fichier.txt
git add . # tous les fichiers
git add -A # tout (y compris suppressions)
git add -p # mode interactif (patch)
# Commiter
git commit -m "Message du commit"
git commit -am "Message" # add + commit (fichiers trackés)
git commit --amend # modifier le dernier commit
git commit --amend --no-edit # amend sans changer le message
# Pousser
git push
git push origin main
git push -u origin main # définir l'upstream
git push --force-with-lease # force push sécuriséBranches
Gérer les branches
# Lister les branches
git branch # locales
git branch -r # remote
git branch -a # toutes
# Créer une branche
git branch ma-feature
# Créer et basculer
git checkout -b ma-feature
git switch -c ma-feature # Git 2.23+
# Basculer de branche
git checkout main
git switch main # Git 2.23+
# Renommer une branche
git branch -m ancien-nom nouveau-nom
git branch -m nouveau-nom # branche courante
# Supprimer une branche
git branch -d ma-feature # si mergée
git branch -D ma-feature # forcer
# Supprimer branche remote
git push origin --delete ma-featureMerge
# Merger une branche dans la branche courante
git merge ma-feature
# Merge sans fast-forward (garde l'historique de branche)
git merge --no-ff ma-feature
# Annuler un merge en cours
git merge --abortRebase
Ne jamais rebaser des commits déjà poussés sur une branche partagée !
# Rebaser la branche courante sur main
git rebase main
# Rebase interactif (réorganiser, squash, edit)
git rebase -i HEAD~3 # 3 derniers commits
git rebase -i main # depuis main
# Continuer après résolution de conflits
git rebase --continue
# Annuler un rebase en cours
git rebase --abortRebase interactif - commandes
# Dans l'éditeur du rebase interactif :
# pick = garder le commit tel quel
# reword = changer le message
# edit = arrêter pour modifier
# squash = fusionner avec le commit précédent
# fixup = comme squash mais ignore le message
# drop = supprimer le commitHistorique
Consulter l’historique
# Log basique
git log
# Log compact
git log --oneline
# Log avec graphe
git log --oneline --graph --all
# Log avec diff
git log -p
# Log d'un fichier
git log -- fichier.txt
git log -p -- fichier.txt
# Rechercher dans les commits
git log --grep="bug fix"
git log -S "function_name" # cherche dans le codeVoir les différences
# Différences non stagées
git diff
# Différences stagées
git diff --staged
git diff --cached
# Différence entre branches
git diff main..feature
# Différence d'un fichier
git diff fichier.txt
# Stats des changements
git diff --statBlame et show
# Qui a modifié chaque ligne
git blame fichier.txt
git blame -L 10,20 fichier.txt # lignes 10 à 20
# Afficher un commit
git show abc1234
git show HEAD
git show HEAD~2 # 2 commits avant HEADAnnuler des changements
Avant le commit
# Annuler les modifs d'un fichier (non stagé)
git checkout -- fichier.txt
git restore fichier.txt # Git 2.23+
# Déstageer un fichier
git reset HEAD fichier.txt
git restore --staged fichier.txt # Git 2.23+
# Annuler toutes les modifs locales
git checkout -- .
git restore .Après le commit
# Annuler le dernier commit (garde les changements)
git reset --soft HEAD~1
# Annuler le dernier commit (perd les changements stagés)
git reset --mixed HEAD~1 # défaut
# Annuler le dernier commit (perd tout)
git reset --hard HEAD~1
# Créer un commit inverse (safe pour branches partagées)
git revert abc1234
git revert HEADStash
Le stash permet de sauvegarder temporairement des modifications non commitées.
# Stasher les modifications
git stash
git stash push -m "description"
# Inclure les fichiers non trackés
git stash -u
# Lister les stash
git stash list
# Appliquer le dernier stash
git stash pop # applique et supprime
git stash apply # applique sans supprimer
# Appliquer un stash spécifique
git stash pop stash@{2}
# Voir le contenu d'un stash
git stash show -p stash@{0}
# Supprimer des stash
git stash drop stash@{0}
git stash clear # tousCherry-pick
# Appliquer un commit spécifique sur la branche courante
git cherry-pick abc1234
# Cherry-pick sans commiter
git cherry-pick -n abc1234
# Cherry-pick plusieurs commits
git cherry-pick abc1234 def5678
# Cherry-pick une plage de commits
git cherry-pick abc1234..xyz9999Remote
Gérer les remotes
# Lister les remotes
git remote -v
# Ajouter un remote
git remote add origin https://github.com/user/repo.git
git remote add upstream https://github.com/original/repo.git
# Changer l'URL d'un remote
git remote set-url origin git@github.com:user/repo.git
# Supprimer un remote
git remote remove origin
# Renommer un remote
git remote rename origin old-originSynchroniser
# Récupérer les changements (sans merger)
git fetch
git fetch origin
git fetch --all
# Récupérer et merger
git pull
git pull origin main
# Pull avec rebase (évite les merge commits)
git pull --rebase
# Pousser
git push
git push origin main
# Pousser une nouvelle branche
git push -u origin ma-feature
# Pousser toutes les branches
git push --all
# Pousser les tags
git push --tagsTags
# Lister les tags
git tag
git tag -l "v1.*"
# Créer un tag léger
git tag v1.0.0
# Créer un tag annoté
git tag -a v1.0.0 -m "Version 1.0.0"
# Taguer un ancien commit
git tag -a v1.0.0 abc1234 -m "Version 1.0.0"
# Pousser un tag
git push origin v1.0.0
# Pousser tous les tags
git push --tags
# Supprimer un tag local
git tag -d v1.0.0
# Supprimer un tag remote
git push origin --delete v1.0.0Nettoyage
# Supprimer les fichiers non trackés (dry run)
git clean -n
# Supprimer les fichiers non trackés
git clean -f
# Supprimer fichiers et dossiers non trackés
git clean -fd
# Supprimer les branches mergées
git branch --merged | grep -v main | xargs git branch -d
# Nettoyer les références remote obsolètes
git remote prune origin
git fetch --pruneCas d’usage courants
Fichier .gitignore
# Patterns courants
# Fichiers spécifiques
secret.txt
# Extensions
*.log
*.tmp
# Dossiers
node_modules/
dist/
.cache/
# Fichiers systèmes
.DS_Store
Thumbs.db
# Environnement
.env
.env.local
# IDE
.idea/
.vscode/
*.swp
# Négation (ne pas ignorer)
!important.logIgnorer des fichiers déjà trackés
# Retirer du tracking sans supprimer le fichier
git rm --cached fichier.txt
git rm -r --cached dossier/
# Puis ajouter au .gitignore et commiter