====== Git======
===== Récupérer sources depuis un dépôt =====
git clone git@framagit.org:mres-web/site_MRES_squelette.git
Pour Framagit, renseigner au préalable dans les paramètres du compte la clé publique du PC, à récuperer dans ''~/.ssh/id_rsa.pub''
===== Récupérer les changements d'un dépôt distant =====
Resynchronise le dépôt local avec le dépôt distant. Ne change rien dans l'espace de travail.
# Avec "origine" = nom du dépôt distant (remote)
git fetch origin
===== Gestion des branches =====
Créer une branche : ((https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell#_create_new_branch))
git branch nomdelabranche
Basculer sur cette branche : ((https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell#_switching_branches))
git checkout nomdelabranche
Faire ces deux actions (créer et basculer sur une nouvelle branche) en une seule commande avec un raccourci : ((https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging#_basic_branching))
git checkout -b nomdelabranche
Fusionner les changements d'une autre branche (de //hotfix// vers //main// dans cet exemple) :
git checkout main
git merge hotfix
Re-baser son travail sur une autre branche, par exemple avant de demander la fusion de ses changements dans cette branche. Résultat similaire à un ''merge'' mais pour obtenir un historique plus propre.
⚠️ À ne pas faire sur une branche publique qui a potentiellement servi de base à d'autres modifications (([[https://git-scm.com/book/en/v2/Git-Branching-Rebasing#The%20Perils%20of%20Rebasing|The Perils of Rebasing]]))
# Re-baser l'environnement de travail sur la branche develop. Idéalement faire un fetch avant !
git rebase develop
Obtenir la liste des branches :
git branch
# Uniquement celles qui n'ont pas encore été fusionnées :
git branch --no-merged
Supprimer une branche :
git branch -d hotfix
Renommer une branche, par exemple de //master// vers //main// ((https://git-scm.com/book/en/v2/Git-Branching-Branch-Management#_changing_a_branch_name)):
git branch --move master main
# L'envoyer sur le dépôt distant
git push --set-upstream origin main
# Supprimer l'ancienne du dépôt distant
git push origin --delete master
# Vérifier le résultat :
git branch --all
===== Obtenir statut de l'espace de travail =====
git status
===== Créer dépôt de travail à partir de sources =====
A la racine des sources, jouer les commandes suivantes :
git init
git add .
git commit -m 'Initial Commit'
Sources : [[https://git-scm.com/book/en/v2/Git-Basics-Getting-a-Git-Repository|Git Basics - Getting a Git Repository]]
===== Créer dépôt nu ("bare") =====
S'il existe déjà un dépôt Git, le cloner :
git clone --bare my_project my_project.git
Exemple avec un dossier SMB (sous windows)
git clone --bare "C:\Users\ajacquemin\Sources\dsi\nom_depot" "\\\\serveurfichiers\...\git\gdm\dsi\nom_depot.git"
Exemple sous Linux
git clone --bare ~/Webmastering/backup/dokuwiki-git/ /media/eddie/public/git/dokuwiki.git
Sources : [[http://git-scm.com/book/en/Git-on-the-Server-Getting-Git-on-a-Server|Getting Git on a Server]] et [[http://www.bitflop.com/document/111|Git bare vs. non-bare repositories]]
===== Gestion des dépôts distants (remote) =====
==== Lister ====
$ git remote -v
origin //Efiles.feel-it.mad/xanit/sources_dvpt/git/gdm/commerce/bonkdostor-gdm (fetch)
origin //Efiles.feel-it.mad/xanit/sources_dvpt/git/gdm/commerce/bonkdostor-gdm (push)
Sources : [[http://git-scm.com/book/en/Git-Basics-Working-with-Remotes|Working with Remotes]]
==== Ajouter ====
git remote add origin "///serveur-de-fichiers\dossier\depot.git"
Si besoin remplacer ''origin'' par l'alias à donner au dépôt.
==== Modifier ====
# Changer l'URL d'un dépôt distant
$ git remote set-url origin "….git"
# Renommer un dépôt distant
$ git remote rename origin newname
Remplacer ''origin'' par le nom du dépôt distant
===== Lister changements d'un commit =====
Obtenir la liste de tous les fichiers modifiés par un commit donné
git diff-tree --no-commit-id --name-only -r
Source : http://stackoverflow.com/questions/424071/how-to-list-all-the-files-in-a-commit
===== Comparer deux branches =====
Liste tous les fichiers qui diffèrent entre deux branches :
git diff --stat=120 branche1..branche2
Source : http://stackoverflow.com/questions/9834689/comparing-two-branches-in-git#9834872
===== Changer date d'un commit =====
git filter-branch -f --env-filter \
'if [ $GIT_COMMIT = ee75eb2698bb3c2da694eb3599ad7a990999d1f3 ]
then
export GIT_AUTHOR_DATE="Sun Feb 09 22:00:00 2014 +0100"
export GIT_AUTHOR_DATE="Sun Feb 09 22:00:00 2014 +0100"
fi'
Source : http://stackoverflow.com/questions/454734/how-can-one-change-the-timestamp-of-an-old-commit-in-git
===== Changer auteur d'un commit =====
#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="arnaud@jacquemin.info"
CORRECT_NAME="Arnaud Jacquemin"
CORRECT_EMAIL="arnaud.jacquemin+framagit@free.fr"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
===== Annuler un commit =====
==== Non poussé ====
Supprimer définitivement le dernier commit non poussé :
git reset --hard HEAD~1
Supprimer le dernier commit non poussé tout en conservant les modifications dans la staging area :
git reset --soft HEAD~1
==== Déjà poussé ====
Revenir dans l’étant du commit précédent en appliquant un nouveau commit qui est exactement l’opposé du précédent : très pratique pour annuler un commit qui a déjà été poussé, et qu’on ne peut donc pas supprimer du dépôt central sans gêner les collaborateurs :
git revert HEAD~1
Source : http://gebeo.info/2014/05/13/git-annuler-le-dernier-commit/
Annuler partiellement un commit déjà poussé :
git revert --no-commit
git reset # sortir les modification de la staging area
# effectuer les éventuelles corrections souhaitées
git add # ajouter les modifications souhaitées dans le staging
git checkout . # pour ne pas modifier les autres fichiers (d'où l'annulation partielle)
git commit
Source : http://stackoverflow.com/questions/5669358/can-i-do-a-partial-revert-in-git#5669428
===== Dépôt sur OVH =====
LOGINOVH=login
ssh ${LOGINOVH}@ssh.cluster006.ovh.net
#Sur le serveur OVH
NOMPROJET=nom_projet
mkdir -p ~/sources/${NOMPROJET}.git
cd ~/sources/${NOMPROJET}.git
git init --bare
#En local
NOMPROJET=nom_projet
LOGINOVH=login
cd ./${NOMPROJET}
git remote add ovh ssh://${LOGINOVH}@ssh.cluster006.ovh.net/home/${LOGINOVH}/sources/${NOMPROJET}.git
git push ovh master
Source : http://wiki.rezo-zero.com/index.php?title=Cr%C3%A9er_un_d%C3%A9p%C3%B4t_nu_sur_OVH&oldid=657
A lire aussi : https://blog.jtlebi.fr/2013/11/30/gerer-son-site-avec-git-sur-un-serveur-mutualise/
===== Corriger error: refs does not point to a valid object =====
git repack -a && rm ./objects/info/alternates
Cf. http://randyfay.com/content/git-clone-reference-considered-harmful
===== Supprimer un tag =====
git push --delete origin tagname
git tag -d tagname
Source : http://stackoverflow.com/questions/5480258/how-to-delete-a-remote-tag
===== Supprimer toutes les modifications locales =====
Remove untracked files from the current directory as well as any files that Git usually ignores.
# Undo changes in tracked files
git reset --hard
# Remove untracked files
git clean -df
Source : https://www.atlassian.com/git/tutorials/undoing-changes/git-clean