skip to content

Recherche

Git

Commandes Git essentielles et workflow !

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’utilisateurgit config --global user.name "Jean Dupont"
📧 Configurer emailgit config --global user.email "jean@mail"
📋 Voir la configurationgit config --list
🏗️ Initialiser un dépôtgit init
📂 Cloner un dépôt distantgit clone https://github.com/user/repo.git

📄 Gestion des fichiers

📌 Action🧠 Commande
📊 Voir le statut des fichiersgit status
➕ Ajouter un fichiergit add <fichier>
➕ Ajouter tous les fichiersgit add .
➖ Retirer du staginggit reset <fichier>
🗑️ Supprimer un fichiergit rm <fichier>
📝 Voir les différencesgit diff
👁️ Voir les fichiers stagésgit diff --cached

💾 Commits et historique

📌 Action🧠 Commande
💾 Commiter avec messagegit commit -m "Message"
⚡ Add + commit en une foisgit commit -am "Message"
✏️ Modifier le dernier commitgit commit --amend
📜 Voir l’historiquegit log
📋 Historique condenségit log --oneline
🌳 Historique graphiquegit log --graph --oneline
🔍 Voir un commit précisgit show <commit-id>

🌿 Gestion des branches

📌 Action🧠 Commande
📋 Lister les branchesgit branch
🆕 Créer une branchegit branch <nom-branche>
🔄 Changer de branchegit checkout <nom-branche>
⚡ Créer et changergit checkout -b <nom-branche>
🔄 Changer (moderne)git switch <nom-branche>
⚡ Créer et changer (moderne)git switch -c <nom-branche>
🗑️ Supprimer une branchegit branch -d <nom-branche>
🔗 Fusionner une branchegit merge <nom-branche>

🌐 Dépôts distants

📌 Action🧠 Commande
📋 Lister les remotesgit remote -v
🔗 Ajouter un remotegit remote add origin <url>
⬆️ Pousser vers le remotegit push origin <branche>
⬇️ Récupérer du remotegit pull origin <branche>
📥 Télécharger sans mergergit fetch origin
🏷️ Pousser les tagsgit push --tags
🆕 Pousser nouvelle branchegit push -u origin <branche>

🛠️ Dépannage et annulations

📌 Action🧠 Commande
↩️ Annuler les modificationsgit checkout -- <fichier>
🔙 Revenir au dernier commitgit reset --hard HEAD
⏪ Revenir à un commit précisgit reset --hard <commit-id>
📦 Sauvegarder temporairementgit stash
📤 Restaurer le stashgit stash pop
📋 Lister les stashgit stash list
🔍 Chercher dans l’historiquegit log --grep="motclé"
🧹 Nettoyer les fichiersgit 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)