Merging Branches in Git
We’ll cover merging best practices a bit later but let’s set some ground rules:
- Make sure all of your changes are committed so they’re included as part of the merge. Running
git-statusbefore a merge is a good sanity check.
- When you merge, you should check out the branch into which you want to merge. For this example we want to check out
masteragain before merging.
- Merge conflicts can happen if Git can’t safely combine the changes between branches. If this happens you’ll have to manually resolve the conflict. We’ll cover this more later when we discuss merges and how to fix problems in Git.
We already added and committed our change but let’s check the status of repository 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
$ git checkout master Switched to branch 'master'
And now for the merge. The
git-merge command takes one argument 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 commit from the refactoring 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!