Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+3 votes
2 views
in DevOps and Agile by (29.3k points)
edited by

I'm new to Git, and now I'm in this situation:

I have four branches (master, b1, b2, and b3).

After I worked on b1-b3, I realized I have something to change on branch master that should be in all other branches.

I changed what I needed in master and... here is my problem:

How do I update all other branches with master branch code?

3 Answers

+3 votes
by (50.2k points)
edited by

You have two ways to resolve this problem 

Use Merge, but it creates an extra commit for merge

Alternatively, you can do a rebase.

Let’s see how this can be done: 

Checkout each branch:

git checkout b1

Then merge:

git merge origin/master

Then push:

git push origin b1

With rebase use the following commands:

git fetch

git rebase origin/master

Thus, you can update your git branch from the master. Here is an excellent tutorial for git please go through this link and you will get your basics of git more clear.

 

Update Git branches from master -git update branch from master
Intellipaat-community
by (19.4k points)
it would be better if you use rebase rather than merge.
0 votes
by (37.3k points)

In this case, you need to perform a git merge.
A Git merge combines the histories of two branches, ensuring that the updates from the master are reflected in your other branches.

In your case, using Git merge, you will be able to integrate changes from the master branch into your other branches (b1, b2, b3).

Steps:

  • Switch to Branch b1:

git checkout b1

  • Merge Master into b1:

git merge master

If there are conflicts, resolve them, then commit the changes.

  • Repeat Steps 1-3 for b2 and b3:

Switch to branch b2, merge master, and resolve conflicts.

Do the same for other branches

0 votes
by (1.2k points)

In the case your changes checked in to the master branch. Thus you will need to merge in one go all the other branches b1,b2,b3. It is shown below how that would be done:

Switch to each of the other branches. 

Start with b1: repeat everything after this sentence for each branch.

code:

git checkout b1

Merge master to into the branch: The changed are pulled from the master to b1

Code:

git merge master

That is, in case it doesn't have any conflicts, Git will merge changes from master to b1.

If it has conflicts, then solve conflict and commit as well as add changes with git add and git commit -m “your message”

Repeat the same for other branches: Repeat above commands for b2 and b3.

This will sync every one of the branches you've made (b1, b2, b3) with the master and all its latest updates. The more branches you create from here on out, these steps will update them to be in sync with the master as well.

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...