Mémo Git

De WikiBR
(Redirigé depuis Memo)

Commandes git utiles au quotidien

  • HEAD : dernier commit
  • HEAD^ : avant-dernier
  • HEAD^^ : avant-avant-dernier

git status : montre les modifs entre le state actuel et le dernier commit

git diff : montre les modifs entre le state actuel et le dernier commit

git diff path/to/file : idem mais seulement pour le fichier specifie

git log : historique des commits

git add .  : ajoute tous les fichiers de . au 'index'

git commit : commit les fichiers du index et demande un message (pas recommandé car ouvre vim)

git commit -m "blablabla" : commit les fichiers du index avec le message "blablabla" (recommande, plus simple)

Soft reset = annuler le fait d'avoir commité

git reset HEAD^ : l'avant-dernier commit devient le dernier commit

Hard reset = revert back to a previous commit. Attention ça annihile tes modifs.

git reset --hard HEAD # nuke toutes les modifs qui ont été faites depuis le dernier commit

git pull

  • fast forward (no interleaving between local commits and pulled commits), or
  • merge without conflict, or
  • merge conflict

git push : un push est irréversible.

git branch : list local branchs

git checkout other_branch : aller sur une autre branche

git branch nouvelle_branche : crée une nouvelle branche (locale) (mais ne va pas dessus)

git branch -d branche_a_supprimer : supprime une branche

git checkout -b nouvelle_branche = git branch nouvelle_branche + git checkout nouvelle_branche

Tous les fichiers utilisés par git sont rassemblés dans le sous-dossier .git/. Pour dé-git-ifier un dossier, simplement supprimer le .git/.

.gitignore : fichier où spécifier les fichiers que git n'a pas à suivre (e.g. fichiers de config de projet d'eclipse>))

Pour les curieux

Autres version control systems

  • CVS : vieux
  • [SVN] (Subversion) : assez utilisé
  • Mercurial
  • Bazaar

Notions générales

Notation des commits :

  • HEAD : dernier commit
  • HEAD^ : avant-dernier
  • HEAD^^ : avant-avant-dernier
  • HEAD~2 : idem
  • d6d98923868578a7f38dea79833b56d0326fcba1 : indique un numéro de commit précis
  • d6d9892 : le même commit (autocomplete implicite)
  • tags

Note that merge commits may have more than one parent:

git show HEAD^1 # show the first parent of HEAD (same as HEAD^) git show HEAD^2 # show the second parent of HEAD

Vocabulaire

The object database is the rather elegant system used to store the history of your project-files, directories, and commits.

The index file is a cache of the state of a directory tree, used to create commits, check out working directories, and hold the various trees involved in a merge.

Blob stands for Binary Large Object. Each version of a file is represented by blob. A blob holds the file data but doesn’t contain any metadata about the file. It is a binary file, and in Git database, it is named as SHA1 hash of that file. In Git, files are not addressed by names. Everything is content-addressed.

Tree is an object, which represents a directory. A tree is a binary file that stores references to blobs and trees which are also named as SHA1 hash of the tree object.

Tag assigns a meaningful name with a specific version in the repository. A tag is a branch, which nobody intends to modify. Once a tag is created for a particular commit, even if you create a new commit, it will not be updated

HEAD is a pointer, which always points to the latest commit in the branch. Whenever you make a commit, HEAD is updated with the latest commit. The heads of the branches are stored in .git/refs/heads/directory.

Config

git config --global user.name "mon_pseudo"

git config --global user.email "moi@email.com"

Le fichier modifié par ces commandes et qui garde trace de ces paramètres est ~/.gitconfig

LF (\n) vs CRLF (\r\n)

GNU/Linux and Mac OS uses line-feed (LF), or new line as line ending character, while Windows uses line-feed and carriage-return (LFCR) combination to represent the line-ending character.

  • git config --global core.autocrlf true : configure the Git client to convert line endings to CRLF format while checking out, and convert them back to LF format during the commit operation. (Useful for Windows system)
  • git config --global core.autocrlf input : configure the Git client to convert line endings from CRLF to LF while performing the checkout operation. (Useful for GNU/Linux or Mac OS)

Examiner sa config

Examiner sa config globale : tout simplement aller voir ~/.gitconfig.

Examiner la config d'un repository : regarder dans le .git/, ou bien utiliser la commande git config git config --list git config --get remote.origin.url

Création / import / export de repository

git init

git clone [http[s]|ssh|ftp[s]|git]://host.xz[:port]/path/to/repo.git/

Pushing a new (local) repository to the server :

(user@local:~$) mkdir github_repo ; cd github_repo vim README.txt [...] vim blah.c [...] git init git add . git commit -m "Initial commit" git remote add origin https://github.com/kangralkar/testing_repo.git

  1. defines remote-repository shorthand "origin" to the value [the GitHub repository's URL]

git push --set-upstream origin master

  1. push local changes (on local branch master) to the remote repository "origin". push operation will ask for GitHub user name and password. After successful authentication, the operation will succeed.

remote repository shorthand

alice$ git remote add bob /home/bob/myrepo

Après avoir exécuté ça, on peut écrire "bob" à la place de "/home/bob/myrepo" partout

Voir les modifs

Voir les modifs locales

git status : montre les modifs entre le state actuel et le dernier commit

git diff : montre les modifs entre le state actuel et le dernier commit

git diff path/to/file : idem mais seulement pour le fichier spécifié

git log: historique des commits

git log -p : historique complet des modifs

git log --stat : historique résume des modifs

gitk : will show a nice graphical representation of the history.

git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7 : see the details about this commit

git show HEAD

git show experimental : see the tip of the "experimental" branch

Voir les modifs sur le serveur =

Alice can peek at what Bob did without merging first, using the "fetch" command; this allows Alice to inspect what Bob did, using a special symbol "FETCH_HEAD", in order to determine if he has anything worth pulling, like this:

alice$ git fetch /home/bob/myrepo master

alice$ git log -p HEAD..FETCH_HEAD

alice$ gitk -p HEAD..FETCH_HEAD

The range notation HEAD..FETCH_HEAD means "show everything that is reachable from the FETCH_HEAD but exclude anything that is reachable from HEAD". Alice already knows everything that leads to her current state (HEAD), and reviews what Bob has in his state (FETCH_HEAD) that she has not seen with this command.

Alice may want to view what both of them did since they forked. She can use three-dot form instead of the two-dot form:

alice$ git log HEAD...FETCH_HEAD

alice$ gitk HEAD...FETCH_HEAD

This means "show everything that is reachable from either one, but exclude anything that is reachable from both of them".

using remote-repository shorthand

alice$ git fetch bob : Unlike the longhand form, when Alice fetches from Bob using a remote repository shorthand set up with git remote, what was fetched is stored in a remote-tracking branch, in this case bob/master

alice$ git log -p master..bob/master : shows a list of all the changes that Bob made since he branched from Alice’s master branch.

Commits

git add path/to/file1 path/to/file2 ... : ajoute des fichiers au 'index' (~ version 'staging')

git add .  : ajoute tous les fichiers de . au 'index'

git commit : commit les fichiers du index et demande un message

git commit -m "blablabla" : commit les fichiers du index avec le message "blablabla"

git commit -a : = git add . + git commit

git commit --amend : modifie le dernier message de commit

Undo a commit

Soft reset = annuler le fait d'avoir commité

git reset HEAD^ # l'avant-dernier commit devient le dernier commit

Hard reset = revert back to a previous commit. Attention ça annihile des modifs.

git reset --hard HEAD # nuke toutes les modifs qui ont été faites depuis le dernier commit

git checkout path/to/file : restaurer un fichier tel qu’il était au dernier commit

Si on a déjà push au serveur, la seule option est de créer et de push un commit qui fait les modifs inverses (revert commit).

git revert 6261cc2 : crée un commit "inverse" du commit spécifié, i.e. qui revert les modifs (-- et le commit)

git revert 6261cc2 --no-commit : pareil, mais ne commit pas ces modifs inverses

Push and pull to server

git pull (= git fetch + git merge)

  • fast forward (no interleaving between local commits and pulled commits), or
  • merge without conflict, or
  • merge conflict

git push

Le changement vers le serveur doit être de type fast-forward car le serveur ne peut régler les conflits à votre place s’il y en a. Personne ne doit avoir fait un push avant vous depuis votre dernier pull. Le mieux est de s’assurer que vous êtes à jour en faisant un pull avant de faire un push. Si le push échoue, vous serez de toute façon invités à faire un pull. Un push est irréversible.

Branches

gestion locale

git branch : see local branches

git branch nouvelle_branche : crée une nouvelle branche (locale) (mais ne va pas dessus)

git checkout other_branch : aller sur une autre branche

git branch -d branche_a_supprimer : supprime une branche

git checkout -b nouvelle_branche = git branch nouvelle_branche + git checkout nouvelle_branche

git merge branche_a_merger : merge branche_a_merger vers (dans) la branche courante

Avant de changer de branche, tous les fichiers doivent être commités ou stashés. git status ne devrait afficher aucun fichier en cours de modification.

stash

git stash : stash toutes les modifs dans un stash (propre à chaque branche)

git stash apply : unstash les modifs de la stash (de la branche courante)

gestion des branches locales + branches sur le serveur

origin/branche_machin : désigne une branche sur le serveur (origin)

git branch -r : lister toutes les branches que le serveur connait

git push --set-upstream origin branche_locale : copier une branche créée en local sur le serveur

supprimer une branche sur le serveur : cf Google

Tags

git tag NOMTAG IDCOMMIT : tagger une version, i.e. donner un alias à un commit précis pour le référencer sous ce nom.

git push --tags : un tag n’est pas envoyé au serveur lors d’un push, il faut préciser l’option --tags pour que ce soit le cas.

git tag -d NOMTAG : supprime un tag déjà créé

Divers

git grep "TODO" (avec expressions régulières). avec -n : numéros de lignes. search any of the files it manages in your current directory

git grep "TODO" c82a22c39cbc32576f64f5c6b3f24b99ea8149c7 : search for strings in a specified version of your project .gitignore

Références

https://git-scm.com/docs/gittutorial (replicates man gittutorial) https://git-scm.com/docs/giteveryday (replicates man giteveryday) https://www.tutorialspoint.com/git/ https://openclassrooms.com/fr/courses/1233741-gerez-vos-codes-source-avec-git https://git-scm.com/book/en/v1/Getting-Started