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
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:
- After you finish editing from a feature branch, stage all the changes by
git add -A
- Make a commit from that feature branch by
git commit -m "your message"
- Checkout to the master branch by
git checkout master
- Merge the feature branch to the master by
git merge feature_branch_name
- Push the changes to the remote origin by
git push origin master
Troubleshoot: 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.
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.
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.
After you create the pull request, you or the assigned person can accept it by clicking the “Merge pull request” button.
Finally, you’ll see a confirmation button, click on that to confirm the merge.
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
- What is Git and why it is used?
- How to install Git on Windows & Mac?
- How to use Git with Visual Studio Code?
- How to use Git and GitHub?
- What is the difference between Git and GitHub?
- How to connect local Git to a remote server?
- How to change Git remote origin URL?
- How to create a Git repository on GitHub?
- Git add all (stage all changes)
- How to undo the git add -A command?
- How to undo the last Git commit?
- How to change the git commit message?
- How to remove or unstage a file from Git commit?
- How to git push after rebase?
- Git switch branches: How to checkout the git branch?
- How to merge a git branch to master?
- Git branch: create, checkout, merge, list, command, delete & push
- How to clone a Git repository?
- How to git pull to override the local project?
- How to remove file from Git?
- When should you use git rebase?
- Git commands & explanation (downloadable cheatsheet included)
- How to create GitHub Pages?
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.