Skip to content

Instantly share code, notes, and snippets.

@jpchateau
Last active April 4, 2024 16:06
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save jpchateau/d540f6bbae2d8a81d6f6 to your computer and use it in GitHub Desktop.
Save jpchateau/d540f6bbae2d8a81d6f6 to your computer and use it in GitHub Desktop.
Git - Commandes / Configuration / Astuces

Commandes

git add -vA # ajoute tous les changements au commit, verbeux
git rm [fichiers] # supprime les fichiers du dépôt
git add -p [fichier] # permet de préciser quels morceaux de code d'un fichier sont à ajouter au commit
git pull # met à jour ses fichiers locaux à partir d'un dépôt distant (cela effectue un fetch puis un merge)
git pull -r # met à jour ses fichiers locaux en effectuant un rebase
git push # envoie les modifications locales sur un dépôt distant
git push -f # force à envoyer ses modifications locales sur un dépôt distant. À utiliser avec précaution.
git stash # mettre de côté ses fichiers modifiés afin de pouvoir faire un pull
git stash pop # réintégrer les fichiers ayant été mis de côté par un stash
git reflog # afficher toutes les actions ayant été effectuées sur le dépôt
git reset HEAD [nom_du_fichier] # permet de retirer un fichier du stage (fichiers à committer) sans perdre ses modifications
git reset --hard HEAD@{1} # revenir à l'état spécifié (obtenu par reflog)
git reset --soft HEAD@{1} # revenir à l'état spécifié (obtenu par reflog), mais en gardant ses dernières modifications
git branch [branche] # créer une branche
git checkout [branche] # switcher d'une branche à une autre
git checkout [branche_distante] # récupère une branche distante (raccourci)
git checkout -b [branche] # crée une branche et fait un checkout sur cette dernière (raccourci)
git push origin [branche] # envoie cette branche sur le dépôt distant précisé
git branch -d [branche] # supprime une branche en local
git push origin :[branche_distante] # supprime une branche sur le dépôt distant
git branch --track [branche] origin/[nom_de_la_branche] # récupère une branche distante
git merge [branche] --no-ff # fusionne les modifications de la branche donnée sur la branche en cours, sans avance rapide
git log --pretty="%s" --no-merges v1..v2 # affiche les différences entre deux tags, à la ligne et sans les merge
git log --all --grep='my-feature' # retrouve les commits contenant le texte donné dans son message de commit
git log --pretty=oneline -S'some code'
git blame [fichier] -L 1,5 # affiche l'utilisateur ayant modifié les lignes 1 à 5 du fichier spécifié
git commit --amend -m "new commit name" # modifie le dernier commit (ne pas avoir poussé auparavant)
git fetch -p # après récupération des branches distantes, supprime les branches n'existant plus sur le dépôt distant
git cherry-pick [hash_du_commit] # permet de transférer un commit d'une branche à une autre
git cherry-pick [hash_du_commit_de_merge] -m 1 # permet de transférer un commit de merge d'une branche à une autre
git tag # affiche la liste des tags
git tag [nom_du_tag] # crée un tag (exemple : 1.0.0)
git push --tags # pousse les tags créés
git branch -m [nouveau_nom] # renomme la branche courante
git merge --squash [branche] # merge les commits provenant d'une autre branche en les regroupant en un seul commit
git tag -l | xargs git tag -d && git fetch -t # supprime les tags en local et récupère les remote tags
git push origin :refs/tags/[nom_du_tag] # supprime le tag distant
git clean -fd # supprime les fichiers et répertoires non trackés
git log --format=format: --name-only --since=12.month \ # fichiers les plus souvent modifiés depuis 1 an

Configuration

git config -e # édition de la configuration du projet
git config -e --global # édition de la configuration globale de Git
git config -l --global # liste la configuration globale de Git
git config --global user.name "John Doe" # paramétrage du nom de l'utilisateur
git config --global user.email johndoe@example.com # paramétrage de l'email de l'utilisateur
git config --global color.ui true # active la coloration au sein du terminal
git config --global push.default current # force à envoyer seulement la branche courante
git config --global core.editor vi # indique l'éditeur par défaut (messages de commit, résolution de conflits, édition de la configuration...)
git config --global pull.rebase true # rebase automatique lors d'un pull
git config --global help.autocorrect 1 # lance la commande en cas de typo s'il n'y a qu'une seule commande qui peut matcher
git config core.filemode false # les droits des fichiers ne seront pas pris en compte
git config core.excludesfile ~/.gitignore # exclure des fichiers sans polluer le .gitignore du projet

Visualisation des branches

[alias] lg = log --color --graph --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset" --abbrev-commit

ou

Editer votre fichier ~/.bashrc :

alias log='log --color --graph --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset" --abbrev-commit'
source ~/.bashrc

Signification des options de la commande git add -p

https://git-scm.com/book/en/v2/Git-Tools-Interactive-Staging

Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]?

  • y stage this hunk for the next commit
  • n do not stage this hunk the next commit
  • q quit; do not stage this hunk or any of the remaining ones
  • a stage this hunk and all later hunks in the file
  • d do not stage this hunk or any of the later hunks in the file
  • g select a hunk to go to
  • / search for a hunk matching the given regex
  • j leave this hunk undecided, see next undecided hunk
  • J leave this hunk undecided, see next hunk
  • k leave this hunk undecided, see previous undecided hunk
  • K leave this hunk undecided, see previous hunk
  • s split the current hunk into smaller hunks
  • e manually edit the current hunk
  • ? print help

Annuler un commit de merge

git revert -m 1 [hash_du_commit_de_merge]

Davantage d'informations ici : https://mijingo.com/blog/reverting-a-git-merge

Migration d'un dépôt

D'après https://www.atlassian.com/git/tutorials/git-move-repository
Le nouveau repository doit etre créé et vierge.

  1. Clone repo origine
git clone <url to ORI repo> temp-dir
  1. Voir toutes les branches à intégrer
cd temp-dir
git branch -a
  1. Pour chaque branche à repousser, checkout
git checkout branch-name
  1. Récuper les tags
git fetch --tags
  1. Vérification des branches et des tags
git tag
git branch -a
  1. Supprimer le lien au repo d'origine
git remote rm origin
  1. Lier au nouveau repo
git remote add origin <url to NEW repo>
  1. Pousser les tags et les branches
git push origin --all
git push --tags
  1. supprimer le temp-dir
rm temp-dir -rf

Outils divers

  • Git quick stats: Various statistics of a Git repository
  • gitk: The Git repository browser
  • Gource: A software version control visualization tool
  • Tig: Text-mode interface for Git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment