How to Git- Branching & Merging
In this guide, we are going to learn Branching & Merging in Git Version Control System with examples in Git Bash and GitHub. A branch allows the user to switch between these versions so that he can work on different changes independently from each other. Merging allows you to combine the changes which were created by two different branches.
Using branch, you can work on different versions of your collection of files. A branch allows you to create variations of your codes. It isolates them from master branch changes. The default branch is most often called master.
Let’s create a branch to our project in different methods.
Create branch using Git Bash
You can create a new branch using
git branch branch_name command. The following example will create a branch named “aboutus-updates”.
yp@yp-PC MINGW64 ~/simple-website (master) $ git branch contact-updates
Now we have created a branch called aboutus-changes. i.e. we have another variation of code. But we are not on the new branch. Currently, we are still on the master branch. Now, we need to switch to our branch. Use
git checkout branch_name command to switch branch. Let’s go back to our example and switch it.
yp@yp-PC MINGW64 ~/simple-website (master) $ git checkout aboutus-changes Switched to branch 'aboutus-changes' yp@yp-PC MINGW64 ~/simple-website (aboutus-changes)
Now, we are on the aboutus-changes branch. If you make any changes to the repository, it will only affect to aboutus-changes branch, not master branch. Now we have two completely isolated versions of our code. we have a master branch and aboutus-changes branch.
Create branch using Github Desktop
To create a branch on GitHub Desktop, Locate Create a new branch icon on the top of GitHub desktop and click. Fill out branch name and create a new branch.
Once you’ve created the new branch, you can publish it to GitHub by clicking on Publish button.
Let’s go ahead and make some changes to our code to understand branching and merging. Let’s add some text to about.html to view some changes on our branch. Before you do the changes, make sure that you’re on the corresponding branch that we are working for.
The following image shows you the changes that made on about.html. Commit the file to aboutus-changes branch.
Once you commit the changes, you can view Git diagram on Git Desktop with each commit on each branch. This diagram grows e the projects become bigger. Let’s go ahead and learn how to merge the Git branches.
If we want to get rid of aboutus-changes branch and take the changes from aboutus-changes and push it on the master branch, then we have to do merging. Git Merging combines the changes which were created by two different branches. You can merge based on branches, tags or commits.
git merge branch_name command will merge the
branch_name with current branch. Let us go back to the aboutus-changes branch and merge it with the master branch.
Merge branch using Git Bash
The following code will merge the currently active branch aboutus-changes to the master branch.
git checkout aboutus-changes git merge master
Once yo complete the merging, delete the aboutus-changes branch using the following command.
git branch -D aboutus-changes
Merge branch using GitHub Desktop
If you are using GitHub Desktop, you can merge using Pull request button. The following example will merge the currently active branch aboutus-changes to the master branch.
Click on Pull request. and fill out an optional description and send the pull request. The pull request will send to your GitHub account and it will automatically sync with GitHub.
To complete the merge, open up GitHub’s web interface, locate our pull request and click on merge the request.
When pulling from a remote repository, prefer doing a rebase to a merge. This will help to keep the history easier to read. For more details about Git merge see the documentation.