Git est un système de gestion de versions distribué qui permet de suivre et contrôler les modifications du code source.
⚙️ Configuration de base
| 📌 Action | 🧠 Commande |
|---|
| 👤 Configurer nom d’utilisateur | git config --global user.name "Jean Dupont" |
| 📧 Configurer email | git config --global user.email "jean@mail" |
| 📋 Voir la configuration | git config --list |
| 🏗️ Initialiser un dépôt | git init |
| 📂 Cloner un dépôt distant | git clone https://github.com/user/repo.git |
📄 Gestion des fichiers
| 📌 Action | 🧠 Commande |
|---|
| 📊 Voir le statut des fichiers | git status |
| ➕ Ajouter un fichier | git add <fichier> |
| ➕ Ajouter tous les fichiers | git add . |
| ➖ Retirer du staging | git reset <fichier> |
| 🗑️ Supprimer un fichier | git rm <fichier> |
| 📝 Voir les différences | git diff |
| 👁️ Voir les fichiers stagés | git diff --cached |
💾 Commits et historique
| 📌 Action | 🧠 Commande |
|---|
| 💾 Commiter avec message | git commit -m "Message" |
| ⚡ Add + commit en une fois | git commit -am "Message" |
| ✏️ Modifier le dernier commit | git commit --amend |
| 📜 Voir l’historique | git log |
| 📋 Historique condensé | git log --oneline |
| 🌳 Historique graphique | git log --graph --oneline |
| 🔍 Voir un commit précis | git show <commit-id> |
🌿 Gestion des branches
| 📌 Action | 🧠 Commande |
|---|
| 📋 Lister les branches | git branch |
| 🆕 Créer une branche | git branch <nom-branche> |
| 🔄 Changer de branche | git checkout <nom-branche> |
| ⚡ Créer et changer | git checkout -b <nom-branche> |
| 🔄 Changer (moderne) | git switch <nom-branche> |
| ⚡ Créer et changer (moderne) | git switch -c <nom-branche> |
| 🗑️ Supprimer une branche | git branch -d <nom-branche> |
| 🔗 Fusionner une branche | git merge <nom-branche> |
🌐 Dépôts distants
| 📌 Action | 🧠 Commande |
|---|
| 📋 Lister les remotes | git remote -v |
| 🔗 Ajouter un remote | git remote add origin <url> |
| ⬆️ Pousser vers le remote | git push origin <branche> |
| ⬇️ Récupérer du remote | git pull origin <branche> |
| 📥 Télécharger sans merger | git fetch origin |
| 🏷️ Pousser les tags | git push --tags |
| 🆕 Pousser nouvelle branche | git push -u origin <branche> |
🛠️ Dépannage et annulations
| 📌 Action | 🧠 Commande |
|---|
| ↩️ Annuler les modifications | git checkout -- <fichier> |
| 🔙 Revenir au dernier commit | git reset --hard HEAD |
| ⏪ Revenir à un commit précis | git reset --hard <commit-id> |
| 📦 Sauvegarder temporairement | git stash |
| 📤 Restaurer le stash | git stash pop |
| 📋 Lister les stash | git stash list |
| 🔍 Chercher dans l’historique | git log --grep="motclé" |
| 🧹 Nettoyer les fichiers | git clean -fd |
📝 Fichier .gitignore
Le .gitignore permet d’exclure des fichiers du versioning
Exemple basique
# Fichiers système
.DS_Store
Thumbs.db
# Logs
*.log
logs/
# Dépendances
node_modules/
vendor/
# Configuration locale
.env
config.local.json
Patterns utiles
*.ext - Tous les fichiers avec extension
dossier/ - Tout un dossier
!exception.ext - Exception (ne pas ignorer)
**/cache - Dossier cache partout
🔄 Workflow
Workflow simple
# 1. Cloner ou initialiser
git clone https://github.com/user/repo.git
cd repo
# 2. Créer une branche
git checkout -b ma-feature
# 3. Travailler et commiter
git add .
git commit -m "Ajout de ma feature"
# 4. Pousser la branche
git push -u origin ma-feature
# 5. Fusionner (après review)
git checkout main
git merge ma-feature
git push origin main
États des fichiers
Working Directory → Staging Area → Repository → Remote
(modifié) add (staged) commit (commité) push (distant)