Merging

Goals

  • Learn how to merge two diverging branches to bring the changes back into a single branch.

Merge the branches

Merging brings the changes in two branches together. Let’s go back to the greet branch and merge master onto greet.

Execute:

  1. git checkout greet
  2. git merge master
  3. git hist --all

Output:

  1. $ git checkout greet
  2. Switched to branch 'greet'
  3. $ git merge master
  4. Merge made by the 'recursive' strategy.
  5. README | 1 +
  6. 1 file changed, 1 insertion(+)
  7. create mode 100644 README
  8. $ git hist --all
  9. * a87176d 2020-06-20 | Merge branch 'master' into greet (HEAD -> greet) [Jim Weirich]
  10. |\
  11. | * 8d90176 2020-06-20 | Added README (master) [Jim Weirich]
  12. * | 228a31e 2020-06-20 | Updated Rakefile [Jim Weirich]
  13. * | 98cfd8a 2020-06-20 | Hello uses Greeter [Jim Weirich]
  14. * | cf0438b 2020-06-20 | Added greeter class [Jim Weirich]
  15. |/
  16. * 5aec14d 2020-06-20 | Added a Rakefile. [Jim Weirich]
  17. * 721b979 2020-06-20 | Moved hello.rb to lib [Jim Weirich]
  18. * 907a445 2020-06-20 | Add an author/email comment [Jim Weirich]
  19. * 4254c94 2020-06-20 | Added a comment (tag: v1) [Jim Weirich]
  20. * c8b3af1 2020-06-20 | Added a default value (tag: v1-beta) [Jim Weirich]
  21. * 30c2cd4 2020-06-20 | Using ARGV [Jim Weirich]
  22. * 4445720 2020-06-20 | First Commit [Jim Weirich]

By merging master into your greet branch periodically, you can pick up any changes to master and keep your changes in greet compatible with changes in the mainline.

However, it does produce ugly commit graphs. Later we will look at the option of rebasing rather than merging.

Up Next

But first, what if the changes in master conflict with the changes in greet?