Synchronizing with GitHub
Table of contents
- Git remotes
- Cloning a repository
- Git fetch vs git pull
- Git push
- Handling conflicts
- Synchronization workflows
- Best practices
1 - Git remotes
What is a remote?
A remote is a reference to a remote repository (such as GitHub).
┌─────────────────┐ ┌─────────────────┐
│ Local Repo │ ←────── │ GitHub Repo │
│ (votre PC) │ ──────→ │ (origin) │
└─────────────────┘ └─────────────────┘
↑ ↑
git pull git push
git fetch
View the remotes
git remote -v
Result:
origin [email protected]:user/repo.git (fetch)
origin [email protected]:user/repo.git (push)
Add a remote
git remote add origin [email protected]:user/repo.git
Other remote commands
# Renommer un remote
git remote rename origin github
# Changer l'URL
git remote set-url origin [email protected]:user/new-repo.git
# Supprimer un remote
git remote remove origin
# Voir les détails
git remote show origin
🔝 Back to table of contents
2 - Cloning a repository
Basic clone
# Via SSH (recommandé)
git clone [email protected]:user/repo.git
# Via HTTPS
git clone https://github.com/user/repo.git
Clone options
# Cloner dans un dossier spécifique
git clone [email protected]:user/repo.git mon-dossier
# Cloner une branche spécifique
git clone -b develop [email protected]:user/repo.git
# Clone superficiel (sans historique)
git clone --depth 1 [email protected]:user/repo.git
# Clone avec sous-modules
git clone --recurse-submodules [email protected]:user/repo.git
What happens during a clone
- Downloads the entire repository
- Automatically configures
origin - Creates a local
mainbranch linked toorigin/main
🔝 Back to table of contents
3 - Git fetch vs git pull
Git fetch
Downloads the changes without applying them.
git fetch origin
git fetch origin main
git fetch --all # Tous les remotes
After a fetch:
# Voir les différences
git diff main origin/main
# Appliquer manuellement
git merge origin/main
Git pull
Downloads AND applies the changes (fetch + merge).
git pull origin main
git pull # Si tracking configuré
Comparison
| Aspect | fetch | pull |
|---|---|---|
| Downloads | ✅ | ✅ |
| Applies | ❌ | ✅ |
| Safety | ✅ Safer | ⚠️ May create conflicts |
| Control | Full | Automatic |
When to use which?
# Workflow sécurisé (recommandé)
git fetch origin
git log HEAD..origin/main # Voir ce qui a changé
git merge origin/main # Appliquer
# Workflow rapide
git pull origin main
Pull with rebase
# Au lieu de merge
git pull --rebase origin main
# Configurer par défaut
git config --global pull.rebase true
🔝 Back to table of contents
4 - Git push
Basic push
git push origin main
First push (with tracking)
git push -u origin main
# ou
git push --set-upstream origin main
After -u, you can simply run:
git push
git pull
Push a new branch
# Créer et pousser une branche
git checkout -b feature/new-feature
git push -u origin feature/new-feature
Push all branches
git push --all origin
Push tags
# Un tag
git push origin v1.0.0
# Tous les tags
git push --tags
Force push (dangerous)
# Écrase l'historique distant
git push --force
# Plus sécurisé (vérifie que personne n'a push entre temps)
git push --force-with-lease
⚠️ Never force push to main/master if others are working on it!
🔝 Back to table of contents
5 - Handling conflicts
When does a conflict occur?
When you and someone else have modified the same part of a file.
git pull origin main
# CONFLICT (content): Merge conflict in file.txt
# Automatic merge failed; fix conflicts and commit
Resolve the conflicts
- Identify the conflicting files
git status
- Open and edit the files
The file contains:
<<<<<<< HEAD
Votre version
=======
Version du remote
>>>>>>> origin/main
- Choose the right version (or combine them)
Version finale après résolution
- Mark as resolved
git add file.txt
- Finish the merge
git commit
# Git génère un message de merge automatiquement
Abort if there's a problem
git merge --abort
# ou
git pull --abort
Visual tools for conflicts
git mergetool
🔝 Back to table of contents
6 - Synchronization workflows
Solo workflow
# Avant de travailler
git pull origin main
# Développer...
git add .
git commit -m "feat: nouvelle fonctionnalité"
# Pousser
git push origin main
Team workflow (feature branches)
# 1. Mettre à jour main
git checkout main
git pull origin main
# 2. Créer une feature branch
git checkout -b feature/ma-feature
# 3. Développer et commiter
git add .
git commit -m "feat: ma feature"
# 4. Pousser la branche
git push -u origin feature/ma-feature
# 5. Créer une Pull Request sur GitHub
# 6. Après merge, nettoyer
git checkout main
git pull origin main
git branch -d feature/ma-feature
Keep your branch up to date
# Méthode 1 : Merge
git checkout feature/ma-feature
git fetch origin
git merge origin/main
# Méthode 2 : Rebase (historique plus propre)
git checkout feature/ma-feature
git fetch origin
git rebase origin/main
🔝 Back to table of contents
7 - Best practices
✅ Do
| Practice | Why |
|---|---|
| Pull before starting | Avoid conflicts |
| Push regularly | Back up your work |
| Use feature branches | Isolate changes |
| Check before pushing | git status, git diff |
❌ Avoid
| Practice | Risk |
|---|---|
| Force push to main | Loss of team work |
| Push without pulling | Conflicts |
| Large monolithic commits | Hard to review |
Verification commands
# Avant de pousser
git status
git log origin/main..HEAD # Commits à pousser
git diff origin/main # Différences
# Après fetch
git log HEAD..origin/main # Commits à récupérer
Summary
| Command | Description |
|---|---|
git fetch | Download without applying |
git pull | Download and apply |
git push | Send to the remote |
git push -u origin branch | First push with tracking |
git push --force-with-lease | Safe force push |