Skip to main content

Synchronizing with GitHub


Table of contents


  1. Git remotes
  2. Cloning a repository
  3. Git fetch vs git pull
  4. Git push
  5. Handling conflicts
  6. Synchronization workflows
  7. 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

  1. Downloads the entire repository
  2. Automatically configures origin
  3. Creates a local main branch linked to origin/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

Aspectfetchpull
Downloads
Applies
Safety✅ Safer⚠️ May create conflicts
ControlFullAutomatic

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

  1. Identify the conflicting files
git status
  1. Open and edit the files

The file contains:

<<<<<<< HEAD
Votre version
=======
Version du remote
>>>>>>> origin/main
  1. Choose the right version (or combine them)
Version finale après résolution
  1. Mark as resolved
git add file.txt
  1. 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

PracticeWhy
Pull before startingAvoid conflicts
Push regularlyBack up your work
Use feature branchesIsolate changes
Check before pushinggit status, git diff

❌ Avoid

PracticeRisk
Force push to mainLoss of team work
Push without pullingConflicts
Large monolithic commitsHard 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

CommandDescription
git fetchDownload without applying
git pullDownload and apply
git pushSend to the remote
git push -u origin branchFirst push with tracking
git push --force-with-leaseSafe force push

🔝 Back to table of contents