Merging Branches in Git

We’ll cov­er merg­ing best prac­tices a bit lat­er but let’s set some ground rules:

  • Make sure all of your changes are com­mit­ted so they’re includ­ed as part of the merge. Run­ning git-status before a merge is a good san­i­ty check.
  • When you merge, you should check out the branch into which you want to merge. For this exam­ple we want to check out master again before merging.
  • Merge con­flicts can hap­pen if Git can’t safe­ly com­bine the changes between branch­es. If this hap­pens you’ll have to man­u­al­ly resolve the con­flict. We’ll cov­er this more lat­er when we dis­cuss merges and how to fix prob­lems in Git.

We already added and com­mit­ted our change but let’s check the sta­tus of repos­i­to­ry again just to be sure we are safe to do the merge.

$ git status
  On branch ri_refactoring-code
  nothing to commit, working directory clean

Okay, good. Now we can switch back to the master branch.

$ git checkout master
  Switched to branch 'master'

And now for the merge. The git-merge com­mand takes one argu­ment that is the branch you want to merge into the checked-out branch.

$ git merge ri_refactoring-code -m "initial refactoring code"
  [master e6b6621] intitial refactoring of the code

The merge is done. Let’s look at the log to see if our com­mit from the refac­tor­ing branch is there.

$ git log
  commit e6b6621c971ab7eb6e2a6dbc67c83be787ebe0c4
  Merge: 4a994e2 b1dc0e7
  Author: Ryan Irelan <[email protected]>
  Date:   Fri Apr 1 13:39:38 2016 -0500
	
	    intitial refactoring of the code

And, there is it. Success!