Git

How to Pull Master Into Branch in Git

Utilizing the Git platform while developing software permits you to create multiple branches for different modules of the projects. However, if the user wants to switch from the master branch, the changes made in this branch will not be automatically transferred to the other branches. To do so, it is required to add these changes with Git pull operation manually.

In this guide, we will provide the methods to pull a master into a branch in Git.

How to Pull Master Into Branch in Git using git pull?

On Git, you may need to pull the changes made in the “master” to a different branch. These changes cannot be transferred automatically. Therefore, users need to make them manually using the Git “$ git pull origin master” command. To do so, follow the below-provided steps.

Step 1: Open Git Bash
To open the “Git Bash” terminal on your system, search it using “Startup” menu:

Step 2: Move to Git Local Repository
Navigate to the Git local repository with the help of the “cd” command:

$ cd "C:\Users\nazma\Git\Master_Pull"

Step 3: Fetch Remote Repo Data
Next, execute the “git fetch” command with the remote name “origin”:

$ git fetch origin

The above-provided command will fetch the objects and references of the Git remote repository:

Step 4: Pull Master
Lastly, pull the master into branch using “git pull origin” command with branch “master”:

$ git pull origin master --allow-unrelated-histories

In the below output, the “master” branch is merged with another branch. Here, the “–allow-unrelated-histories” option is utilized to merge the history which does not share a common ancestor when merging the projects:

Now, move to the next section to pull the master into another branch using the “git rebase” command.

How to Pull Master Into Branch in Git Using git rebase?

You can also utilize the “git rebase” command to pull the master into the branch in Git. To do so, check out the given section.

Step 1: Fetch Remote Repo Data
First, fetch the metadata of the Git remote repository with the help of provided command:

$ git fetch origin

Step 2: Pull Master
Next, execute the “git rebase” command that will reapply commits on the top of the remote branch:

$ git rebase origin/master

As you can see, we have successfully rebased and updated the remote branch with local branch “master”:

We have compiled different methods to pull master into a branch in Git.

Conclusion

To pull the master into the branch in Git, first, move to the Git local repository and fetch all data and references of the remote repository into the local repository. Then, pull the master into the remote branch using the “$ git pull origin master” command. To pull into the master branch, the “$ git rebase origin/master” command can be utilized. In this guide, we have learned the procedure to pull a master into a branch in Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.