Git

How to Merge Another Developer’s Branch into Mine?

While working on a team project in Git, developers often work on separate branches to fix bugs or develop a new feature in their software development project. When each developer finishes his work on a branch, there can be a situation to merge that branch with the main project. Moreover, merging allows you to collaborate and share source code with each other. It also helps in integrating changes from multiple developers.

This blog will demonstrate the procedure to merge another developer’s branch into our project.

How to Merge Another Developer’s Branch into Our Project?

To merge another developer’s branch into your project, try out the below-provided steps:

  • Navigate to another developer’s repository and copy its HTTP URL.
  • Switch to the local directory.
  • Add another developer’s repository as a remote in your project.
  • Fetch remote repository content and choose the desired remote branch.
  • Merge the remote branch in your repository using the “git merge <remote-branch>” command.

Step 1: View Desired Branch Content
First, navigate to another developer’s project whose branch you want to merge into your project. For instance, we want to merge the “GitUser0422” developer’s “master” branch content into our project:

Step 2: Copy HTTPS URL
Next, copy the HTTP URL of another developer’s repository:

Step 3: Navigate to the Local Repository
Then, redirect to the particular local directory:

cd "C:\Git\test_Repo"

Step 4: Add Remote URL
Now, add the other developer’s repository as a remote in your project:

git remote add origin https://github.com/GitUser0422/first_demo.git

Step 5: Verify Remote URL
Ensure whether the remote has been added not through the below-provided command:

git remote -v

Step 6: Fetch Remote Changes
After that, run the provided command to fetch the changes of desired target branch:

git fetch origin

It can be seen that the other developer’s branches including content have been fetched into our local repository:

Step 7: View Remote Branches
To list the remote branch, type out the following command. Here, the “-r” option is used to list only the remote branches:

git branch -r

It can be seen that the current repository contains “main” and “master” two remote branches:

Step 8: Merge Desired Branch
Now, execute the “git merge” command along with the desired remote branch name that needs to be merged with the current local repository. For instance, we want to merge the content of the “master” branch:

git merge origin/master --allow-unrelated-histories

The below output indicates that the merge operation has been performed successfully:

Step 9: Verify Changes
Lastly, view the commit history to ensure that the remote branch has been merged successfully:

git log --oneline

As you can see, the remote branch has been merged with the project:

We have explained the method of merging another developer’s branch into our project.

Conclusion

To merge another developer’s branch into your project, first, copy the HTTP URL of another developer’s repository and add it as a remote in your project. Then, fetch the remote repository’s content. Next, view remote branches and choose the desired branch that you want to merge into your project. Lastly, execute the “git merge <remote-branch>” command and verify changes. This blog has demonstrated the procedure to merge another developer’s branch into our project.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.