Core Git Workflow Commands
Table of contents
- The basic Git workflow
- git init - Initialize a repository
- git status - Check the state
- git add - Add to staging
- git commit - Record changes
- git log - View the history
- git clone - Clone a repository
- git push - Send to the server
- git pull - Retrieve changes
- Command summary
1 - The basic Git workflow
The three Git areas
┌─────────────────┐ git add ┌─────────────────┐ git commit ┌─────────────────┐
│ │ ───────────▶ │ │ ───────────▶ │ │
│ Working │ │ Staging │ │ Repository │
│ Directory │ │ Area │ │ (.git) │
│ │ ◀─────────── │ (Index) │ │ │
│ │ git restore │ │ │ │
└─────────────────┘ └─────────────────┘ └─────────────────┘
Fichiers Prêts à Historique
modifiés commiter des versions
File lifecycle
| State | Description |
|---|---|
| Untracked | New file, not yet tracked by Git |
| Staged | Added to staging, ready for commit |
| Committed | Recorded in the history |
| Modified | Modified since the last commit |
🔝 Back to table of contents
2 - git init - Initialize a repository
2.1 Create a new repository
mkdir mon-projet
cd mon-projet
git init
Result:
Initialized empty Git repository in /chemin/vers/mon-projet/.git/
2.2 What happens
Git creates a hidden .git folder that contains:
.git/
├── HEAD # Pointe vers la branche actuelle
├── config # Configuration du dépôt
├── description # Description du dépôt
├── hooks/ # Scripts automatiques
├── objects/ # Stockage des données
└── refs/ # Références (branches, tags)
⚠️ Never manually delete or modify the
.gitfolder! You would lose all your history.
2.3 Verify the initialization
ls -la
git status
🔝 Back to table of contents
3 - git status - Check the state
3.1 Basic usage
git status
3.2 The different possible states
Empty repository:
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)
Untracked files:
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
index.html
style.css
Files in staging:
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: index.html
Modified files:
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
modified: index.html
3.3 Short format
git status -s
# ou
git status --short
Result:
M index.html # M = Modified
A style.css # A = Added (staged)
?? nouveau.txt # ?? = Untracked
🔝 Back to table of contents
4 - git add - Add to staging
4.1 Add a specific file
git add fichier.txt
4.2 Add multiple files
git add fichier1.txt fichier2.txt fichier3.txt
4.3 Add by extension
git add *.html
git add *.css
4.4 Add a folder
git add src/
4.5 Add all files
git add .
# ou
git add --all
# ou
git add -A
4.6 Add interactively
git add -p
# ou
git add --patch
This command lets you choose which changes to add within each file.
4.7 Remove from staging
# Retirer un fichier du staging (garder les modifications)
git restore --staged fichier.txt
# Ancienne syntaxe (encore valide)
git reset HEAD fichier.txt
🔝 Back to table of contents
5 - git commit - Record changes
5.1 Commit with a message
git commit -m "Message descriptif du commit"
5.2 Commit with the editor
git commit
Opens the configured editor to write a detailed message.
5.3 Commit in a single command (add + commit)
git commit -am "Message"
⚠️ This option only works for already tracked files (not new files).
5.4 Modify the last commit
# Modifier le message
git commit --amend -m "Nouveau message"
# Ajouter des fichiers oubliés
git add fichier-oublie.txt
git commit --amend --no-edit
5.5 Best practices for messages
Recommended structure:
<type>: <description courte> (max 50 caractères)
<corps du message optionnel>
- Explication détaillée
- Pourquoi ce changement
<footer optionnel>
Fixes #123
Common types:
| Type | Description |
|---|---|
feat | New feature |
fix | Bug fix |
docs | Documentation |
style | Formatting (no code change) |
refactor | Code refactoring |
test | Adding tests |
chore | Maintenance, dependencies |
Examples:
git commit -m "feat: ajouter la page de connexion"
git commit -m "fix: corriger le bug d'affichage du menu"
git commit -m "docs: mettre à jour le README"
🔝 Back to table of contents
6 - git log - View the history
6.1 Basic log
git log
6.2 Condensed log (one line per commit)
git log --oneline
Result:
a1b2c3d feat: ajouter la page de connexion
e4f5g6h fix: corriger le bug du menu
i7j8k9l docs: mettre à jour README
6.3 Graph log
git log --oneline --graph --all
6.4 Log with statistics
git log --stat
6.5 Limited log
# Les 5 derniers commits
git log -5
# Commits d'un auteur
git log --author="Jean"
# Commits depuis une date
git log --since="2024-01-01"
# Commits contenant un mot
git log --grep="bug"
6.6 Handy alias
git config --global alias.lg "log --oneline --graph --all --decorate"
Usage:
git lg
🔝 Back to table of contents
7 - git clone - Clone a repository
7.1 Clone via HTTPS
git clone https://github.com/utilisateur/repo.git
7.2 Clone via SSH (recommended)
git clone [email protected]:utilisateur/repo.git
7.3 Clone into a specific folder
git clone [email protected]:utilisateur/repo.git mon-dossier
7.4 Clone a specific branch
git clone -b develop [email protected]:utilisateur/repo.git
7.5 Shallow clone (without history)
git clone --depth 1 [email protected]:utilisateur/repo.git
🔝 Back to table of contents
8 - git push - Send to the server
8.1 Basic push
git push origin main
8.2 Push with tracking
git push -u origin main
After -u, you can simply run:
git push
8.3 Push all branches
git push --all origin
8.4 Push tags
git push --tags
8.5 Force push (use with caution)
git push --force
# ou plus sécurisé
git push --force-with-lease
⚠️ Warning:
--forceoverwrites the remote history. Only use it on your own branches!
🔝 Back to table of contents
9 - git pull - Retrieve changes
9.1 Basic pull
git pull origin main
9.2 Simple pull (if tracking is configured)
git pull
9.3 Pull with rebase
git pull --rebase origin main
9.4 Fetch + Merge manually
git fetch origin
git merge origin/main
💡 Tip:
git fetchis safer because it retrieves without merging automatically.
🔝 Back to table of contents
10 - Command summary
Essential commands
| Command | Description |
|---|---|
git init | Initialize a repository |
git status | See the state of files |
git add <fichier> | Add to staging |
git add . | Add all files |
git commit -m "msg" | Create a commit |
git log | View the history |
git clone <url> | Clone a repository |
git push | Send to the server |
git pull | Retrieve from the server |
Typical workflow
# 1. Créer/modifier des fichiers
echo "Hello World" > index.html
# 2. Vérifier les modifications
git status
# 3. Ajouter au staging
git add index.html
# 4. Créer un commit
git commit -m "feat: ajouter la page d'accueil"
# 5. Envoyer vers GitHub
git push origin main
Next step
Time for practice with a complete Git workflow exercise!