$ mkdir Git $ cd Git $ git config --global user.name jehanproc $ git config --global user.email my.mail@int-evry.fr $ git config --global color.diff auto $ git config --global color.status auto $ git config --global color.branch auto $ git init Initialized empty Git repository in /mci/mci/login/Git/.git/ $ cat ~/.gitconfig [user] name = jehanproc email = my.mail@int-evry.fr [color] diff = auto status = auto branch = auto
$ git status # On branch master # # Initial commit # nothing to commit (create/copy files and use "git add" to track)
$ vim file1.txt $ git status # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # file1.txt nothing added to commit but untracked files present (use "git add" to track) $ git add file1.txt $ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: file1.txt
$ git commit -m "add initial file1.txt" [master (root-commit) b679adb] add initial file1.txt 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 file1.txt $ git status # On branch master nothing to commit (working directory clean)
$ vim file1.txt $ git status # On branch master # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: file1.txt # no changes added to commit (use "git add" and/or "git commit -a")
$ git diff diff --git a/file1.txt b/file1.txt index ce01362..94954ab 100644 --- a/file1.txt +++ b/file1.txt @@ -1 +1,2 @@ hello +world
commit -a = commit de toutes les modifications .
$ git commit -a -m "modification de file1.txt -> +world" [master 9058851] modification de file1.txt -> +world 1 files changed, 1 insertions(+), 0 deletions(-) $ git status # On branch master nothing to commit (working directory clean)
$ git log commit 9058851ef6738163117ada54a94dbfc04365061c Author: jehanproc <my.email@int-evry.fr> Date: Tue Apr 9 10:01:05 2013 +0200 modification de file1.txt -> +world commit b679adb3b2e2fa5e30d1f52b2fa4f12cff888b66 Author: jehanproc <my.email@int-evry.fr> Date: Tue Apr 9 09:51:01 2013 +0200 add initial file1.txt
$ git reset --hard HEAD^ HEAD is now at b679adb add initial file1.txt $ cat file1.txt hello
par defaut, une seule branche *master*
$ git branch * master
creation d'un branche *option1* , ne modifie en rien les fichiers (pas de recopie)
$ git branch option1 $ git branch * master option1 $ ls -al total 52 -rw-r--r-- 1 login mci 6 9 avril 10:11 file1.txt drwxr-xr-x 8 login mci 4096 9 avril 10:11 .git
passage sur la branche option1 , git checkout est utilisé pour changer de branche mais aussi pour restaurer un fichier tel qu’il était lors du dernier commit. La commande a donc un double usage.
$ git checkout option1 Switched to branch 'option1' $ git branch master * option1 $ ls -al total 52 -rw-r--r-- 1 login mci 6 9 avril 10:11 file1.txt drwxr-xr-x 8 login mci 4096 9 avril 10:28 .git
$ vim file1.txt $ cat file1.txt hello wild $ git commit -a -m "ajout l3 wild dans file1" [option1 9c17567] ajout l3 wild dans file1 1 files changed, 2 insertions(+), 0 deletions(-) $ git status # On branch option1 nothing to commit (working directory clean) $ git log commit 9c17567714d9158126d39d3b1dda20ebe5146d82 Author: jehanproc <my.email@int-evry.fr> Date: Tue Apr 9 11:18:36 2013 +0200 ajout l3 wild dans file1 commit b679adb3b2e2fa5e30d1f52b2fa4f12cff888b66 Author: jehanproc <my.email@int-evry.fr> Date: Tue Apr 9 09:51:01 2013 +0200 add initial file1.txt
$ git checkout master Switched to branch 'master' $ git log commit b679adb3b2e2fa5e30d1f52b2fa4f12cff888b66 Author: jehanproc <my.email@int-evry.fr> Date: Tue Apr 9 09:51:01 2013 +0200 add initial file1.txt $ cat file1.txt hello
modification
$ vim file1.txt $ cat file1.txt hello world $ git commit -a -m "add l2 +world file1" [master 5d03c08] add l2 +world file1 1 files changed, 1 insertions(+), 0 deletions(-)
on merge option1 dans master
$ git merge option1 Auto-merging file1.txt CONFLICT (content): Merge conflict in file1.txt Automatic merge failed; fix conflicts and then commit the result.
constat du conflit
$ cat file1.txt hello <<<<<<< HEAD world ======= wild >>>>>>> option1
gestion du conflit
$ vim file1.txt $ cat file1.txt hello world wild
commit
$ git commit -a -m "marge master et option1, resolution conflit file1" [master 8fedaa6] marge master et option1, resolution conflit file1 $ git status # On branch master nothing to commit (working directory clean)
Destruction de branche .
$ git branch -d option1 Deleted branch option1 (was 9c17567).
cette section concerne l'administrateur reseau de repositories GIT
echange de clé avant “fermeture” du shell gituser
$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/gituser/.ssh/id_rsa): Created directory '/home/gituser/.ssh'. [login@arvouin ~/.ssh] $ ssh-copy-id gituser@svnshare.it-sudparis.eu
# yum install git # useradd -m -d /home/gituser gituser # usermod -s /usr/bin/git-shell gituser # tail -2 /etc/shells /bin/csh /usr/bin/git-shell # mkdir git-shell-commands # chmod 755 git-shell-commands # cp /usr/share/doc/git-1.7.4.1/contrib/git-shell-commands/* /home/gituser/git-shell-commands/ # chown -R gituser /home/gituser/git-shell-commands/* # chmod +x /home/gituser/git-shell-commands/list /home/gituser/git-shell-commands/help
au sujet de git-shell-commands cf http://serverfault.com/questions/285324/git-shell-not-enabled/325484#325484
on creer un repository vierge sur le serveur
# su -s /bin/bash - gituser $ mkdir gittuto.git $ cd gittuto.git/ $ git --bare init Initialized empty Git repository in /home/gituser/gittuto.git/
sur le client on declare notre serveur remote qui va heberger notre repo en reseau (protocole ssh)
syntaxe: git remote add [nomcourt] [url]
$ git remote add tuto ssh://gituser@gitshare.tem-tsp.eu/~/gittuto.git
verification de la declaration et des repo remote disponibles
$ git remote -v tuto ssh://gituser@gitshare.tem-tsp.eu/~/gittuto.git (fetch) tuto ssh://gituser@gitshare.tem-tsp.eu/~/gittuto.git (push)
on pousse alors notre depot (branche master) local vers le serveur
$ git push tuto master Counting objects: 12, done. Delta compression using up to 2 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (12/12), 917 bytes, done. Total 12 (delta 1), reused 0 (delta 0) To ssh://gituser@gitshare.tem-tsp.eu/~/gittuto.git * [new branch] master -> master
sur le serveur le directory “objects” a evolué
[gituser@gitshare gittuto.git]$ ls -ltra drwxrwxr-x 4 gituser gituser 4096 Apr 9 14:54 refs drwxrwxr-x 2 gituser gituser 4096 Apr 9 14:54 info drwxrwxr-x 2 gituser gituser 4096 Apr 9 14:54 hooks -rw-rw-r-- 1 gituser gituser 23 Apr 9 14:54 HEAD -rw-rw-r-- 1 gituser gituser 73 Apr 9 14:54 description -rw-rw-r-- 1 gituser gituser 66 Apr 9 14:54 config drwxrwxr-x 2 gituser gituser 4096 Apr 9 14:54 branches drwxrwxr-x 7 gituser gituser 4096 Apr 9 14:54 . drwxrwxr-x 16 gituser gituser 4096 Apr 9 15:08 objects
depuis un (autre) client (teststud) on va recuperer ce repo et le modifier
$ ssh-copy-id gituser@gitshare.tem-tsp.eu $ git clone ssh://gituser@gitshare.tem-tsp.eu/~/gittuto.git Cloning into 'gittuto'... remote: Counting objects: 12, done. remote: Compressing objects: 100% (4/4), done. remote: Total 12 (delta 1), reused 0 (delta 0) Receiving objects: 100% (12/12), done. Resolving deltas: 100% (1/1), done. -bash-4.2$ ls gittuto -bash-4.2$ cd gittuto/ -bash-4.2$ ls file1.txt -bash-4.2$ git remote -v origin ssh://gituser@gitshare.tem-tsp.eu/~/gittuto.git (fetch) origin ssh://gituser@gitshare.tem-tsp.eu/~/gittuto.git (push)
modifications et ajouts locaux
-bash-4.2$ vim file2.txt -bash-4.2$ git add file2.txt -bash-4.2$ git commit -a -m "add file2" [master 9287d44] add file2 1 files changed, 2 insertions(+), 0 deletions(-) create mode 100644 file2.txt
on envoie sur le serveur nos modifications
-bash-4.2$ git push Counting objects: 4, done. Delta compression using up to 2 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 307 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To ssh://gituser@gitshare.tem-tsp.eu/~/gittuto.git 8fedaa6..9287d44 master -> master
sur le serveur, c'est encore le directory “objects” qui viens d'etre modifié
[root@gitshare gittuto.git]# ls -ltr | tail -2 drwxrwxr-x 2 gituser gituser 4096 Apr 9 14:54 branches drwxrwxr-x 19 gituser gituser 4096 Apr 9 15:26 objects
depuis notre premier client nous allons recuperer les modifications faites par le second client (teststud)
$ git remote show tuto * remote tuto Fetch URL: ssh://gituser@gitshare.tem-tsp.eu/~/gittuto.git Push URL: ssh://gituser@gitshare.tem-tsp.eu/~/gittuto.git HEAD branch: master Remote branch: master tracked Local ref configured for 'git push': master pushes to master (local out of date)
on note le ”(local out of date)” !.
on recupere (pull) depuis le serveur les dernieres modifications du repo “tuto” dans notre branche “master”
$ git pull tuto master From ssh://gitshare.tem-tsp.eu/~/gittuto * branch master -> FETCH_HEAD Updating 8fedaa6..9287d44 Fast-forward file2.txt | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-) create mode 100644 file2.txt $ ls file1.txt file2.txt
l'objectif est de centraliser le dépôt sur un serveur gitlab de nos fichiers sources locaux
exemple de creation/initialisation d'une arboresence git sur nos fichiers sources locaux (generation du .git)
[teststud@elaphe projet-demo-git]$git init Dépôt Git vide initialisé dans /mci/disi/teststud/Progs/projet-demo-git/.git/
il convient de disposer d'une clée SSH pour echanger en shell avecle serveur gitlab afin d'eviter tout echange de login/passwords .
on génère alors une clée scpecifique pour le serveur gitlab visé, il conviendra ensuite de la declaré sur notre profile sur le serveur .
$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/user/proc/.ssh/id_rsa): /mci/disi/teststud/.ssh/id_rsa_gitlab18 Enter passphrase (empty for no passphrase):
On déclare alors dans notre configuration ssh (/config) une association entre cette nouvelle clé et le hostname du serveur gitlab (le user git est générique)
$ cat ~/.ssh/config host gitlab18.imtbs-tsp.eu HostName gitlab18.imtbs-tsp.eu IdentityFile ~/.ssh/id_rsa_gitlab18 User git
Enfin sur l'interface web du serveur gitlab, il faut deposer notre clée dans notre profile :
git add * git commit -m "add all files local"
nous ajoutons un serveur d'hebergement gitlab a notre repository local
[teststud@elaphe projet-demo-git]$git remote add gitdemo-gitlab18 git@gitlab18.imtbs-tsp.eu:teststud/git-demo.git
visualisation des repodistories distants avec lesquels notre arborescence locale est associée
[teststud@elaphe projet-demo-git]$git remote -v gitdemo-gitlab18 git@gitlab18.imtbs-tsp.eu:teststud/git-demo.git (fetch) gitdemo-gitlab18 git@gitlab18.imtbs-tsp.eu:teststud/git-demo.git (push)
depose de nos fichiers locaux sur le depos distant avec enregistrement initial de la clé publique du serveur gitlab .
[teststud@elaphe projet-demo-git]$git push gitdemo-gitlab18 master The authenticity of host 'gitlab18.imtbs-tsp.eu (157.159.10.68)' can't be established. ECDSA key fingerprint is SHA256:XJlCXEvrN/K4UvzHX0Spj0xYDX67z8C+2dpx1Gk4QEU. ECDSA key fingerprint is MD5:87:ea:2a:73:a0:98:a1:d6:1a:dc:c5:00:e0:b7:02:bf. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'gitlab18.imtbs-tsp.eu,157.159.10.68' (ECDSA) to the list of known hosts. Décompte des objets: 4, fait. Delta compression using up to 8 threads. Compression des objets: 100% (2/2), fait. Écriture des objets: 100% (4/4), 298 bytes | 149.00 KiB/s, fait. Total 4 (delta 0), reused 0 (delta 0) remote: remote: The private project teststud/git-demo was successfully created. remote: remote: To configure the remote, run: remote: git remote add origin git@gitlab18.imtbs-tsp.eu:teststud/git-demo.git remote: remote: To view the project, visit: remote: http://gitlab18.imtbs-tsp.eu/teststud/git-demo remote: To gitlab18.imtbs-tsp.eu:teststud/git-demo.git * [new branch] master -> master
depuis un autre client/terminal (simulation d'un travail collaboratif) on recupere le projetfraichement déposé et on le modifie localement + push final .
[teststud@anaconda Progs]$mkdir Gitlab [teststud@anaconda Progs]$cd Gitlab/ [teststud@anaconda Gitlab]$git clone git@gitlab18.imtbs-tsp.eu:teststud/git-demo.git Clonage dans 'git-demo'... remote: Enumerating objects: 4, done. remote: Counting objects: 100% (4/4), done. remote: Compressing objects: 100% (2/2), done. remote: Total 4 (delta 0), reused 0 (delta 0) Réception d'objets: 100% (4/4), fait.
modification locale
[teststud@anaconda Gitlab]$cd git-demo/ [teststud@anaconda git-demo]$ls file1.txt file2.txt [teststud@anaconda git-demo]$vim file2.txt [teststud@anaconda git-demo]$git commit -am "change comment in file2.txt TS" [master 091050d] change comment in file2.txt TS Committer: Compte Test TESTSTUD <teststud@anaconda.int-evry.fr> 1 file changed, 1 insertion(+)
on constate que notre modification locale est maintenant en avance sur le serveur
[teststud@anaconda git-demo]$git remote update Récupération de origin [teststud@anaconda git-demo]$git status Sur la branche master Votre branche est en avance sur 'origin/master' de 1 commit. (utilisez "git push" pour publier vos commits locaux) rien à valider, la copie de travail est propre [teststud@anaconda git-demo]$git whatchanged origin/master -n 1 commit 9ff2aaf20f09bf4d3cb3ac1fd24e4aa84debe1dc (origin/master, origin/HEAD) Author: Compte Test TESTSTUD <teststud@elaphe.int-evry.fr> Date: Mon Sep 10 10:35:53 2018 +0200 add all files local :000000 100644 0000000 d367f44 A file1.txt :000000 100644 0000000 1746142 A file2.txt
on pousse nos modifications sur le serveur
[teststud@anaconda git-demo]$git push origin master Décompte des objets: 3, fait. Delta compression using up to 32 threads. Compression des objets: 100% (2/2), fait. Écriture des objets: 100% (3/3), 337 bytes | 112.00 KiB/s, fait. Total 3 (delta 0), reused 0 (delta 0) To gitlab18.imtbs-tsp.eu:teststud/git-demo.git 9ff2aaf..091050d master -> master
si on retourne sur une autre PC/machine/terminal qui dispose d'une autre arborescence (hors NFS salles TP) , on verifie l'etat des lieux de notre repertoire local:
[teststud@elaphe projet-demo-git]$git remote show gitdemo-gitlab18 * distante gitdemo-gitlab18 URL de rapatriement : git@gitlab18.imtbs-tsp.eu:teststud/git-demo.git URL push : git@gitlab18.imtbs-tsp.eu:teststud/git-demo.git Branche HEAD : master Branche distante : master suivi Référence locale configurée pour 'git push' : master pousse vers master (le local n'est pas à jour)
“le locale n'est pas à jour”
on recupere alors les modifications remote en local
[teststud@elaphe projet-demo-git]$git branch -a * master remotes/gitdemo-gitlab18/master [teststud@elaphe projet-demo-git]$git pull gitdemo-gitlab18 master Depuis gitlab18.imtbs-tsp.eu:teststud/git-demo * branch master -> FETCH_HEAD Mise à jour 9ff2aaf..091050d Fast-forward file2.txt | 1 + 1 file changed, 1 insertion(+) [teststud@elaphe projet-demo-git]$cat file2.txt 2eme fichier modif from anaconda