0% found this document useful (0 votes)
4 views149 pages

3 Git Branchingandmergin 150928235608 Lva1 App6892

The document discusses the concepts of branching and merging in Git, highlighting the advantages of using branches for isolating new features while keeping the main codebase stable. It explains the process of creating branches, making commits, and merging changes, including strategies like fast-forward and recursive merges. Additionally, it addresses potential conflicts that may arise during merging and how to manage them.

Uploaded by

Otavio Costa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views149 pages

3 Git Branchingandmergin 150928235608 Lva1 App6892

The document discusses the concepts of branching and merging in Git, highlighting the advantages of using branches for isolating new features while keeping the main codebase stable. It explains the process of creating branches, making commits, and merging changes, including strategies like fast-forward and recursive merges. Additionally, it addresses potential conflicts that may arise during merging and how to manage them.

Uploaded by

Otavio Costa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 149

Rodrigo Branas – @rodrigobranas - http://www.agilecode.com.

br

#3 - Git - Branching e Merging


Rodrigo Branas
rodrigo.branas@agilecode.com.br
http://www.agilecode.com.br

• Arquiteto de Software na Gennera


• Professor na Agile Code
• Autor na Java Magazine e PacktPub
• Palestrante
http://www.youtube.com/rodrigobranas
O que é um branch e quais são
as vantagens de utilizá-lo?
Um branch é uma nova linha de
desenvolvimento que permite isolar o
código de uma nova funcionalidade,
mantendo a linha base estável.
É possível trocar de branch
facilmente, a qualquer momento
Os commits podem continuar
contando a história do projeto
O branch pode ser sincronizado e
compartilhado, evitando perdas
Depois de trabalhar no branch é
necessário realizar um merge
git branch
commit a9ae
master tree f4b3
parent
commit 372d
master tree d11b
parent a9ae

commit a9ae
tree f4b3
parent
commit 7f64
master tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae

commit a9ae
tree f4b3
parent
commit b274
master tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae

commit a9ae
tree f4b3
parent
git branch feature1
git branch
commit b274
master tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae

commit a9ae
tree f4b3
parent
commit b274
master tree 15eb feature1
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae

commit a9ae
tree f4b3
parent
No Git, um branch é apenas uma
referência para um commit.
cat .git/refs/heads/master
cat .git/refs/heads/feature1
git log --oneline --decorate
HEAD?
cat .git/HEAD
commit b274
master tree 15eb feature1
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae

commit a9ae
tree f4b3
parent
commit b274
HEAD master tree 15eb feature1
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae

commit a9ae
tree f4b3
parent
git checkout feature1
cat .git/HEAD
commit b274
HEAD master tree 15eb feature1
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae

commit a9ae
tree f4b3
parent
commit b274
master tree 15eb feature1 HEAD
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae

commit a9ae
tree f4b3
parent
Criando um commit no branch...
echo d > d.txt
git add -A
git commit -m "d.txt"
commit a42c
tree ad86 feature1 HEAD
parent b274

commit b274
master tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae

commit a9ae
tree f4b3
parent
git log --oneline --decorate
Trocando de branch, os arquivos
são substituídos
git checkout master
ls -la
git log --oneline --decorate
git log --oneline --decorate --all
commit a42c
tree ad86 feature1 HEAD
parent b274

commit b274
master tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae

commit a9ae
tree f4b3
parent
commit a42c
tree ad86 feature1
parent b274

commit b274
HEAD master tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae

commit a9ae
tree f4b3
parent
Realizando um merge no
master...
git merge feature1
commit a42c
tree ad86 feature1
parent b274

commit b274
HEAD master tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae

commit a9ae
tree f4b3
parent
commit a42c
HEAD master tree ad86 feature1
parent b274

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae

commit a9ae
tree f4b3
parent
Fast-Forward
A estratégia fast-forward é apenas uma
atualização da referência e só é possível
quando não existe divergência entre
os branches.
git log --oneline --decorate --all
git branch -d feature1
git log --oneline --decorate --all
commit a42c
HEAD master tree ad86 feature1
parent b274

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae

commit a9ae
tree f4b3
parent
commit a42c
HEAD master tree ad86
parent b274

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae

commit a9ae
tree f4b3
parent
Criando uma divergência entre o
master e o branch...
git branch feature2
git checkout feature2

ou

git checkout -b feature2


commit a42c
HEAD master tree ad86
parent b274

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae

commit a9ae
tree f4b3
parent
commit a42c
master tree ad86 feature2 HEAD
parent b274

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae

commit a9ae
tree f4b3
parent
echo e > e.txt
git add -A
git commit -m "e.txt"
git log --oneline --decorate --all
commit a42c
master tree ad86 feature2 HEAD
parent b274

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae

commit a9ae
tree f4b3
parent
commit a42c
master tree ad86 feature2 HEAD
parent b274

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae
commit fde2
tree a025 feature2 HEAD
parent a42c

commit a42c
master tree ad86
parent b274

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae
git checkout master
commit fde2
tree a025 feature2 HEAD
parent a42c

commit a42c
master tree ad86
parent b274

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae
commit fde2
tree a025 feature2
parent a42c

commit a42c
HEAD master tree ad86
parent b274

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae
commit a42c commit fde2
HEAD master tree ad86 tree a025 feature2
parent b274 parent a42c

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae
echo f > f.txt
git add -A
git commit -m "f.txt"
commit a42c commit fde2
HEAD master tree ad86 tree a025 feature2
parent b274 parent a42c

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae
commit d67a
HEAD master tree 10e6
parent a42c

commit a42c commit fde2


tree ad86 tree a025 feature2
parent b274 parent a42c

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae
git log --oneline --decorate --all
git log --oneline --decorate --all --graph
Realizando um merge...
git merge feature2
commit d67a
HEAD master tree 10e6
parent a42c

commit a42c commit fde2


tree ad86 tree a025 feature2
parent b274 parent a42c

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d

commit 372d
tree d11b
parent a9ae
commit d67a
HEAD master tree 10e6
parent a42c

commit a42c commit fde2


tree ad86 tree a025 feature2
parent b274 parent a42c

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d
commit cc8a
tree 10e6
HEAD master parent d67a
parent fde2

commit d67a
tree 10e6
parent a42c

commit a42c commit fde2


tree ad86 tree a025 feature2
parent b274 parent a42c

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d
Recursive
A estratégia recursive é utilizada
quando existe divergência entre os
branches e um commit para unir ambos
se torna necessário.
git log --oneline --decorate --all --graph
git branch -d feature2
commit cc8a
tree 10e6
HEAD master parent d67a
parent fde2

commit d67a
tree 10e6
parent a42c

commit a42c commit fde2


tree ad86 tree a025 feature2
parent b274 parent a42c

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d
commit cc8a
tree 10e6
HEAD master parent d67a
parent fde2

commit d67a
tree 10e6
parent a42c

commit a42c commit fde2


tree ad86 tree a025
parent b274 parent a42c

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d
E se der conflito?
git checkout -b feature3
commit cc8a
tree 10e6
HEAD master parent d67a
parent fde2

commit d67a
tree 10e6
parent a42c

commit a42c commit fde2


tree ad86 tree a025
parent b274 parent a42c

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d
commit cc8a
tree 10e6
master parent d67a feature3 HEAD
parent fde2

commit d67a
tree 10e6
parent a42c

commit a42c commit fde2


tree ad86 tree a025
parent b274 parent a42c

commit b274
tree 15eb
parent 7f64

commit 7f64
tree 6300
parent 372d
echo g2 > g.txt
git add -A
git commit -m "g.txt"
commit cc8a
tree 10e6
master parent d67a feature3 HEAD
parent fde2

commit d67a
tree 10e6
parent a42c

commit a42c commit fde2


tree ad86 tree a025
parent b274 parent a42c

commit b274
tree 15eb
parent 7f64
commit c5b5
tree 82b4 feature3 HEAD
parent cc8a

commit cc8a
tree 10e6
master parent d67a
parent fde2

commit d67a
tree 10e6
parent a42c

commit a42c commit fde2


tree ad86 tree a025
parent b274 parent a42c

commit b274
tree 15eb
parent 7f64
git log --oneline --decorate --all --graph
git checkout master
commit c5b5
tree 82b4 feature3 HEAD
parent cc8a

commit cc8a
tree 10e6
master parent d67a
parent fde2

commit d67a
tree 10e6
parent a42c

commit a42c commit fde2


tree ad86 tree a025
parent b274 parent a42c

commit b274
tree 15eb
parent 7f64
commit c5b5
tree 82b4 feature3
parent cc8a

commit cc8a
tree 10e6
HEAD master parent d67a
parent fde2

commit d67a
tree 10e6
parent a42c

commit a42c commit fde2


tree ad86 tree a025
parent b274 parent a42c

commit b274
tree 15eb
parent 7f64
echo g1 > g.txt
git add -A
git commit -m "g.txt"
commit c5b5
tree 82b4 feature3
parent cc8a

commit cc8a
tree 10e6
HEAD master parent d67a
parent fde2

commit d67a
tree 10e6
parent a42c

commit a42c commit fde2


tree ad86 tree a025
parent b274 parent a42c

commit b274
tree 15eb
parent 7f64
commit cc8a
commit c5b5
tree 10e6
HEAD master parent d67a
tree 82b4 feature3
parent cc8a
parent fde2

commit d67a
tree 10e6
parent a42c

commit a42c commit fde2


tree ad86 tree a025
parent b274 parent a42c
commit c85e
HEAD master tree a1bb
parent cc8a

commit cc8a
commit c5b5
tree 10e6
parent d67a
tree 82b4 feature3
parent cc8a
parent fde2

commit d67a
tree 10e6
parent a42c

commit a42c commit fde2


tree ad86 tree a025
parent b274 parent a42c
git merge feature3
git status
git diff
vi g.txt
git add -A
git commit -m "g.txt"
commit c85e
HEAD master tree a1bb
parent cc8a

commit cc8a
commit c5b5
tree 10e6
parent d67a
tree 82b4 feature3
parent cc8a
parent fde2

commit d67a
tree 10e6
parent a42c

commit a42c commit fde2


tree ad86 tree a025
parent b274 parent a42c
commit 270e
tree 82b4
HEAD master parent c85e
parent c5b5

commit c85e
tree a1bb
parent cc8a

commit cc8a
commit c5b5
tree 10e6
parent d67a
tree 82b4 feature3
parent cc8a
parent fde2

commit d67a
tree 10e6
parent a42c

commit a42c commit fde2


tree ad86 tree a025
parent b274 parent a42c
git log --oneline --decorate --all --graph
git branch -d feature3
commit 270e
tree 82b4
HEAD master parent c85e
parent c5b5

commit c85e
tree a1bb
parent cc8a

commit cc8a
commit c5b5
tree 10e6
parent d67a
tree 82b4 feature3
parent cc8a
parent fde2

commit d67a
tree 10e6
parent a42c

commit a42c commit fde2


tree ad86 tree a025
parent b274 parent a42c
commit 270e
tree 82b4
HEAD master parent c85e
parent c5b5

commit c85e
tree a1bb
parent cc8a

commit cc8a
commit c5b5
tree 10e6
tree 82b4
parent d67a
parent cc8a
parent fde2

commit d67a
tree 10e6
parent a42c

commit a42c commit fde2


tree ad86 tree a025
parent b274 parent a42c
Rodrigo Branas
Site: http://www.agilecode.com.br
Twitter: @rodrigobranas
Facebook: http://www.facebook.com/canalrodrigobranas
SlideShare: http://www.slideshare.com/rodrigobranas
YouTube: http://www.youtube.com/rodrigobranas
LinkedIn: http://br.linkedin.com/in/rodrigobranas
+Plus: https://plus.google.com/+RodrigoBranas
GitHub: http://www.github.com/rodrigobranas

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy