You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
List only local branches and find which remote branches they are tracking
git branch -vv
Create a branch and switch to it
git switch -c branch_name
Note:
Naming its branches correctly
Even if there are conventions according to communities and programming languages (camelCase, spinal-case, PascalCase etc.) it is proven that the snake_case is the easiest to read.
So that's what is often encountered to name branches.
Use a prefix
A prefix, separated by a /, is often used to give a better idea of the content.
Example:
Update your code with changes from a remote server (Fetch + Merge / Rebase)
Recover all branches and clean up those that no longer exist on the remote server.
git fetch --all --prune
Update your local master branch
Get changes from the remote: git fetch --all --prune
If necessary, switch to your local master branch: git switch master
Update your local branch with the changes from the remote origin: git merge --ff-only origin/master
NOTE: the --ff-only option is very important.
This is to ensure that the master branch of the server is clean and consistent with your local branch.
Using this option,
If the update does not pass, that means that someone (or yourself) did something they probably shouldn't have done on the master branch...
Update a working branch with changes from the master branch
Get changes from the remote: git fetch --all --prune
If necessary, switch to your working branch: git switch working_branch_name
Update your working branch with changes from the remote origin: git rebase --no-ff origin/master
NOTE: the -- no-ff option is very important.
This is to guarantee that git won't fast forward and it's up to you to resolve any conflicts.
NOTE: Updating your local master branch or a working branch with changes from the remote master branch (the most important source of truth) is therefore symmetrical:
On one side: merge --ff-only -> fast forward only
On the other side: rebase --no-ff -> without any fast forward
This workflow ensures that all conflicts are properly settled on the working branch BEFORE merging changes into master
(Shared working branch) Update your working branch with changes made by someone else
Get changes from the remote: git fetch --all --prune
If necessary, switch to your working branch: git switch working_branch_name
If necessary, stash any non commited changes: git stash save "before update"
Update your local branch with changes from the remote server: git merge --ff-only origin/working_branch_name
Get only changes from commit (Cherry-pick)
Find the commit hash you want to pick (using git log or other means): git log
If necessary, switch to the branch to modify: git switch branch_name
Get the changes: git cherry-pick COMMIT_HASH
Cherry pick all commits between 2 commits
git cherry-pick FIRST_COMMIT..LAST_COMMIT
Show git history (Log)
git log --online -n 10 # Last 10 commmits
Clean / Modify history
We saw previously that the --amend option allows you to modify or rename the last commit.
Interactive rebase is very useful to clean up your history into comprhensive commit before doing a PR.
For each commit, you can:
reorder,
merge,
remove,
rename,
modify (add changes),
split into several commits
etc.
git rebase -i FIRST_COMMIT^1
TIP: If you just want to add changes to a past commit without going through an interactive rebase, you can use the --fixup option
History before:
6e9f6fc4f7 feat: Forgot password
82c6f15175 feat: User authentication
a86858b228 feat: Adding a user
9192a998a9 feat: Added movie to library
You want to modify the commit 9192a998a9 feat: Adding a movie to the library
git commit --fixup 9192a998a9
History after:
eadf86710e fixup! feat: Adding a movie to the library
6e9f6fc4f7 feat: Forgot password
82c6f15175 feat: User authentication
a86858b228 feat: Adding a user
9192a998a9 feat: Added movie to library
Then, all you have to do is an interactive rebase using the --autosquash option and save:
git rebase -i --autosquash FIRST_COMMIT^1
This will automatically order fixup commits and flag them accordingly.
Exemple d'alias: brgit config --global alias.br branch
Lister que les branches "locales"
git br
Lister que les branches locales et voir quelle branches distantes est suivie
git br -vv
Supprimer une branche
git br -D nom_de_branche autre_branche
Basculer rapidement sur la branche où on était précédemment
git co -
TIP: c'est une bonne pratique de lister régulièrement ses branches locales
et supprimer celles qui ne sont plus présentes sur le dépot distant.
Renommer une branche
git branch -m ancien_nom nouveau_nom
Naviguer entre les branches
git checkout nom_de_la_branche
Exemple d'alias: co
git config --global alias.co checkout
Créer une branche et basculer directement dessus
git co -b nom_de_branche
Note:
Bien nommer ses branches
Même s'il existe des conventions selon les communautés et les languages de
programmation (camelCase, spinal-case, PascalCase etc.) il est prouvé que le
snake_case est le plus facile à lire. C'est donc ce qui est souvent rencontré
pour nommer les branches.
utiliser un prefix
Un prefix, séparé par un /, est souvent utiliser pour donner une idée du contenu.
Exemple:
Travailler avec ses modifications (Stage, Unstage, Stash)
Savoir où j'en suis
git status
version condensée: git status -sb
Exemple d'alias: st
git config --global alias.st "status -sb"
TIP: Avant de faire un commit, afficher le status est très utile pour vérifier
qu'on n'oublie rien ou qu'on n'ajoute pas un fichier sans le vouloir.
Ajouter des modifications à valider (Stage)
git add
ou
git stage
Ajouter des modifications à valider mais que des fichiers déjà suivis
git add -u
Enlever toutes les modifications à valider (Unstage)
git reset HEAD --
ou fichier par fichier:
git reset HEAD -- fichier autre_fichier
Exemple d'alias: unstage
git config --global alias.unstage "reset HEAD --"
Supprimer toutes les modifications qu'on a fait
git co -- .
ou fichier par fichier:
git co -- fichier autre_fichier
Mettre de coté des modifications (Stash) (planquer/mettre de coté)
git stash save "commentaire"
Lister le stash
git stash list
Réappliquer les modifications et enlever du stash
git stash pop stash@{0}
NOTE: il faut parfois "echapper" les caratères spéciaux pour certains terminaux ou shell,
ex: stash@\{0\}
Réappliquer les modifications sans les enlever du stash
git stash apply stash@{0}
Supprimer du stash
git stash drop stash@{0}
Tout supprimer du stash
git stash clear
Valider ses modifications (Commit)
git commit
Exemple d'alias: ci
git config --global alias.ci commit
Ajouter directement un message court
git ci -m "git101(commit): Message court"
Message de commit semantic
Comme pour les branches, les messages de commit sont importants.
L'idéal, notamment pour les fonctionnalités, est d'essayer d'écrire des messages
qui exprime ce que l'application peut faire de plus.
En parcourants l'historique on peut alors voir l'évolution du logiciel (se lit de bas en haut).
Exemple :
feat: Paiement refusé - Mise ne attente de la commande
feat: Paiement de la commande
feat: Ajout d'un film au panier
feat: Recherche de films par catégorie
feat: Mot de passe oublié
feat: Authentification d'un utilisateur
feat: Ajout d'un utilisateur
feat: Ajout d'un film à la bibliothèque
Il faut éviter les commits qui n'apportent pas d'information pertinente,
tels que :
update init_db.sql
Add class
[...]
Semantic Commit, comme pour les branches, c'est une bonne pratique répandue
qui utilise des préfix :
feat: (new feature for the user, not a new feature for build script)
fix: (bug fix for the user, not a fix to a build script)
docs: (changes to the documentation)
style: (formatting, missing semi colons, etc; no production code change)
refactor: (refactoring production code, eg. renaming a variable)
test: (adding missing tests, refactoring tests; no production code change)
chore: (updating grunt tasks etc; no production code change)
On peut ajouter un élément technique entre paranthèse () si on plus de précision
Exemple :
fix(ui): Message
chore(ws): Message
[...]
Modifier le dernier commit pour ajouter des modifications
git commit --amend --no-edit
Modifier le dernier commit pour ajouter des modifications et changer le message
Récupérer les modifications présentes sur le serveur : git fap
Si besoin, basculer sur sa branche master locale : git co master
Mettre à jour sa branche locale avec celle du serveur : git merge --ff-only origin/master
NOTE: l'option --ff-only est très importante.
Elle garantie que la branche master du serveur est "propre" et cohérente avec votre branche locale.
Avec cette option,
Quand la mise à jour ne passe pas,
Alors cela signifie que quelqu'un a fait quelque chose qu'il n'aurait surement pas dû sur la branche master...
Mettre à jour une branche de travail avec la branche master
Récupérer les modifications présentes sur le serveur : git fap
Si besoin, basculer sur sa branche de travail : git co nom_de_la_branche
Mettre à jour la branche avec celle du serveur : git rebase --no-ff origin/master
NOTE: l'option --no-ff est très importante.
Elle garantie que git ne fera pas d'avance rapide et c'est à vous de régler les conflits s'il y en a.
NOTE: La mise à jour de la branche master et celle d'une branche avec master se fait donc de manière
symètrique :
Merge --ff-only : uniquement fast forward
Rebase --no-ff : sans aucun fast forward
Cela garantie que que tous les conflits soient régler proprement sur la branche de travail AVANT de merger les modifications dans master
(Branche de travail partagée) Mettre à jour sa branche de travail avec les modifications apportées par une autre personne
Ici, on peut procéder de plusieurs façon,
A - Soit, comme pour une mise à jour avec master: pénible mais efficace, pas besoin de nettoyage de l'historique avant merge sur master
B - Soit avec des commits de merge: plus souple et plus facile, mais obligera un nettoyage de l'historique avant merge sur master
Solution A:
Récupérer les modifications présentes sur le serveur : git fap
Si besoin, basculer sur sa branche de travail : git co nom_de_la_branche
Mettre à jour la branche avec celle du serveur : git rebase --no-ff origin/nom_de_la_branche
NOTE: ce mécanisme récupère les modifications du serveur et "réapplique" par dessus nos modifications
Solution B:
Récupérer les modifications présentes sur le serveur : git fap
Si besoin, basculer sur sa branche de travail : git co nom_de_la_branche
Mettre à jour la branche avec celle du serveur : git merge origin/nom_de_la_branche
NOTE: ce mécanisme récupère les modifications du serveur et les applique par dessus nos modifications avec un commit de merge.
/!\ L'historique devient rapidement illisible et les mêmes modifications peuvent apparaitre plusieurs fois.
Récupérer seulement les modifications d'un commit provenant d'une autre branche (cherry-pick)
Trouver le hash du commit que l'on veut récupérer (avec git log ou autre moyen) : git log
Si besoin, basculer sur la branche à modifier: git co ma_branche
Récupérer les modifications : git cherry-pick HASH_DU_COMMIT