Skip to content

AllixIOT/learn_git

Repository files navigation

INSTALLAZIONE GIT

GIT download - Software GIT ufficiale.

Configurazione e setup primo utilizzo: git config

Da shell configura git digitando i comandi:

git config --global user.name "John Smith" 
git config --global user.email john@example.com

Plugin per Visual Studio

Plugin VisualStudio - plugin per abilitare GIT per Visual Studio 2008. Da Visual Studio 2015 esiste un plugin ufficile Microsoft.

CREAZIONE DI UN REPOSITORY LOCALE: git init

Richiama la git bash e all'interno della shell, crea una directory (es: learn_git)

mkdir learn_git

cd learn_git

Per inizializzare il repository locale, dall'interno quindi della directory di progetto:

git init

viene inizializzato così il repository locale. Eseguendo il comando all'interno della repository ls -la si notano la directory nascosta .git che contiene le informazioni del repository.

Esiste anche il comando git init <repo_name> per creare il repository: ammettiamo di essere in una directory chiamato worksapce, da qui digito il comando git init mio_repo; viene così creato il repository mio_repo sotto la directory workspace.

Vedi anche git clone

ANALISI REPOSITORY: git status

Comando git status

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)
$ git status
On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)

Versione abbreviata git status -s

$ git status -s
 M README
MM Rakefile
A  lib/git.rb
M  lib/simplegit.rb
?? LICENSE.txt

File nuovi che non sono tracciati hanno simbolo ??, file nuovi hanno A mentre file modificati hanno il simbolo M. Ci sono due colonne nell'output - la colonna di destra rappresenta lo status nella staging area mentre quella di destra lo status quello del working tree.

Per vedere i cambiamenti non ancora messi in stage: git diff

Per vderere quello che è in stage e andrà nella prossima commit: git diff --staged o sinonimo git diff --chached

SALVARE I CAMBIAMENTI AL REPOSITORY: git add e git commit

Per salvare nel repository un file o più file della working area è necessario:

  1. aggiungere le risorse da salvare nel repository all'area di staging: git add
  2. eseguire la commit: git commit

Aggiungere un file:

$ touch pippo.txt

di nuovo git status, cosa notate?

$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        pippo.txt

nothing added to commit but untracked files present (use "git add" to track)

Uso git add per aggiungerlo all'area di staging (o anche detta index)

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   pippo.txt


cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)

Per salvare nel repository locale le modifiche dell'area di staging, eseguire git commit

$ git commit
[master (root-commit) e0194cd]  Initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 pippo.txt

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)

esegui il comando git log per vedere lo storico delle commit o commit history

$ git log
commit e0194cd18b886ffed923d7bce4c5c0fbd4010e62
Author: Massimo Cappellano <Massimo.Cappe@gmail.com>
Date:   Mon Jun 26 20:55:12 2017 +0200

     Initial commit

     Changes to be committed:
            new file:   pippo.txt

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)
cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)
$ git log --oneline --all --decorate
e0194cd (HEAD -> master)  Initial commit

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)

Vedi per approfondire Commit history.

CANCELLAZIONE DEI FILE: git rm

Se un file è stato committato e lo si vuole cancellare.

Ad Esempio:

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/prova_test
$ git init
Initialized empty Git repository in D:/ALLIX/prova_test/.git/

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/prova_test (master)
$ touch PROJECT.txt

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/prova_test (master)
$ git add PROJECT.txt

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/prova_test (master)
$ git status -s
A  PROJECT.txt

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/prova_test (master)
$ git rm PROJECT.txt
error: the following file has changes staged in the index:
    PROJECT.txt
(use --cached to keep the file, or -f to force removal)

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/prova_test (master)
$ git commit -m "added file"
[master (root-commit) 66409c2] added file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 PROJECT.txt

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/prova_test (master)
$ git rm PROJECT.txt
rm 'PROJECT.txt'

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/prova_test (master)
$ ls

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/prova_test (master)
$ git status -s
D  PROJECT.txt

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/prova_test (master)
$ git commit -m "deleted file"
[master 93f7c60] deleted file
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 PROJECT.txt

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/prova_test (master)
$ git log --oneline --decorate
93f7c60 (HEAD -> master) deleted file
66409c2 added file

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/prova_test (master)

Nell'esempio viene aggiunto il file PROJECT.txt nell'area di stage (git add).

Se a questo punto (cioè prima di eseguire l'operazione di commit) si tentata l'operazione git rm del file che è nell'area di stage, git non lo permette perchè l'aggiunta file non è stata committata e quindi sarebbe in questo nodo persa. Lo permette se con l'opzione -f in cui si forza l'operazione o con l'opzione --cached che non cancella il file nella working tree ma solo dall'area di stage.

Eseguita la commit di aggiunta del file, eseguiamo git rm . Eseguendo il comando ls, vediamo che il file PROJECT.txt è stato cancellato dal working tree.

Eseguendo poi lo status, il messaggio indica che nell'area di staging il file è deleted.

Eseguiamo infine la commit per rendere persistente la modifica.

Perchè usare git rm invece del semplice rm

.gitignore

Se vogliamo che alcuni tipi di file siano ignorati da git, vanno inseriti nel file .gitignore da creare nella directory di progetto. Anche questo file in genere viene salvato nel repository.

Ad esempio in genere non vengono salvati gli eseguibili e quindi in .gitignore si nette una riga:


#.gitignore esempio

*.exe

In genere in file .gitignore si specificano directory e pattern di file.

# è usato per il commento su linea.

Qui esempio di un template di .gitignore per ambiente Visual Studio.

I file .gitignore si possono creare anche in sottodirectory e vengono in genere inseriti in caso si voglia creare una directory vuota che venga salvata nel repository; le directory vuote non vengono salvate da git, allora nella directory si crea un file .gitignore che essendo un file fa si che l'operazione commit salvi anche la directory vuota.

Es tipico, la directory di log: si vuole che la directory dei log sia presente sul repository ma non il contenuto, i file di log, che non devono essere salvati su repository.

CREAZIONE DI UN REPOSITORY REMOTO

Creazione account su GitHub https://github.com

Crea un repository public con un nome.

repo start

repo conf

Aggiunta al REPOSITORY LOCALE del REPOSITORY REMOTO: git remote add

Una volta creato un repository remoto, è necessario agganciarlo al repository locale:

git remote add <remote_name> <remote_repo_url>

<remote_name> in genere origin per convenzione

<remote_repo_url>, url del repository remoto

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)
$ git remote add origin https://github.com/MassimoCappellano/try_git_example.git

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)
$ git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 252 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
Branch master set up to track remote branch master from origin.
To https://github.com/MassimoCappellano/try_git_example.git
 * [new branch]      master -> master

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)

Ora controlla git remote

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)
$ git remote -v
origin  https://github.com/MassimoCappellano/try_git_example.git (fetch)
origin  https://github.com/MassimoCappellano/try_git_example.git (push)

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)

REPOSITORY to REPOSITORY COLLABORATION: git push

modifica file pippo.txt

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)
$ vim pippo.txt

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)
$

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)
$

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/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:   pippo.txt

no changes added to commit (use "git add" and/or "git commit -a")

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)
$ git commit -a -m "modified pippo"
[master 38039f5] modified pippo
 1 file changed, 2 insertions(+)
warning: LF will be replaced by CRLF in pippo.txt.
The file will have its original line endings in your working directory.

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)

Ora dice che il branch master è avanti di una commit rispetto a origin/master

Esecuzione git push

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)
$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 254 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/MassimoCappellano/try_git_example.git
   e0194cd..38039f5  master -> master

cam@DESKTOP-6FO16O4 MINGW64 /d/ALLIX/learn_git (master)

TIPS:

  • Compare local git branch with remote branch?
    Ad esempio vedere le differenze tra il master locale con quello remoto: git diff master origin/master

CLONAZIONE DI UN REPOSITORY REMOTO: git clone

Se un progetto è già presente su un repository centrale, è possibile con git clone <repo_url> ottenere una copia locale.

<repo_url> è l'indirizzo della repository remoto che si vuole clonare.

repo_clone

cam@DESKTOP-6FO16O4 MINGW64 /d/TEST
$ git clone https://github.com/MassimoCappellano/try_git_example.git
Cloning into 'try_git_example'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.

cam@DESKTOP-6FO16O4 MINGW64 /d/TEST

Possiblilità di clonare il progetto specificando una directory (da git help clone):

git clone [--template=<template_directory>]
	  [-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]
	  [-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>]
	  [--dissociate] [--separate-git-dir <git dir>]
	  [--depth <depth>] [--[no-]single-branch]
	  [--recursive | --recurse-submodules] [--[no-]shallow-submodules]
	  [--jobs <n>] [--] <repository> [<directory>]

git init vs git clone

git init e git clone possono essere facilmente confusi. Ad alto livello, entrambi possono essere utilizzati per inizializzare un repository. Tuttavia, git clone è dipendente da git init. git clone è usato per creare una copia di un repositoty esistente. Al suo interno git clone prima chiama git init per creare un nuovo repository, poi copia i dati dal repository esistente.

ESERVIZI DA SVOLGERE: 1° lezione

ESERCIZIO UNO

  • creare un repository locale;

  • committare delle risorse sul repository locale;

  • eseguire anche in diversi step più operazioni di add e di commit sul repository locale;

  • verificare con git log il contenuto;

  • creare un repository remoto su github (pubblico o privato non fa differenza);

  • pubblicare il contenuto del repository locale sul repository remoto;

  • verificare il contenuto da interfaccia web di github;

  • eseguire in un' altra directory l'operazione di clone del repository remoto;

TUTORIALS

https://www.atlassian.com/git/tutorials completare Getting Start

poi la parte Collaborating per lavorare con repositori remoti.

Un altro approccio per imparare git: Git from the Bottom Up

REFERENZE

https://www.atlassian.com/git

Pro Git Book

About

Corso di git - work in progress

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published