git – started



git – started

0 0


git-started

Presentación corta de introducción a Git.

On Github andres1905 / git-started

git

started

git

sistema distribuido de control de versiones.

  • veloz y eficiente
  • bajo impacto y alto rendimiento
  • operaciones locales

trabajando localmente

\o/

command line #FTW

¿...o no?

(・・?

git init

Crea un repositorio nuevo...

						
git-example user$ git init
Initialized empty Git repository in /tmp/git-example/.git/
						
					

o reinicia uno existente

						
git-example user$ git init
Reinitialized existing Git repository in /tmp/git-example/.git/
						
					
						
git-example user$ touch a.txt
git-example user$ touch b.txt
git-example user$ touch c.txt
						
					

git status

Muestra el estado del repositorio y los cambios realizados en él.

						
git-example user$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	a.txt
#	b.txt
#	c.txt
nothing added to commit but untracked files present (use "git add" to track)
						
					

git add

Prepara la ejecución del siguiente commit agregando archivos al stage.

  • git add .
  • git add file.ext other.ext ...

Luego, al verificar el estado:

						
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   a.txt
#	new file:   b.txt
#	new file:   c.txt
						
					

git commit

Guarda los cambios del stage con un mensaje del usuario.

En un editor de texto (como vi):

						
Adding initial files to my first commit 

# 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:   a.txt
#       new file:   b.txt
#       new file:   c.txt
						
					
						
[master (root-commit) 8ed6a6f] Adding initial files to my first commit
 3 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a.txt
 create mode 100644 b.txt
 create mode 100644 c.txt
						
					
						
git-example user$ rm a.txt
git-example user$ 
						
					

Al verificar el estado:

						
# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	deleted:    a.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
						
					

git add --all

Agrega al stage archivos controlados, que pueden ya no estar disponibles (como los eliminados).

  • git add -A
  • git add -A file.ext other.ext ...

Luego, al verificar el estado:

						
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	deleted:    a.txt
						
					

git commit -m ""

						
git-example user$ git commit -m "Removing wrong file."
[master 3010f10] Removing wrong file.
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 a.txt
						
					
						
git-example user$ echo "B" > b.txt
git-example user$ touch d.txt
git-example user$ 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:   b.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	d.txt
no changes added to commit (use "git add" and/or "git commit -a")
						
					

git commit -am ""

ó "matar dos pájaros de un tiro".

						
git-example user$ git commit -am "Adding B content and d file."
[master 8cc0a4c] Adding B content and d file.
 1 file changed, 1 insertion(+)
						
					
						
git-example user$ git status
						
						
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	d.txt
nothing added to commit but untracked files present (use "git add" to track)
						
					

(~_~;)

pero... no se suponía que...

						
git-example user$ git add .
git-example user$ git status
						
						
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	new file:   d.txt
						
					

git commit --amend

						
[master 09b9cbb] Adding B content and d file.
 2 files changed, 1 insertion(+)
 create mode 100644 d.txt
						
					
						
git-example user$ echo "C" > c.txt
git-example user$ touch e.txt
git-example user$ git add .
git-example user$ git commit -m "Adding e file and writing C on c.txt"
						
						
[master a4cc3a3] Adding e file and writing C on c.txt
 2 files changed, 1 insertion(+)
 create mode 100644 e.txt
						
					
"El pueblo que no conoce su historia está condenado a repetirla"

cierto, aunque fuera de contexto en este caso, algo similar aplica...

git log

						
commit a4cc3a36c5b491ad222e6d95ab1a386c5aac25c5
Author: user <user@server.com>
Date:   Tue Mar 4 19:47:18 2014 -0500

    Adding e file and writing C on c.txt

commit 09b9cbb24a27a4b3084d1fba2b574c8b2b1876ee
Author: user <user@server.com>
Date:   Tue Mar 4 19:14:16 2014 -0500

    Adding B content and d file.

commit 3010f10efae970bddf07de012b9d4675112827cb
Author: user <user@server.com>
Date:   Tue Mar 4 19:00:30 2014 -0500

    Removing wrong file.

commit 8ed6a6f8e5cdebf31699df03870e69725f919b87
Author: user <user@server.com>
Date:   Tue Mar 4 18:31:48 2014 -0500

    Adding initial files to my first commit
						
					

no quería saber tanto...

git log --oneline

						
a4cc3a3 Adding e file and writing C on c.txt
09b9cbb Adding B content and d file.
3010f10 Removing wrong file.
8ed6a6f Adding initial files to my first commit
						
					

¡uy! ¡este proyecto tiene muchos commits!

-n

  • git log -n 2
git log --oneline -n 2
						
git-example user$ git log -n 2
commit a4cc3a36c5b491ad222e6d95ab1a386c5aac25c5
Author: user <user@server.com>
Date:   Tue Mar 4 19:47:18 2014 -0500

    Adding e file and writing C on c.txt

commit 09b9cbb24a27a4b3084d1fba2b574c8b2b1876ee
Author: user <user@server.com>
Date:   Tue Mar 4 19:14:16 2014 -0500

    Adding B content and d file.
						
					

y... ¿cómo sé que ha pasado con un archivo?

--

  • git log -- b.txt
git log --oneline -- b.txt
						
git-example user$ git log --oneline -- b.txt
09b9cbb Adding B content and d file.
8ed6a6f Adding initial files to my first commit
						
					

git show

Muestra el contenido del commit más reciente.

						
commit a4cc3a36c5b491ad222e6d95ab1a386c5aac25c5
Author: user <user@server.com>
Date:   Tue Mar 4 19:47:18 2014 -0500

    Adding e file and writing C on c.txt
						
diff --git a/c.txt b/c.txt
index e69de29..3cc58df 100644
--- a/c.txt
+++ b/c.txt
@@ -0,0 +1 @@
+C
diff --git a/e.txt b/e.txt
new file mode 100644
index 0000000..e69de29
						
					

¿y los detalles de un commit más antiguo?

git show <commit-id>

						
git-example user$ git show 09b9cbb
commit 09b9cbb24a27a4b3084d1fba2b574c8b2b1876ee
Author: user <user@server.com>
Date:   Tue Mar 4 19:14:16 2014 -0500

    Adding B content and d file.

diff --git a/b.txt b/b.txt
index e69de29..223b783 100644
--- a/b.txt
+++ b/b.txt
@@ -0,0 +1 @@
+B
diff --git a/d.txt b/d.txt
new file mode 100644
index 0000000..e69de29
						
					

¡bacán! pero solo me interesan los archivos afectados...

git show <id> --name-only

						
git-example user$ git show 09b9cbb --name-only
commit 09b9cbb24a27a4b3084d1fba2b574c8b2b1876ee
Author: user <user@server.com>
Date:   Tue Mar 4 19:14:16 2014 -0500

    Adding B content and d file.

b.txt
d.txt
						
					

ufff... y ¿qué tal ver los cambios de un archivo en un commit específico?

git show <id> -- file.ext

						
git-example user$ git show 09b9cbb -- b.txt
commit 09b9cbb24a27a4b3084d1fba2b574c8b2b1876ee
Author: user <user@server.com>
Date:   Tue Mar 4 19:14:16 2014 -0500

    Adding B content and d file.

diff --git a/b.txt b/b.txt
index e69de29..223b783 100644
--- a/b.txt
+++ b/b.txt
@@ -0,0 +1 @@
+B
						
					

para ir terminando...

						
git-example user$ echo "DDDDDD" > c.txt
git-example user$ echo "BBBB" > b.txt
git-example user$ git add .
git-example user$ git status
						
						
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	modified:   b.txt
#	modified:   c.txt
						
					

¡oh no! ¡"DDDDDD" no puede ir en c.txt!

git reset HEAD c.txt

						
Unstaged changes after reset:
M	c.txt
						
					

git checkout c.txt

al revisar el estado, c.txt ya no está modificado

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

¡fiu! ahora si..., el último commit

						
git-example user$ git commit -m "B.txt content set to BBBB"
[master fb4292f] B.txt content set to BBBB
 1 file changed, 1 insertion(+), 1 deletion(-)
						
					

aunque... mmm... este cambio debería ser parte de un commit anterior...

						
git log --oneline -- b.txt
fb4292f B.txt content set to BBBB
09b9cbb Adding B content and d file.
8ed6a6f Adding initial files to my first commit
						
					

git rebase -i HEAD~4

						
pick 3010f10 Removing wrong file.
pick 09b9cbb Adding B content and d file.
pick a4cc3a3 Adding e file and writing C on c.txt
pick fb4292f B.txt content set to BBBB

# Rebase 8ed6a6f..fb4292f onto 8ed6a6f
#
# Commands:
...
						
					

reordenamos los commits: fb4292f justo después de 09b9cbb

						
pick 3010f10 Removing wrong file.
pick 09b9cbb Adding B content and d file.
pick fb4292f B.txt content set to BBBB
pick a4cc3a3 Adding e file and writing C on c.txt
...
						
					

cambiar la instrucción pick de fb4292f por squash o fixup.

						
pick 3010f10 Removing wrong file.
pick 09b9cbb Adding B content and d file.
squash fb4292f B.txt content set to BBBB
pick a4cc3a3 Adding e file and writing C on c.txt
...
						
					

luego de ordenar los commits, confirmar los cambios a realizar:

						
# This is a combination of 2 commits.
# The first commit's message is:

Adding B content and d file.

# This is the 2nd commit message:

B.txt content set to BBBB
...
						
					

rebase termina de efectuar los cambios

						
[detached HEAD 9d30a2e] Adding B content and d file.
 2 files changed, 1 insertion(+)
 create mode 100644 d.txt
Successfully rebased and updated refs/heads/master.
						
					

comprobar las modificaciones efectuadas.

						
git-example user$ git log --oneline
32397d1 Adding e file and writing C on c.txt
9d30a2e Adding B content and d file.
3010f10 Removing wrong file.
8ed6a6f Adding initial files to my first commit
						
					

los dos commits utilizados ya no se encuentran, y en su lugar aparece uno nuevo: 9d30a2e

verificar que en el commit 9d30a2e el contenido "BBBB" fue almacenado en b.txt.

						
git-example user$ git show 9d30a2e -- b.txt
commit 9d30a2ec950ff9a376c0e405807fc898d6a94b37
Author: user <user@server.com>
Date:   Tue Mar 4 19:14:16 2014 -0500

    Adding B content and d file.
    
    B.txt content set to BBBB

diff --git a/b.txt b/b.txt
index e69de29..a07e243 100644
--- a/b.txt
+++ b/b.txt
@@ -0,0 +1 @@
+BBBB
						
					

(^ _ ^)/

Carlos Andrés Oquendocoquendo@thoughtworks.com@andres19_05