Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

#3 - Git - Branching e Merging

816 views

Published on

Branching e Merging com Git.

Published in: Software

#3 - Git - Branching e Merging

  1. 1. Rodrigo Branas – @rodrigobranas - http://www.agilecode.com.br #3 - Git - Branching e Merging
  2. 2. 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
  3. 3. http://www.youtube.com/rodrigobranas
  4. 4. O que é um branch e quais são as vantagens de utilizá-lo?
  5. 5. Um branch é uma nova linha de desenvolvimento que permite isolar o código de uma nova funcionalidade, mantendo a linha base estável.
  6. 6. É possível trocar de branch facilmente, a qualquer momento
  7. 7. Os commits podem continuar contando a história do projeto
  8. 8. O branch pode ser sincronizado e compartilhado, evitando perdas
  9. 9. Depois de trabalhar no branch é necessário realizar um merge
  10. 10. git branch
  11. 11. commit a9ae tree f4b3 parent master
  12. 12. commit a9ae tree f4b3 parent commit 372d tree d11b parent a9ae master
  13. 13. commit a9ae tree f4b3 parent commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d master
  14. 14. commit a9ae tree f4b3 parent commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master
  15. 15. git branch feature1
  16. 16. git branch
  17. 17. commit a9ae tree f4b3 parent commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master
  18. 18. commit a9ae tree f4b3 parent commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature1
  19. 19. No Git, um branch é apenas uma referência para um commit.
  20. 20. cat .git/refs/heads/master cat .git/refs/heads/feature1
  21. 21. git log --oneline --decorate
  22. 22. HEAD?
  23. 23. cat .git/HEAD
  24. 24. commit a9ae tree f4b3 parent commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature1
  25. 25. commit a9ae tree f4b3 parent commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature1HEAD
  26. 26. git checkout feature1
  27. 27. cat .git/HEAD
  28. 28. commit a9ae tree f4b3 parent commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature1HEAD
  29. 29. commit a9ae tree f4b3 parent commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature1 HEAD
  30. 30. Criando um commit no branch...
  31. 31. echo d > d.txt git add -A git commit -m "d.txt"
  32. 32. commit a9ae tree f4b3 parent commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature1 HEAD commit a42c tree ad86 parent b274
  33. 33. git log --oneline --decorate
  34. 34. Trocando de branch, os arquivos são substituídos
  35. 35. git checkout master ls -la
  36. 36. git log --oneline --decorate
  37. 37. git log --oneline --decorate --all
  38. 38. commit a9ae tree f4b3 parent commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature1 HEAD commit a42c tree ad86 parent b274
  39. 39. commit a9ae tree f4b3 parent commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature1 commit a42c tree ad86 parent b274 HEAD
  40. 40. Realizando um merge no master...
  41. 41. git merge feature1
  42. 42. commit a9ae tree f4b3 parent commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature1 commit a42c tree ad86 parent b274 HEAD
  43. 43. commit a9ae tree f4b3 parent commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature1 commit a42c tree ad86 parent b274 HEAD
  44. 44. Fast-Forward
  45. 45. 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.
  46. 46. git log --oneline --decorate --all
  47. 47. git branch -d feature1
  48. 48. git log --oneline --decorate --all
  49. 49. commit a9ae tree f4b3 parent commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature1 commit a42c tree ad86 parent b274 HEAD
  50. 50. commit a9ae tree f4b3 parent commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master commit a42c tree ad86 parent b274 HEAD
  51. 51. Criando uma divergência entre o master e o branch...
  52. 52. git branch feature2 git checkout feature2 ou git checkout -b feature2
  53. 53. commit a9ae tree f4b3 parent commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master commit a42c tree ad86 parent b274 HEAD
  54. 54. commit a9ae tree f4b3 parent commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature2 commit a42c tree ad86 parent b274 HEAD
  55. 55. echo e > e.txt git add -A git commit -m "e.txt"
  56. 56. git log --oneline --decorate --all
  57. 57. commit a9ae tree f4b3 parent commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature2 commit a42c tree ad86 parent b274 HEAD
  58. 58. commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature2 commit a42c tree ad86 parent b274 HEAD
  59. 59. commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature2 commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c HEAD
  60. 60. git checkout master
  61. 61. commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature2 commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c HEAD
  62. 62. commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature2 commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c HEAD
  63. 63. commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature2 commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c HEAD
  64. 64. echo f > f.txt git add -A git commit -m "f.txt"
  65. 65. commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature2 commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c HEAD
  66. 66. commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature2 commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c HEAD commit d67a tree 10e6 parent a42c
  67. 67. git log --oneline --decorate --all
  68. 68. git log --oneline --decorate --all --graph
  69. 69. Realizando um merge...
  70. 70. git merge feature2
  71. 71. commit 372d tree d11b parent a9ae commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature2 commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c HEAD commit d67a tree 10e6 parent a42c
  72. 72. commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature2 commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c HEAD commit d67a tree 10e6 parent a42c
  73. 73. commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature2 commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c HEAD commit d67a tree 10e6 parent a42c commit cc8a tree 10e6 parent d67a parent fde2
  74. 74. Recursive
  75. 75. A estratégia recursive é utilizada quando existe divergência entre os branches e um commit para unir ambos se torna necessário.
  76. 76. git log --oneline --decorate --all --graph
  77. 77. git branch -d feature2
  78. 78. commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master feature2 commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c HEAD commit d67a tree 10e6 parent a42c commit cc8a tree 10e6 parent d67a parent fde2
  79. 79. commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c HEAD commit d67a tree 10e6 parent a42c commit cc8a tree 10e6 parent d67a parent fde2
  80. 80. E se der conflito?
  81. 81. git checkout -b feature3
  82. 82. commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c HEAD commit d67a tree 10e6 parent a42c commit cc8a tree 10e6 parent d67a parent fde2
  83. 83. commit 7f64 tree 6300 parent 372d commit b274 tree 15eb parent 7f64 master commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c HEAD commit d67a tree 10e6 parent a42c commit cc8a tree 10e6 parent d67a parent fde2 feature3
  84. 84. echo g2 > g.txt git add -A git commit -m "g.txt"
  85. 85. commit b274 tree 15eb parent 7f64 master commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c HEAD commit d67a tree 10e6 parent a42c commit cc8a tree 10e6 parent d67a parent fde2 feature3
  86. 86. commit b274 tree 15eb parent 7f64 master commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c HEAD commit d67a tree 10e6 parent a42c commit cc8a tree 10e6 parent d67a parent fde2 feature3 commit c5b5 tree 82b4 parent cc8a
  87. 87. git log --oneline --decorate --all --graph
  88. 88. git checkout master
  89. 89. commit b274 tree 15eb parent 7f64 master commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c HEAD commit d67a tree 10e6 parent a42c commit cc8a tree 10e6 parent d67a parent fde2 feature3 commit c5b5 tree 82b4 parent cc8a
  90. 90. commit b274 tree 15eb parent 7f64 master commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c commit d67a tree 10e6 parent a42c commit cc8a tree 10e6 parent d67a parent fde2 feature3 commit c5b5 tree 82b4 parent cc8a HEAD
  91. 91. echo g1 > g.txt git add -A git commit -m "g.txt"
  92. 92. commit b274 tree 15eb parent 7f64 master commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c commit d67a tree 10e6 parent a42c commit cc8a tree 10e6 parent d67a parent fde2 feature3 commit c5b5 tree 82b4 parent cc8a HEAD
  93. 93. master commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c commit d67a tree 10e6 parent a42c commit cc8a tree 10e6 parent d67a parent fde2 feature3 commit c5b5 tree 82b4 parent cc8a HEAD
  94. 94. master commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c commit d67a tree 10e6 parent a42c commit cc8a tree 10e6 parent d67a parent fde2 feature3 commit c5b5 tree 82b4 parent cc8a HEAD commit c85e tree a1bb parent cc8a
  95. 95. git merge feature3
  96. 96. git status
  97. 97. git diff
  98. 98. vi g.txt
  99. 99. git add -A git commit -m "g.txt"
  100. 100. master commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c commit d67a tree 10e6 parent a42c commit cc8a tree 10e6 parent d67a parent fde2 feature3 commit c5b5 tree 82b4 parent cc8a HEAD commit c85e tree a1bb parent cc8a
  101. 101. master commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c commit d67a tree 10e6 parent a42c commit cc8a tree 10e6 parent d67a parent fde2 feature3 commit c5b5 tree 82b4 parent cc8a HEAD commit c85e tree a1bb parent cc8a commit 270e tree 82b4 parent c85e parent c5b5
  102. 102. git log --oneline --decorate --all --graph
  103. 103. git branch -d feature3
  104. 104. master commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c commit d67a tree 10e6 parent a42c commit cc8a tree 10e6 parent d67a parent fde2 feature3 commit c5b5 tree 82b4 parent cc8a HEAD commit c85e tree a1bb parent cc8a commit 270e tree 82b4 parent c85e parent c5b5
  105. 105. master commit a42c tree ad86 parent b274 commit fde2 tree a025 parent a42c commit d67a tree 10e6 parent a42c commit cc8a tree 10e6 parent d67a parent fde2 commit c5b5 tree 82b4 parent cc8a HEAD commit c85e tree a1bb parent cc8a commit 270e tree 82b4 parent c85e parent c5b5
  106. 106. 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

×