Git Intro

Qué es Git?

Git es un sistema de control de version

Distribuido,

Rápido,

y de Código Abierto

Distribuido

  • Similar a Mercurial, Bazaar, Darcs
  • Todos los clones son un mirror completo del repositorio
  • La mayoría de las operaciones son locales (y rápidas)

Rápido

  • La mayoría de las operaciones son locales
  • Las branches son simples referencias
  • Pensado para manejar miles de branches en paralelo
  • Capaz de manejar grandes proyectos como el Kernel de linux

Código Abierto

Git fue creado por Linus Torvalds (quién?) en 2005 para el desarrollo del kernel de linux.

 

El mantenimiento de Git está actualmente supervisado Junio Hamano, quien recibe contribuciones al código de alrededor de 280 programadores (gracias Wikipedia)

Workflow

Manos a la obra

Configuración inicial

» git config --global user.name "Darío"
» git config --global user.email "des@riseup.net"

» cat ~/.gitconfig|head                          
[user]
        email = des@riseup.net
        name = Darío

Creando un Repo

» git init
Initialized empty Git repository in /storage/Playground/git-intro/.git/

» tree -a
└── .git
    ├── branches
    ├── config
    ├── description
    ├── HEAD
    ├── hooks
    │   ├── applypatch-msg.sample
...
    │   └── update.sample
    ├── info
    │   └── exclude
    ├── objects
    │   ├── info
    │   └── pack
    └── refs
        ├── heads
        └── tags

10 directories, 13 files

Desde un repositorio existente

» git clone git@github.com:desyncr/vimrc.git
Cloning into 'vimrc'...
remote: Counting objects: 149, done.
remote: Total 149 (delta 0), reused 0 (delta 0), pack-reused 149
Receiving objects: 100% (149/149), 2.22 MiB | 519.00 KiB/s, done.
Resolving deltas: 100% (82/82), done.
Checking connectivity... done.      

Clones? Remotes? Upstream?

» git remote -v
origin  git@github.com:desyncr/vimrc.git (fetch)
origin  git@github.com:desyncr/vimrc.git (push)

Working dir / Staging / HEAD

» gs           
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

» touch HELLO

» gs
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        HELLO

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

» gs           
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   HELLO
                                   
» git commit -m 'Hello!'
[master d15589c] Hello!
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 HELLO                  

» gs                    
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 directory clean  

Branching!

Manejo de Branches

» git branch feature/update-readme

» git branch -l                   
  feature/update-readme
* master    

» git checkout feature/update-readme 
Switched to branch 'feature/update-readme'
» git checkout -b feature/update-readme
Switched to a new branch 'feature/update-readme'

Creación de Branches

» git merge feature/update-readme          
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
» git merge feature/update-readme -X ours
Merge made by the 'recursive' strategy.   
» git merge feature/update-readme -X theirs
Auto-merging README.md
Merge made by the 'recursive' strategy.
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Merging

Rebasing

» git rebase -i HEAD~5        

pick 8cd3d61 Cleaned old files
pick 36e477d Moved all vendor components to bower
pick 855fbb1 Truncate long qoutes in footer
pick 4cde8f6 Added url to sub-lists
pick 5cb9ead Fix sorting, add delete, fold options

# Rebase a7a56d5..5cb9ead onto a7a56d5 (5 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit

Rebasing

» git rebase -i HEAD~5        

pick 8cd3d61 Cleaned old files
pick 36e477d Moved all vendor components to bower
pick 855fbb1 Truncate long qoutes in footer
pick 4cde8f6 Added url to sub-lists
pick 5cb9ead Fix sorting, add delete, fold options

# Rebase a7a56d5..5cb9ead onto a7a56d5 (5 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit

Rebasing

drop 8cd3d61 Cleaned old files
pick 36e477d Moved all vendor components to bower
pick 855fbb1 Truncate long qoutes in footer
squash 4cde8f6 Added url to sub-lists
squash 5cb9ead Fix sorting, add delete, fold options

Rebasing

# This is a combination of 3 commits.
# The first commit's message is:
Truncate long qoutes in footer

# This is the 2nd commit message:

Added url to sub-lists

# This is the 3rd commit message:

Fix sorting, add delete, fold options

Rebasing

» git rebase -i HEAD~5
[detached HEAD 6998eb0] Truncate long qoutes in footer
 Author: Is Isilon <wassname@wassname.org>
 Date: Mon Feb 1 09:41:24 2016 +0800
 10 files changed, 411 insertions(+), 254 deletions(-)
 rewrite public/javascripts/views/list.js (66%)
 rewrite public/javascripts/views/page.js (72%)
Successfully rebased and updated detached HEAD.

Rebasing

6998eb0 Truncate long qoutes in footer
806ade4 Moved all vendor components to bower
a7a56d5 update installation instructions
ff89165 Version bump
5cb9ead Fix sorting, add delete, fold options
4cde8f6 Added url to sub-lists
855fbb1 Truncate long qoutes in footer
36e477d Moved all vendor components to bower
8cd3d61 Cleaned old files
a7a56d5 update installation instructions
ff89165 Version bump

alias pull="!git pull --rebase"

git commit --amend

Searching commits / blame / log / reflog

Búsqueda commits

# Busqueda full-diff de "bancomer"
» git log --full-diff --stat -p --grep "bancomer"


# Ver todos los commits que tocaron un archivo
» git log --follow --oneline afluenta_kernel/lib/afluenta_country/pkg.ar/Afluenta_Country.class.php
dba97c1 [HOTFIX] [#1233] Nuevo Source y conversion hit en el frontend
2aad1f7 convert array to traditional syntax
31a0448 fix layout
2f64d4c move help region to afluenta country class
abde00f set 'afluenta en la region' from afluenta country class
» git log --since=1.week
» git log --before=1.week

» git log --author="dcavuotti@afluenta.com" --after="2016-9-15" --before="2016-9-20"

» git log --author="dcavuotti@afluenta.com" --since="yesterday"

Anotaciones / Blame

» git blame afluenta_kernel/lib/afluenta_country/pkg.ar/Afluenta_Country.class.php
1cbb6059 (Diego Ghersi        2015-10-09 17:39:35 -0300   1) <?php
1cbb6059 (Diego Ghersi        2015-10-09 17:39:35 -0300   2)
1cbb6059 (Diego Ghersi        2015-10-09 17:39:35 -0300   3) class Afluenta_Country extends Afluenta_Country_Base implements iAfluenta_Country
1cbb6059 (Diego Ghersi        2015-10-09 17:39:35 -0300   4) {
1cbb6059 (Diego Ghersi        2015-10-09 17:39:35 -0300   5)     public function __construct()
1cbb6059 (Diego Ghersi        2015-10-09 17:39:35 -0300   6)     {
1cbb6059 (Diego Ghersi        2015-10-09 17:39:35 -0300   7)     }
1cbb6059 (Diego Ghersi        2015-10-09 17:39:35 -0300   8)
1cbb6059 (Diego Ghersi        2015-10-09 17:39:35 -0300   9)     public function getCurrentCountryId()
1cbb6059 (Diego Ghersi        2015-10-09 17:39:35 -0300  10)     {
1cbb6059 (Diego Ghersi        2015-10-09 17:39:35 -0300  11)         return EnumCountry::COD_AR;

Reflog

» git reflog

e463f76 HEAD@{0}: rebase finished: returning to refs/heads/develop
e463f76 HEAD@{1}: pull: checkout e463f76e58bdecc7ac1a8e267feba69a687e2955
a8c8408 HEAD@{2}: checkout: moving from feature/1227-cambios-en-pantalla-documentacion-lender to develop
460b2d0 HEAD@{3}: checkout: moving from feature/1227-cambios-en-pantalla-documentacion-lender-1 to feature/1227-cambios-en-pantalla-documentacion-lender
460b2d0 HEAD@{4}: checkout: moving from feature/1227-cambios-en-pantalla-documentacion-lender to feature/1227-cambios-en-pantalla-documentacion-lender-1
460b2d0 HEAD@{5}: rebase finished: returning to refs/heads/feature/1227-cambios-en-pantalla-documentacion-lender

Common problems

Finding a lost commit

» git fsck --lost-found
Checking object directories: 100% (256/256), done.
Checking objects: 100% (117159/117159), done.
dangling commit c70e40b60f6d7362d62807ec9f6c9175bff9b83d
dangling commit 3b104c3b7ebc4d15b199b658c7a67e3da1643500
dangling commit df1c10f05d6e4ad51bc5ea633c855d8151ea66f7
dangling commit 521e7cf5705f7a7db90dad6dfd40bc1d32e35367
dangling commit 652288b706999968ec5d6070ca5e0f6a371d9e68
dangling commit 3623d00caae833a7241d240617a5fad7e1729592
dangling commit 4125e4773641fc88833061730700ffd84d1f3ef8
dangling commit c62ccc1471b103b1070b4b793178e9524aa6c804
dangling commit d73074877efeffd7ac42045a9015937268e50ab8


» git reflog
e463f76 HEAD@{0}: rebase finished: returning to refs/heads/develop
e463f76 HEAD@{1}: pull: checkout e463f76e58bdecc7ac1a8e267feba69a687e2955
a8c8408 HEAD@{2}: checkout: moving from feature/1227-cambios-en-pantalla-documentacion-lender to develop
460b2d0 HEAD@{3}: checkout: moving from feature/1227-cambios-en-pantalla-documentacion-lender-1 to feature/1227-cambios-en-pantalla-documentacion-lender

Reverting a commit

» git reset HEAD^1

» git reset origin/develop

» git reset --HARD origin/develop

Reverting a commit

» git reset HEAD^1

» git reset origin/develop

» git reset --HARD origin/develop

Tips & Tricks & Tools

» git hist
*   e463f76 2016-09-20 | Merge branch 'feature/1227-cambios-en-pantalla-documentacion-lender' into 'develop' (HEAD -> develop, origin/develop) [Diego Ghersi]
|\
| * 460b2d0 2016-09-20 | [FEATURE][#1227] Se agrega dbdiff con user_documentation_type (origin/feature/1227-cambios-en-pantalla-documentacion-lender-1, desyncr/feature/1227-cambios-en-pantalla-documentacion-lender-1, desyncr/feature/1227-cambios-en-pantalla-documentacion-lender, feature/1227-cambios-en-pantalla-documentacion-lender-1, feature/1227-cambios-en-pantalla-documentacion-lender) [Dario]
| * e0bcd7c 2016-09-20 | [FEATURE][#1227] Se agrega checkbox en formulario de adhesion para MX [Dario]
| * 0f2bc84 2016-09-19 | [FEATURE][#1227] Se cambia documentacion requerida para Lender en MX [Dario]
* |   e18b605 2016-09-20 | Merge branch 'feature/edit-indep-jubi-laboral-info-bo' into 'develop' [Diego Ghersi]
|\ \
| |/
|/|
| * 0f88cdb 2016-09-20 | [Issue #1244] Editar Income Independiente desde el BO (MX) [Mariano Calvente]
» git compare master develop
*   e463f76 - (HEAD -> develop, origin/develop) Merge branch 'feature/1227-cambios-en-pantalla-documentacion-lender' into 'develop' (19 hours ago)
|\
| * 460b2d0 - (origin/feature/1227-cambios-en-pantalla-documentacion-lender-1, desyncr/feature/1227-cambios-en-pantalla-documentacion-lender-1, desyncr/feature/1227-cambios-en-pantalla-documentacion-lender, feature/1227-cambios-en-pantalla-documentacion-lender-1, feature/1227-cambios-en-pantalla-documentacion-lender) [FEATURE][#1227] Se agrega dbdiff con user_documentation_type (21 hours ago)
| * e0bcd7c - [FEATURE][#1227] Se agrega checkbox en formulario de adhesion para MX (21 hours ago)
| * 0f2bc84 - [FEATURE][#1227] Se cambia documentacion requerida para Lender en MX (21 hours ago)
* |   e18b605 - Merge branch 'feature/edit-indep-jubi-laboral-info-bo' into 'develop' (21 hours ago)
|\ \
| |/
|/|

Aliases FTW

» gd
/var/folders/64/2tz1snw955b167ptxh3r6v_c0000gn/T//ASleZ8_UserDocumentationType.php afluenta_core/afluenta/db/afluenta/UserDocumentationType.php
    const INCOME_VOUCHER = 24;                               const INCOME_VOUCHER = 24;
    const PEP_OFAC = 25;                                     const PEP_OFAC = 25;
    const OTHER = 26;                                        const OTHER = 26;
    const PEP_OFAC_PERU = 27;                                const PEP_OFAC_PERU = 27;
    const OTHER_PERU = 28;                                   const OTHER_PERU = 28;
                                                             const IFE_INE_FRONTAL = 29;
                                                             const IFE_INE_DORSAL = 30;
}                                                        }
/var/folders/64/2tz1snw955b167ptxh3r6v_c0000gn/T//n8NYMa_iAfluenta_Id.interface.php afluenta_kernel/lib/afluenta_id/iAfluenta_Id.interface.php
    public function askNamesInTwostep();                     public function askNamesInTwostep();
    public function getDefaultIdentifierType();              public function getDefaultIdentifierType();
    public function hasInnerCity();                          public function hasInnerCity();
    public function askExtraDataInTwoStep();                 public function askExtraDataInTwoStep();
    public function shouldUseSecondLastName($document_t      public function shouldUseSecondLastName($document_t
ype = null);                                             ype = null);
                                                             public function askConfirmGlossary();
}                                                        }

Side-by-side diffing

Git Legit

$ git sync
# Syncronizes current branch. Auto-merge/rebase, un/stash.

$ git switch <branch>
# Switches to branch. Stashes and restores unstaged changes.

$ git publish <branch>
# Publishes branch to remote server.

$ git unpublish <branch>
# Removes branch from remote server.

$ git branches
# Nice & pretty list of branches + publication status.

http://www.git-legit.org/

Gitlab CLI

» git lab -h
usage: git-lab <command> [options]

commands:

 help    Show help for a given help topic or a help overview.
 mr      Create merge request
 pocomr  Post note
 shcomr  Show merge request comments
 shmr    Show merge request infos
 upmr    Update merge request

https://gitlab.com/bor-sh/git-gitlab

Stashing

Fin

Git Intro

By Darío Cavuotti