projtec:git
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| projtec:git [2017/10/19 15:43] – orel | projtec:git [2024/03/18 15:06] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Git ====== | ====== Git ====== | ||
| - | Un bon tutoriel pour jouer de manière interactive : https:// | + | Un bon tutoriel pour jouer de manière interactive |
| Commençons par créer un dépôt Git " | Commençons par créer un dépôt Git " | ||
| Line 25: | Line 25: | ||
| # mail / user name | # mail / user name | ||
| git config --global user.name " | git config --global user.name " | ||
| - | git config --global user.mail aurelien.esnard@u-bordeaux.fr | + | git config --global user.email aurelien.esnard@u-bordeaux.fr |
| # editeur de texte | # editeur de texte | ||
| git config --global core.editor emacs | git config --global core.editor emacs | ||
| Line 189: | Line 189: | ||
| ==== Les branches locales et distantes ==== | ==== Les branches locales et distantes ==== | ||
| - | Par défaut, nous travaillons sur la branche //master// du dépôt local. | + | Par défaut, nous travaillons sur la branche //master// du dépôt local que nous synchronisons avec la branche distante // |
| + | Il est possible de créer et d' | ||
| Nous allons maintenant créer une nouvelle branche //hotfix//, afin de corriger un bug. | Nous allons maintenant créer une nouvelle branche //hotfix//, afin de corriger un bug. | ||
| Line 200: | Line 201: | ||
| </ | </ | ||
| - | Nous sommes encore dans la branche //master// comme l' | + | Nous sommes encore dans la branche //master// comme l' |
| <code bash> | <code bash> | ||
| - | git checkout | + | git switch |
| git branch | git branch | ||
| * hotfix | * hotfix | ||
| master | master | ||
| + | </ | ||
| + | |||
| + | Regardons l' | ||
| + | <code bash> | ||
| + | git log --graph --oneline | ||
| + | * c908469 (HEAD -> hotfix, origin/ | ||
| + | * d144ffa autre bla bla | ||
| + | (...) | ||
| + | </ | ||
| + | |||
| + | Le dernier commit est bien commun à la branch hotfix, la branch master. Corrigeons le " | ||
| + | |||
| + | <code bash> | ||
| date >> date.txt | date >> date.txt | ||
| git add date.txt | git add date.txt | ||
| Line 212: | Line 225: | ||
| </ | </ | ||
| - | Une fois le bug corrigé, nous pouvons le commiter dans la branche courante //hotfix//. Notons que cela n'a aucun impact sur la branche //master//. Pour propager cette modification vers la branche //master//, il va falloir repasser dans la branche //master// et effectuer un //merge//. | + | Une fois le bug corrigé, nous pouvons le commiter dans la branche courante //hotfix//. Notons que cela n'a aucun impact sur la branche //master//: |
| <code bash> | <code bash> | ||
| - | git checkout | + | git log --graph --oneline |
| + | * c38c81a (HEAD -> hotfix) deux dates dans hotfix | ||
| + | * c908469 (origin/master, origin/ | ||
| + | * d144ffa autre bla bla | ||
| + | (...) | ||
| + | </ | ||
| + | |||
| + | Pour propager cette modification vers la branche //master//, il va falloir repasser dans la branche //master//, faire quelques modifs sur master puis effectuer un // | ||
| + | |||
| + | <code bash> | ||
| + | git switch master | ||
| + | date >> date2.txt | ||
| + | git add date2.txt | ||
| + | git commit -m " | ||
| git merge hotfix | git merge hotfix | ||
| </ | </ | ||
| - | On peut finalement supprimer la branche quand cette dernière n'est plus utile. | + | <code bash> |
| + | git log --graph --oneline | ||
| + | * | ||
| + | |\ | ||
| + | | * c38c81a (hotfix) deux dates dans hotfix | ||
| + | |/ | ||
| + | * c908469 bla bla | ||
| + | * d144ffa autre bla bla | ||
| + | </ | ||
| - | git branch -d hotfix | + | Pour propager ces modifications dans le dépôt distant on fera des pull/push comme d' |
| - | Si l'on souhaite que sa branche locale // | ||
| - | git checkout mybranch | + | Si l'on souhaite que sa branche locale //hotfix// deviennent une branche distante (ou " |
| - | git push --set-upstream origin mybranch | + | |
| - | | + | |
| - | Pour voir toutes les branches | + | <code bash> |
| + | git log --graph --oneline | ||
| + | * c38c81a | ||
| + | * c908469 bla bla | ||
| + | * d144ffa autre bla bla | ||
| + | (...) | ||
| + | git switch hotfix | ||
| + | git push --set-upstream origin hotfix | ||
| + | git log --graph --oneline | ||
| + | * c38c81a (HEAD -> hotfix, origin/ | ||
| + | * c908469 bla bla | ||
| + | * d144ffa autre bla bla | ||
| + | (...) | ||
| + | </ | ||
| + | La encore on se synchronisera avec la branche distance à l'aide des commandes push/pull: | ||
| + | |||
| + | <code bash> | ||
| + | date >> date.txt | ||
| + | git add date.txt | ||
| + | git commit -m "mise à jour de la date" | ||
| + | git log --graph --oneline | ||
| + | * 68b1978 (HEAD -> hotfix) mise à jour de la date | ||
| + | * c38c81a (origin/ | ||
| + | (...) | ||
| + | git push | ||
| + | git log --graph --oneline | ||
| + | * 68b1978 (HEAD -> hotfix, origin/ | ||
| + | * c38c81a deux dates dans hotfix | ||
| + | (...) | ||
| + | </ | ||
| + | |||
| + | |||
| + | Pour voir toutes les branches (locales et distantes) : | ||
| + | <code bash> | ||
| git fetch -p | git fetch -p | ||
| git branch -a | git branch -a | ||
| + | </ | ||
| | | ||
| __Nota Bene__ : le //fetch// permet de mettre à jour son cache local et éventuellement (option -p) d' | __Nota Bene__ : le //fetch// permet de mettre à jour son cache local et éventuellement (option -p) d' | ||
| | | ||
| - | Pour récupérer une branche distante et la suivre via une branche locale : | ||
| - | | + | Puis pour supprimer la branche locale, il faut faire ceci : |
| + | <code bash> | ||
| + | | ||
| + | </code> | ||
| - | Puis pour supprimer la branche distante (enfin presque...), | ||
| + | Puis pour supprimer la branche distante (enfin presque...), | ||
| + | <code bash> | ||
| git push origin --delete mybranch | git push origin --delete mybranch | ||
| - | + | </code> | |
| - | Expliquer le //rebase// simple : | + | |
| - | | + | Pour récupérer une branche distante et la suivre via une branche locale : |
| - | git rebase master | + | <code bash> |
| - | git checkout | + | |
| - | git merge mybranch | + | </ |
| + | |||
| + | Considérons le cas suivant... Expliquer ensuite le //rebase// simple : | ||
| + | |||
| + | <code bash> | ||
| + | git switch | ||
| + | git rebase master | ||
| + | git switch | ||
| + | git merge mybranch | ||
| git branch -d mybranch | git branch -d mybranch | ||
| + | </ | ||
| Pour aller plus loin : | Pour aller plus loin : | ||
| Line 281: | Line 357: | ||
| ==== Restaurer un fichier supprimé ou une ancienne version ==== | ==== Restaurer un fichier supprimé ou une ancienne version ==== | ||
| - | Si l'on souhaite annuler les modifications faites dans le //working directory// | + | Si l'on souhaite annuler les modifications faites |
| - | + | ||
| - | git checkout | + | |
| - | + | ||
| - | Ou bien pour récupérer un fichier | + | |
| git checkout -- myfile | git checkout -- myfile | ||
| Line 294: | Line 366: | ||
| git checkout < | git checkout < | ||
| - | Si l'on souhaite annuler les derniers commits dans le dépôt local pour revenir à la version précédente identifiée par < | ||
| - | | + | Lorsque l'on a commité (en local) des trucs par erreur, il est possible d' |
| + | |||
| + | | ||
| | | ||
| - | Je n'ai pas compris ce qui se passe si on revient à une version précédente au dernier | + | Attention, l'option --hard a pour effet de supprimer *définitivement* dans vos fichiers les modifications du dernier |
| - | // A compléter... avec revert | + | Si on souhaite maintenant annuler le dernier commit sans toucher aux fichiers, par exemple pour corriger un commit incomplet, on peut faire : |
| + | |||
| + | git reset HEAD~1 | ||
| + | |||
| + | Dans ce cas, les fichiers impliqués dans le commit annulé apparaissent comme *modified and not staged*... A vous de retravailler ces fichiers et de proposer un nouveau commit ! Une autre solution à ce problème consiste à faire un nouveau commit qui se fusionne | ||
| + | |||
| + | git commit -m "maj de mon dernier commit..." | ||
| + | |||
| + | Autre cas de figure, j'ai commité des modifs sur deux fichiers (*good* et *bad*), et je souhaite annuler partiellement mon commit en annulant les modifs sur le fichier *bad*, sans pour autant perdre les modifications sur ce fichier dans répertoire local... | ||
| + | |||
| + | git reset HEAD~1 | ||
| + | git add good | ||
| + | git commit -m " | ||
| + | |||
| + | Ou encore, une solution un peu plus tricky... | ||
| + | |||
| + | git reset HEAD~1 -- bad # reset partiel (HEAD ne bouge pas) | ||
| + | git commit -m " | ||
| + | |||
| + | |||
| + | Dans le cas, où l'on a *push* le commit que l'on souhaite supprimer, les chosent se compliquent car elles peuvent maintenant impacter d' | ||
| + | |||
| + | git revert HEAD~1 | ||
| + | git push | ||
| + | |||
| + | Si l'on souhaite synchroniser à nouveau son dépôt local avec *origin/master* en ignorant les supprimant les derniers commits locaux (non synchronisés), | ||
| + | |||
| + | git reset --hard origin/master | ||
| + | |||
| + | A utiliser avec prudence ! Néanmoins, vos anciens commits ne sont pas totalement perdus, on peut les retrouver en faisant : | ||
| + | |||
| + | git reflog | ||
| Line 334: | Line 438: | ||
| # ... continue hacking .. | # ... continue hacking .. | ||
| + | ==== Utilisation du Worktree ==== | ||
| + | |||
| + | C'est une alternative au stash pour réaliser des développements concurrents dans des branches différentes... | ||
| + | |||
| + | $ git worktree add / | ||
| + | Preparing worktree (checking out ' | ||
| + | HEAD is now at 88d22d5 ... | ||
| + | | ||
| + | $ git worktree list | ||
| + | / | ||
| + | / | ||
| + | |||
| + | L' | ||
| + | |||
| + | === Suppression d'une Branche Distante ==== | ||
| + | |||
| + | Pour supprimer une branche locale sur votre ordinateur, la commande est la suivante : | ||
| + | |||
| + | $ git branch -d [nom_de_la_branche] | ||
| + | |||
| + | Ou bien : | ||
| + | |||
| + | $ git branch -D [nom_de_la_branche] | ||
| + | |||
| + | Pour supprimer la branche située sur le dépôt distant (syntaxe récente) : | ||
| + | |||
| + | $ git push origin --delete [nom_de_la_branche] | ||
| + | |||
| + | Pour mettre à jour les changements sur un autre ordinateur, il faut faire : | ||
| + | |||
| + | $ git fetch --all -prune | ||
| + | |||
| + | => https:// | ||
| + | ==== Suppression de Commit Fautifs sur le dépôt distant ==== | ||
| + | |||
| + | Revenir à l' | ||
| + | |||
| + | $ git reset --hard <commit hash> | ||
| + | |||
| + | Forcer cet état comme le dernier état du dépôt : | ||
| + | |||
| + | $ git push --force origin master | ||
| + | | ||
| + | Attention : Ces commandes sont susceptibles d' | ||
| ==== Savane ==== | ==== Savane ==== | ||
| Line 352: | Line 500: | ||
| ==== A Lire ==== | ==== A Lire ==== | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| * http:// | * http:// | ||
| * https:// | * https:// | ||
| Line 357: | Line 508: | ||
| * https:// | * https:// | ||
| * https:// | * https:// | ||
| + | * https:// | ||
projtec/git.1508427815.txt.gz · Last modified: 2024/03/18 15:05 (external edit)
