Development Tools Git Branches and Merging Merging Merging Into Topic Branches

Jamie Reardon
Jamie Reardon
Treehouse Project Reviewer

git checkout branch before merging?

In the video, the teach was checking out the two branches (master and testing) before merging the contents of the each with the command: git merge testing (for checked out master branch) and git merge master (for checked out testing branch).

So my question is, is this necessary to perform the checkouts before merging? Couldn't you also stay in a different checked out branch e.g. git checkout fix-nils and then run the command git merge master testing / git merge testing master to get the same desired results?

Thanks!

1 Answer

Liam Clarke
MOD
Liam Clarke
Treehouse Moderator 19,701 Points

Hi Jamie

I believe this video is showing you how to merge two local branches.

In this case you do need to checkout the receiving branch to ensure the HEAD is pointing to the correct merge-receiving branch.

This is needed because a working copy is needed to compare against to fix any potential conflicts when git is trying to find a common base commit between the two branches.

The example you give will actually merge branches master and testing on top of the current branch. Making a 3-way merge (octopus merge)

 git merge master testing

There are short-hands that can however, fetch and rebase, but only for specific non-fast forward situations.

Jamie Reardon
Jamie Reardon
Treehouse Project Reviewer

Thanks for your detailed answer Liam. I see how that makes sense now to checkout the branch first.