Saturday, September 21, 2024
Google search engine
HomeGuest BlogsGit Merge Master into Branch

Git Merge Master into Branch

Introduction

Git is a version control system that allows users to work in several development lines called branches. The master (or main) branch is usually the default branch in a repository, created by default by the git init command.

Although it is not usually the practice to merge the master branch into other branches, there are some exceptions to the practice.

In this tutorial, you will learn when and how to merge the master branch into another branch.

Merge the master branch into a different one in Git - a tutorial.Merge the master branch into a different one in Git - a tutorial.

Prerequisites

Merging Master into Branch in Git

The master branch is just like any other branch in Git. It is not special in any way other than the fact it is created by default when initializing the repository. However, since all development starts from the master branch, be cautious when merging it into other branches.

The exceptions for merging the master into another branch are when you need to merge a hotfix or a new release branch. There are two methods you can use:

  • git merge. The git merge command creates a new merge commit and preserves the development history.
  • git rebase. The git rebase command rewrites and creates a linear development history.

Although both commands work, git rebase should be used when you are working alone on the project, while git merge is more appropriate for teams.

The sections below show different methods for merging the master branch into another branch.

Method 1: Merge with git rebase

Use git rebase when you are working only in the local repository, or when working with a remote repository that is not publicly visible. Rebasing when you are collaborating with a team can cause confusion because the commits are rearranged and the history is rewritten.

Follow the steps below to rebase the master branch:

1. Open a Git bash window or terminal in Linux and navigate to the directory with your Git repository.

2. Switch to the branch you want the master branch to merge into. Use the git checkout or git switch command.

For example:

Switching branches in Git.Switching branches in Git.

3. Run the following command to rebase the branch:

git rebase master
Rebasing the master branch in Git.Rebasing the master branch in Git.

Resolve any conflicts that arise and the rebase is complete.

Method 2: Merge with git merge

Use git merge when you are collaborating on a project and want to keep the development history intact. Merging is also recommended when the repository is publicly available.

Follow the steps below:

1. Open Git bash or a new terminal window and navigate to the repository directory.

2. Switch to the master branch and pull to download any changes from the remote repository. Use these two commands:

git switch master
git pull

3. Switch to the target branch and perform git pull again to update the commits:

git pull

4. Execute the git merge command to merge the master branch into the target branch:

git merge master
Merging the master branch in Git.Merging the master branch in Git.

Resolve any merge conflicts that arise and complete the merge.

5. Push the changes to the remote repository:

git push origin
Pushing changes from local repository to a remote one.Pushing changes from local repository to a remote one.

The changes are now visible to other developers in the remote repository.

Note: Comparing two branches is a good practice before merging. Learn how to do it by referring to our article How to Compare Two Git Branches.

Conclusion

This tutorial showed how to merge the master branch into another branch using git merge and git rebase. Both commands perform the same action, but in different ways, making them suitable for different situations.

For more Git tutorials, check out our Git beginner’s guide, or learn how to secure your Git installation with SSH or HTTPS.

Was this article helpful?
YesNo

RELATED ARTICLES

Most Popular

Recent Comments