by @HelderDOliveira
¿Qué es?
● Control de versiones
● Distribuido
● Desarrollo no lineal multi-rama
● Fusión (merge) entre ramas
● Conexión http, ftp, rsync, tcp/ip, ssh
● Creado por Linus Torvalds
Instalación
● Descarga: http://git-scm.
com/downloads
● Pasos para la instalación: https://help.
github.com/articles/set-up-git/
Instalación en Windows y Linux
Windows
● Descargar el instalador y ejecutarlo
Linux Debian (Debian, Ubuntu, Elementary, ...)
● apt-get install git
Luego de instalar...
git config --global user.name "me"
git config --global user.email
"me@corp.com"
Conexión
● HTTPS (cacheable)
● SSH (por certificado)
Ejemplo de conexión
git clone -b rama -v ssh:
//svr/opt/gitroot/Project.git
-b : branch
-v: verbose
No más password...
git config --global
credential.helper cache
● git cachea el password
● útil por línea de comandos
● los IDE’s ya cachean el password
Ciclo de vida (I): entorno
mkdir workspace
cd workspace
touch readme.txt
Ciclo de vida (II): crear repositorio
git init
● ejecutarlo dentro
del proyecto
● se crea el directorio
.git dentro del proyecto
Estado
git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# readme.txt
nothing added to commit but untracked files present
(use "git add" to track)
Ciclo de vida (III): rastreando
git add readme.txt
Estado
git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: readme.txt
Ciclo de vida (IV): commit
git commit
Editando el mensaje de commit
Commit inicial del proyecto
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: readme.txt
#
Por defecto usa emacs (ctrl + X para salir y salvar)
No quiero emacs, quiero vi
git config --global core.editor
"vi"
Ciclo de vida (V): nuevo fichero
touch leeme.txt
Estado
git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# leeme.txt
Ciclo de vida (VI): editar el antiguo
echo reading > readme.txt
Estado
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: readme.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# leeme.txt
no changes added to commit (use "git add" and/or "git commit -a")
Ciclo de vida (VI): agregar
git add *.txt
Estado
git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: leeme.txt
# modified: readme.txt
#
Ciclo de vida (VII): commit
git commit
[master d209e96]
Segundo commit del proyecto
1 file changed, 1 insertion(+)
create mode 100644 leeme.txt
Estado
git status
# On branch master
nothing to commit
(working directory clean)
Viendo ramas
git branch
* master
Creando ramas
git branch dev
git branch
dev
* master
(se ha creado la rama, pero seguimos trabajando en la rama actual)
Moviéndose entre ramas
git checkout dev
Switched to branch 'dev'
git branch
* dev
master
Editando en ramas
echo "reading something else" > readme.txt
git status
# On branch dev
# Changes not staged for commit:
#
# modified: readme.txt
#
no changes added to commit (use "git add" and/or "git commit -
a")
Agregando en ramas
git add readme.txt
git status
# On branch dev
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: readme.txt
#
Commit en ramas
git commit
[dev e9ba758] Primer commit a la rama de desarrollo
1 file changed, 1 insertion(+), 1 deletion(-)
git status
# On branch dev
nothing to commit (working directory clean)
Comparando ramas
git diff master..dev
diff --git a/readme.txt b/readme.txt
index f3dd3f9..b41523f 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1 @@
-reading
+reading something else
(ha cambiado la metainformación del fichero y su contenido)
Viendo ramas
gitk
Merge de ramas
git checkout master
Switched to branch 'master'
git merge dev
Updating d209e96..e9ba758
Fast-forward
readme.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
git diff master..dev
more readme.txt
reading something else
¿Qué ha ocurrido en el merge?
1. nos hemos movido a la rama a la que queremos llevar
los cambios
2. hemos pedido a git que fusione los cambios de ambas
ramas
3. comparamos el contenido de ambas ramas y eran
iguales
4. comparamos el contenido de los ficheros y era el
esperado
Viendo las ramas
Ahora gitk muestra ambas ramas con el
mismo estado
Repositorios distribuidos
Podemos conectarnos a los repositorios vía
ssh, ftp, http, ruta de directorios, …
Caso de estudio
Vamos a simular un segundo repositorio
“workspace2” que use el actual “workspace”
como origen.
workspace workspace2
Clonando un proyecto
git clone workspace/ workspace2
Cloning into 'workspace2'...
done.
ls
workspace workspace2
Clonando un proyecto
ls -la workspace2
total 16
drwxr-xr-x 3 helder helder 4096 Dec 3 17:24 .
drwxrwxr-x 5 helder helder 4096 Dec 3 17:24 ..
drwxrwxr-x 8 helder helder 4096 Dec 3 17:24 .git
-rw-rw-r-- 1 helder helder 0 Dec 3 17:24 leeme.txt
-rw-rw-r-- 1 helder helder 23 Dec 3 17:24 readme.txt
Herencia
cd workspace
git remote
cd ../workspace2
git remote
origin
git remote show origin
* remote origin
Fetch URL: /home/workspace/
Push URL: /home/workspace/
Editando en el hijo
echo "I don't like to read" >> readme.txt
more readme.txt
reading something else
I don't like to read
Haciendo commit desde el hijo
git add readme.txt
git commit
[master 6c40dcc] Primer commit en el proyecto hijo
1 file changed, 1 insertion(+)
git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
Ahora el padre y el hijo están dispares
El padre también hace cambios
cd ../workspace
echo "vamos a leer algo" >> leeme.txt
echo "son..., LoL!" >> readme.txt
more readme.txt
reading something else
son..., LoL!
git add *.txt
git commit
[master 51013ba] Otro commit para forzar a mi hijo a
actualizar
2 files changed, 2 insertions(+)
pull vs fetch
Básicamente
pull = fetch + merge
con fetch podemos actualizar en nuestro
proyecto el estado de las ramas remotas
El hijo no conoce los cambios… aún
cd ../workspace2
git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)
Solo muestra lo que ya sabía, que el hijo ha hecho un commit, pero no
muestra los cambios posteriores del padre
El hijo se entera de los cambios
git fetch
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
From /home/helder/Documents/workspace_git_bbs/workspace
e9ba758..51013ba master -> origin/master
git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 1 and 1 different commit each, respectively.
El hijo se trae los cambios
git merge origin/master
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.
more readme.txt
reading something else
<<<<<<< HEAD
I don't like to read
=======
son..., LoL!
>>>>>>> origin/master
¿Qué ha ocurrido?
1. el hijo y el padre se encontraban en el
mismo estado. <Estado 1>
2. el hijo cambia algo y realiza un commit a su
propio repositorio. <Estado 1 + hijo>
3. el padre hace un cambio y hace un commit a
su propio repositorio. <Estado 2>
4. el hijo fusiona el contenido del padre en su
repositorio… <Estado 1 + hijo + Estado 2>
El hijo resuelve el conflicto
● El hijo, aunque con conflictos, ha realizado un merge.
● Ahora git ha dejado al hijo en el nuevo Estado 2.
vi readme.txt
git add readme.txt
git commit
[master ad1b709] Merge remote-tracking branch 'origin/master'
git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
git fetch
git merge origin/master
Already up-to-date.
Push
Por último, el hijo le da sus cambios al padre
para estar a la par:
git push
Counting objects: 10, done.
...
Unpacking objects: 100% (6/6), done.
remote: error: refusing to update checked out branch:
refs/heads/master
El padre aún no aceptaba cambios
Por defecto el padre no acepta cambios vía
push, para solventar este tema el padre puede
dar permisos de push:
cd ../workspace
git config --bool core.bare true
cd ../workspace2
git push
…
b8ca01c..f805606 master -> master
Para experimentar con git...
github.com
helder.de.oliveira.garrido@gmail.com

Git

  • 1.
  • 2.
    ¿Qué es? ● Controlde versiones ● Distribuido ● Desarrollo no lineal multi-rama ● Fusión (merge) entre ramas ● Conexión http, ftp, rsync, tcp/ip, ssh ● Creado por Linus Torvalds
  • 3.
    Instalación ● Descarga: http://git-scm. com/downloads ●Pasos para la instalación: https://help. github.com/articles/set-up-git/
  • 4.
    Instalación en Windowsy Linux Windows ● Descargar el instalador y ejecutarlo Linux Debian (Debian, Ubuntu, Elementary, ...) ● apt-get install git
  • 5.
    Luego de instalar... gitconfig --global user.name "me" git config --global user.email "me@corp.com"
  • 6.
  • 7.
    Ejemplo de conexión gitclone -b rama -v ssh: //svr/opt/gitroot/Project.git -b : branch -v: verbose
  • 8.
    No más password... gitconfig --global credential.helper cache ● git cachea el password ● útil por línea de comandos ● los IDE’s ya cachean el password
  • 9.
    Ciclo de vida(I): entorno mkdir workspace cd workspace touch readme.txt
  • 10.
    Ciclo de vida(II): crear repositorio git init ● ejecutarlo dentro del proyecto ● se crea el directorio .git dentro del proyecto
  • 11.
    Estado git status # Onbranch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # readme.txt nothing added to commit but untracked files present (use "git add" to track)
  • 12.
    Ciclo de vida(III): rastreando git add readme.txt
  • 13.
    Estado git status # Onbranch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: readme.txt
  • 14.
    Ciclo de vida(IV): commit git commit
  • 15.
    Editando el mensajede commit Commit inicial del proyecto # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: readme.txt # Por defecto usa emacs (ctrl + X para salir y salvar)
  • 16.
    No quiero emacs,quiero vi git config --global core.editor "vi"
  • 17.
    Ciclo de vida(V): nuevo fichero touch leeme.txt
  • 18.
    Estado git status # Onbranch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # leeme.txt
  • 19.
    Ciclo de vida(VI): editar el antiguo echo reading > readme.txt
  • 20.
    Estado git status # Onbranch 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: readme.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # leeme.txt no changes added to commit (use "git add" and/or "git commit -a")
  • 21.
    Ciclo de vida(VI): agregar git add *.txt
  • 22.
    Estado git status # Onbranch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: leeme.txt # modified: readme.txt #
  • 23.
    Ciclo de vida(VII): commit git commit [master d209e96] Segundo commit del proyecto 1 file changed, 1 insertion(+) create mode 100644 leeme.txt
  • 24.
    Estado git status # Onbranch master nothing to commit (working directory clean)
  • 25.
  • 26.
    Creando ramas git branchdev git branch dev * master (se ha creado la rama, pero seguimos trabajando en la rama actual)
  • 27.
    Moviéndose entre ramas gitcheckout dev Switched to branch 'dev' git branch * dev master
  • 28.
    Editando en ramas echo"reading something else" > readme.txt git status # On branch dev # Changes not staged for commit: # # modified: readme.txt # no changes added to commit (use "git add" and/or "git commit - a")
  • 29.
    Agregando en ramas gitadd readme.txt git status # On branch dev # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: readme.txt #
  • 30.
    Commit en ramas gitcommit [dev e9ba758] Primer commit a la rama de desarrollo 1 file changed, 1 insertion(+), 1 deletion(-) git status # On branch dev nothing to commit (working directory clean)
  • 31.
    Comparando ramas git diffmaster..dev diff --git a/readme.txt b/readme.txt index f3dd3f9..b41523f 100644 --- a/readme.txt +++ b/readme.txt @@ -1 +1 @@ -reading +reading something else (ha cambiado la metainformación del fichero y su contenido)
  • 32.
  • 33.
    Merge de ramas gitcheckout master Switched to branch 'master' git merge dev Updating d209e96..e9ba758 Fast-forward readme.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) git diff master..dev more readme.txt reading something else
  • 34.
    ¿Qué ha ocurridoen el merge? 1. nos hemos movido a la rama a la que queremos llevar los cambios 2. hemos pedido a git que fusione los cambios de ambas ramas 3. comparamos el contenido de ambas ramas y eran iguales 4. comparamos el contenido de los ficheros y era el esperado
  • 35.
    Viendo las ramas Ahoragitk muestra ambas ramas con el mismo estado
  • 36.
    Repositorios distribuidos Podemos conectarnosa los repositorios vía ssh, ftp, http, ruta de directorios, …
  • 37.
    Caso de estudio Vamosa simular un segundo repositorio “workspace2” que use el actual “workspace” como origen. workspace workspace2
  • 38.
    Clonando un proyecto gitclone workspace/ workspace2 Cloning into 'workspace2'... done. ls workspace workspace2
  • 39.
    Clonando un proyecto ls-la workspace2 total 16 drwxr-xr-x 3 helder helder 4096 Dec 3 17:24 . drwxrwxr-x 5 helder helder 4096 Dec 3 17:24 .. drwxrwxr-x 8 helder helder 4096 Dec 3 17:24 .git -rw-rw-r-- 1 helder helder 0 Dec 3 17:24 leeme.txt -rw-rw-r-- 1 helder helder 23 Dec 3 17:24 readme.txt
  • 40.
    Herencia cd workspace git remote cd../workspace2 git remote origin git remote show origin * remote origin Fetch URL: /home/workspace/ Push URL: /home/workspace/
  • 41.
    Editando en elhijo echo "I don't like to read" >> readme.txt more readme.txt reading something else I don't like to read
  • 42.
    Haciendo commit desdeel hijo git add readme.txt git commit [master 6c40dcc] Primer commit en el proyecto hijo 1 file changed, 1 insertion(+) git status # On branch master # Your branch is ahead of 'origin/master' by 1 commit. Ahora el padre y el hijo están dispares
  • 43.
    El padre tambiénhace cambios cd ../workspace echo "vamos a leer algo" >> leeme.txt echo "son..., LoL!" >> readme.txt more readme.txt reading something else son..., LoL! git add *.txt git commit [master 51013ba] Otro commit para forzar a mi hijo a actualizar 2 files changed, 2 insertions(+)
  • 44.
    pull vs fetch Básicamente pull= fetch + merge con fetch podemos actualizar en nuestro proyecto el estado de las ramas remotas
  • 45.
    El hijo noconoce los cambios… aún cd ../workspace2 git status # On branch master # Your branch is ahead of 'origin/master' by 1 commit. # nothing to commit (working directory clean) Solo muestra lo que ya sabía, que el hijo ha hecho un commit, pero no muestra los cambios posteriores del padre
  • 46.
    El hijo seentera de los cambios git fetch remote: Counting objects: 7, done. remote: Compressing objects: 100% (2/2), done. remote: Total 4 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (4/4), done. From /home/helder/Documents/workspace_git_bbs/workspace e9ba758..51013ba master -> origin/master git status # On branch master # Your branch and 'origin/master' have diverged, # and have 1 and 1 different commit each, respectively.
  • 47.
    El hijo setrae los cambios git merge origin/master Auto-merging readme.txt CONFLICT (content): Merge conflict in readme.txt Automatic merge failed; fix conflicts and then commit the result. more readme.txt reading something else <<<<<<< HEAD I don't like to read ======= son..., LoL! >>>>>>> origin/master
  • 48.
    ¿Qué ha ocurrido? 1.el hijo y el padre se encontraban en el mismo estado. <Estado 1> 2. el hijo cambia algo y realiza un commit a su propio repositorio. <Estado 1 + hijo> 3. el padre hace un cambio y hace un commit a su propio repositorio. <Estado 2> 4. el hijo fusiona el contenido del padre en su repositorio… <Estado 1 + hijo + Estado 2>
  • 49.
    El hijo resuelveel conflicto ● El hijo, aunque con conflictos, ha realizado un merge. ● Ahora git ha dejado al hijo en el nuevo Estado 2. vi readme.txt git add readme.txt git commit [master ad1b709] Merge remote-tracking branch 'origin/master' git status # On branch master # Your branch is ahead of 'origin/master' by 2 commits. git fetch git merge origin/master Already up-to-date.
  • 50.
    Push Por último, elhijo le da sus cambios al padre para estar a la par: git push Counting objects: 10, done. ... Unpacking objects: 100% (6/6), done. remote: error: refusing to update checked out branch: refs/heads/master
  • 51.
    El padre aúnno aceptaba cambios Por defecto el padre no acepta cambios vía push, para solventar este tema el padre puede dar permisos de push: cd ../workspace git config --bool core.bare true cd ../workspace2 git push … b8ca01c..f805606 master -> master
  • 52.
    Para experimentar congit... github.com
  • 53.