Site Navigation

Post navigation

Getting Good with Git: Merging

Posted: October 21, 2012 Updated: July 4, 2014

In the last post, I went over branching in Git. We created a branch named custom_grid in order to play around with different grid implementations without effecting our Master branch. Good news, the custom grid is a go! It is far simpler than the one that ships with Bootstrap, and sometimes simplicity is the best route. Now it is time to merge the changes in the custom_grid branch with the Master branch. In this post, I will run through a simple merge in Git.

Viewing Code Differences Between Branches

git diff [name_of_branch]..[name_of_branch_2]

View the differences between the [name_of_branch] branch and the [name_of_branch_2] branch. Typically, you will be comparing your master branch with another branch, ie: git diff master..[experimental_branch].

Preparing to Merge Branches

Before you merge branches, you want to verify that you are on the branch that you want to merge changes into. Keeping with the example above, make sure that the master branch is active/checked out prior to merging.

git branch

View a list of branches in the repository. The branch with the astrix next to it is the active branch (the branch that is currently checked out).

git checkout [name_of_branch]

Checkout (switch to) the branch named [name_of_branch]. Again, you want to make sure that you checkout the branch that you want to merge your code changes into.

Merging Branches

git merge [name_of_branch_2]

Merge the code in the branch named [name_of_branch_2] with the currently active (checked out) branch. Keeping with our example, I would have checked out the master branch in the previous step and now I am tell Git that I want to merge the changes in [name_of_branch] with my master branch.

Verifying a Successful Merge

git log

View Git’s log for the currently active branch. Now you can verify that the name of the most recent commit in your source branch matches the name of the most recent commit in the branch that you just merged.

git diff [name_of_branch]..[name_of_branch_2]

View the differences between the [name_of_branch] branch and the [name_of_branch_2] branch. Note: since you have just merged [name_of_branch_2] with [name_of_branch], Git will return nothing. This is Git’s way of telling you that there are no differences between the two branches… the merge was successful!

That covers the most simple merge where there are no merge conflicts to troubleshoot. I will update this post at a later time with how to troubleshoot merge conflicts.