Merging

Remember that the purpose of a topic branch (likesidebar) is that it lets you make changes that do not affect themasterbranch. Once you make changes on the topic branch, you can either decide that you don't like the changes on the branch and you can just delete that branch, or you can decide that you want to keep the changes on the topic branch and combine those changes in with those on another branch.

Combining branches together is called merging.

Git can automatically merge the changes on different branches together. This branching and merging ability is what makes Git incredibly powerful! You can make small or extensive changes on branches, and then just use Git to combine those changes together.

Let's see how this works, in theory. Pay attention to the two main types of merges in Git, a regular merge and a Fast-forward merge.

VIDEO

Quick Check In

Let's do a quick check-in to make sure we're both starting at the same point. Check off each of the following:

Task List

  • I am in thenew-git-projectproject

  • I have checked out themasterbranch

  • the output ofgit statusincludes the textworking directory clean

⚠️ Know The Branch ⚠️

It's very important to know which branch you're on when you're about to merge branches together. Remember that making a merge makes a commit.

As of right now, we do not know how to_undo_changes. We'll go over it in the next lesson, but if you make a merge on the wrong branch, use this command to undo the merge:

git reset --hard HEAD^

(Make sure to include the^character! It's a known as a "Relative Commit Reference" and indicates "the parent commit". We'll look at Relative Commit References in the next lesson.)

The Merge Command

Thegit mergecommand is used to combine Git branches:

$ git merge <name-of-branch-to-merge-in>

When a merge happens, Git will:

  • look at the branches that it's going to merge
  • look back along the branch's history to find a single commit that both branches have in their commit history
  • combine the lines of code that were changed on the separate branches together
  • makes a commit to record the merge

Fast-forward Merge

In our project, I've checked out themasterbranch and I want _it_ to have the changes that are on thefooterbranch. If I wanted to verbalize this, I could say this is - "I want to merge in thefooterbranch". That "merge in" is important; when a merge is performed, the_other_branch's changes are brought into the branch that's currently checked out.

Let me stress that again - When we merge, we're merging some other branch into the current (checked-out) branch. We're not merging two branches into a new branch. We're not merging the current branch into the other branch.

Now, sincefooteris directly ahead ofmaster, this merge is one of the easiest merges to do. Mergingfooterintomasterwill cause aFast-forward merge. A Fast-forward merge will just move the currently checked out branch_forward_until it points to the same commit that the other branch (in this case,footer) is pointing to.

To merge in thefooterbranch, run:

$ git merge footer

This is what my Terminal displays after running the command:

_The Terminal application showing the result of merging thefooterbranchinto_themasterbranch.\

QUESTION 1 OF 2

Take a look at the repository, again, now that you've merged the two branches together. Do themasterbranch and thefooterbranch point to the same commit?

  • Yes

  • No

SUBMIT

Perform A Regular Merge

Fantastic work doing a Fast-forward merge! That wasn't too hard, was it?

But you might say - "Of course that was easy, all of the commits are already there and the branch pointer just moved forward!"...and you'd be right. It's the simplest of merges.

So let's do the more common kind of merge where two_divergent_branches are combined. You'll be surprised that to merge in a divergent branch likesidebaris actually no different!

To merge in thesidebarbranch, make sure you're on themasterbranch and run:

$ git merge sidebar

Because this combines two divergent branches, a commit is going to be made. And when a commit is made, a commit message needs to be supplied. Since this is a_merge commit_a default message is already supplied. You can change the message if you want, but it's common practice to use the default merge commit message. So when your code editor opens with the message, just close it again and accept that commit message.

This is what my Terminal shows after using the default commit message:

The Terminal application showing the result of merging thesidebarbranch.

Aaaand that's all there is to merging! It's pretty simple, isn't it? You might read a lot of material that makes branching and merging seem overly complicated, but it's really not too bad at all.

QUESTION 2 OF 2

How about a quick quiz to see if you're following along with how merging works.

Let's say a repository has 4 branches in it:

  • master
  • allisons-mobile-footer-fix
  • nav-updates
  • jonathans-seo-changes

The changes onmasterandallisons-mobile-footer-fixneed to be merged together. If HEAD points toallisons-mobile-footer-fix, which branch will move when the merge is performed?

  • master

  • allisons-mobile-footer-fix

  • nav-updates

  • jonathans-seo-changes

SUBMIT: That's right! Whichever branch the special HEAD pointer is pointing at, that's the branch that will have the merge commit.

What If A Merge Fails?

The merges we just did were able to merge successfully. Git is able to intelligently combine lots of work on different branches. However, there are times when it can't combine branches together. When a merge is performed and fails, that is called amerge conflict. We'll look at merge conflicts, what causes them, and how to resolve them in the next lesson.

Merge Recap

To recap, thegit mergecommand is used to combine branches in Git:

$ git merge <other-branch>

There are two types of merges:

  • Fast-forward merge – the branch being merged in must be _ahead _of the checked out branch. The checked out branch's pointer will just be moved forward to point to the same commit as the other branch.
  • the regular type of merge
    • two divergent branches are combined
    • a merge commit is created

Further Research

results matching ""

    No results matching ""