Git merge

In this post, I will show how you can merge a git branch to the master. Also, I will explain some other related topics that you should care about when merging, creating branches & switching.

Let’s get started.

How to merge a git branch to the master?

There are two ways to merge a branch with the master branch. Such as using the command line & using GitHub. I will show you both ways to merge.

If you work alone, the command line would be the better option. If you work on a team and other team members need to review the code before merging, the second way would be good.

Merge a git branch using the command line

git merge command & process

To merge a git branch to the master branch, type the following command & hit enter:

git merge master

This will merge a feature branch to the master. But there is a caveat!

Before you merge a branch, make sure you switched or checked out to the master branch. And then perform the merge command that you see above.

Git merging is related to the branches. So here is a sidenote before you create a branch. You have to make sure that there are no uncommitted changes before you create a new branch.

Support me: if this post helped you, please share it on your website.

After you create a new branch, you can switch to the new branch by git checkout new_barnch_name. And after you finish work from that branch, you have to checkout to the master branch, and from there you have to perform the actual merge (git merge master).

This is one way of merging a branch to the master from the command line. But you can also merge from the GitHub repository.

All commands step-by-step:

  1. After you finish editing from a feature branch, stage all the changes by git add -A
  2. Make a commit from that feature branch by git commit -m "your message"
  3. Checkout to the master branch by git checkout master
  4. Merge the feature branch to the master by git merge feature_branch_name
  5. Push the changes to the remote origin by git push origin master

Troubleshoot: Not something we can merge

git command error: not something we can merge

Some of you may an error message after performing a git merge command such as “– not something we can merge” or something similar.

This is just because you misspelled the branch name when merging to master. To fix this error, type the correct branch name you want to merge to the master and hit enter.

How to merge a branch using GitHub (without the command line)

In the above section, you learn how to merge a branch using the command line. In this section, you will learn how to do it in GitHub.

After you complete work from a branch, you have to make a commit and push the uncommitted changes to the featured branch where you have been working on.

Git push to a specific branch that is not master

Let’s assume that you worked on a branch named “sidebar-changes.” So the command will be as follows:

git commit -am "finished working on sidebar"

git push origin sidebar-changes

After that, if you to your GitHub repository, you’ll see a new “Compare & pull request” button. Click on that button to open a pull request.

GitHub compare & pull request

You can write a custom description for this pull request if it needs to and assign this to another team member (or yourself). And click on the “Create pull request” button at the very bottom.

Create a pull request on GitHub

After you create the pull request, you or the assigned person can accept it by clicking the “Merge pull request” button.

merge pull request on GitHub

Finally, you’ll see a confirmation button, click on that to confirm the merge.

Confirm merge on GitHub

That’s it! This is how you can merge a branch using GitHub.

What to do after merging a branch using GitHub

After merging a branch using GitHub, if you go to the master branch on your command line, you’ll see that you don’t have the latest changes. Because the merge happened on GitHub and not on your local machine.

To get around this, you have to pull the latest changes from GitHub. Type the following command in your terminal and hit enter.

git pull origin master

This will grab the latest change from the remote origin (GitHub). To learn more about pulling, see this post.

Video instruction

The above video shows most of the things in action that I mentioned in this post.

Learn more about Git

Conclusion

Before you merge a branch, make sure you switched to the master branch and then perform the merge command.

In this post, I showed you two ways to merge a feature branch to the master. In my opinion, the command line interface is much easier than the GitHub UI. But if you work on a team and other people need to compare or check the code before merging, the second way is better. So you have to know both ways.