53
by @HelderDOliveira

Git

Embed Size (px)

Citation preview

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 en Windows y LinuxWindows● 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 "[email protected]"

Conexión● HTTPS (cacheable)

● SSH (por certificado)

Ejemplo de conexióngit 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 workspacecd workspacetouch readme.txt

Ciclo de vida (II): crear repositoriogit init

● ejecutarlo dentrodel proyecto

● se crea el directorio .git dentro del proyecto

Estadogit status# On branch master## Initial commit## Untracked files:# (use "git add <file>..." to include in what will be committed)## readme.txtnothing added to commit but untracked files present (use "git add" to track)

Ciclo de vida (III): rastreando

git add readme.txt

Estadogit 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 commitCommit 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

Estadogit 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

Estadogit 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.txtno changes added to commit (use "git add" and/or "git commit -a")

Ciclo de vida (VI): agregar

git add *.txt

Estadogit 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

Estadogit status

# On branch masternothing to commit (working directory clean)

Viendo ramas

git branch* master

Creando ramasgit branch dev

git branch dev* master

(se ha creado la rama, pero seguimos trabajando en la rama actual)

Moviéndose entre ramas

git checkout devSwitched to branch 'dev'

git branch* dev master

Editando en ramasecho "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 ramasgit add readme.txt

git status# On branch dev# Changes to be committed:# (use "git reset HEAD <file>..." to unstage)## modified: readme.txt#

Commit en ramasgit commit

[dev e9ba758] Primer commit a la rama de desarrollo 1 file changed, 1 insertion(+), 1 deletion(-)

git status# On branch devnothing to commit (working directory clean)

Comparando ramasgit diff master..dev

diff --git a/readme.txt b/readme.txtindex 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 ramasgitk

Merge de ramasgit checkout masterSwitched to branch 'master'

git merge devUpdating d209e96..e9ba758Fast-forward readme.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)

git diff master..dev

more readme.txtreading 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 ramasAhora gitk muestra ambas ramas con el mismo estado

Repositorios distribuidosPodemos conectarnos a los repositorios vía ssh, ftp, http, ruta de directorios, …

Caso de estudioVamos a simular un segundo repositorio “workspace2” que use el actual “workspace” como origen.

workspace workspace2

Clonando un proyectogit clone workspace/ workspace2Cloning into 'workspace2'...done.

lsworkspace workspace2

Clonando un proyectols -la workspace2total 16drwxr-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

Herenciacd workspacegit remote

cd ../workspace2git remoteorigin

git remote show origin* remote origin Fetch URL: /home/workspace/ Push URL: /home/workspace/

Editando en el hijoecho "I don't like to read" >> readme.txt

more readme.txtreading something elseI don't like to read

Haciendo commit desde el hijogit 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 cambioscd ../workspaceecho "vamos a leer algo" >> leeme.txtecho "son..., LoL\!" >> readme.txtmore 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 fetchBásicamente

pull = fetch + merge

con fetch podemos actualizar en nuestro proyecto el estado de las ramas remotas

El hijo no conoce los cambios… aúncd ../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 cambiosgit fetchremote: 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 cambiosgit 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.txtgit add readme.txtgit 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 fetchgit merge origin/master Already up-to-date.

PushPor ú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 cambiosPor defecto el padre no acepta cambios vía push, para solventar este tema el padre puede dar permisos de push:cd ../workspacegit config --bool core.bare truecd ../workspace2git push…b8ca01c..f805606 master -> master

Para experimentar con git...

github.com