Hitesh Garg
Hitesh Garg

Reputation: 13

How to Merge Multiple Continuous Git branches?

Let's say I am on branch branch-1. I did some changes and made 5 commits and opened a PR on GitHub repo.

Now, I did git checkout -b branch-2. Then I did some improvements and made 5 more commits. (notice that there are total 10 commits now in branch-2)

Now, I did git checkout -b branch-3. Then I did some improvements and made 5 more commits. (notice that there are total 15 commits now in branch-3)

Now, I did git checkout -b branch-4. Then I did some improvements and made 5 more commits. (notice that there are total 20 commits now in branch-4)

Now, There are 4 different PRs on GitHub repo corresponding to 4 branches.

Now, Here are some of my questions:-

  1. I did git checkout branch-2 and done some more improvements and committed them. now, how can I bring those changes back to branch-3 and branch-4?

  2. Is there any other efficient way to do work in this case? I mean when you have to create small PRs on Github repo but all new branches depend on previous branches.

    Thanks :)

Upvotes: 0

Views: 121

Answers (2)

phd
phd

Reputation: 94483

1: You can do it with merge or with rebase. In such linear case I prefer rebase.

git rebase branch-2 branch-1
git rebase branch-3 branch-2
git rebase branch-4 branch-3
  1. Create a new PR after the previous is accepted.

Upvotes: 0

janos
janos

Reputation: 124646

Don't create a new PR from a branch that depends on another PR. It makes it harder for the maintainer to review. If he looks at branch-2 before branch-1, he will see all the changes in both, which might be overwhelming all at once. And then, after reviewing branch-2 if he opens branch-1, he will waste his time reading stuff he has already reviewed.

If you want to create a new PR from a branch that depends on another PR, either wait until the first PR is merged, or consider deleting the first PR. This is still not a great option, because if you create a lot of PRs and then delete them to supersede by another PR, that can be a lot of noise.

Also ask yourself if these branches need to depend on each other. If not, then start new branches from origin/master, not from a pending PR.

If branch-1 received comments after you already started work on branch-2 which depends on branch-1, not a problem. Implement the requested improvements in branch-1, and once accepted, merge or rebase into branch-2. After branch-1 is accepted, when you create the PR for branch-2, the changes in branch-1, including the corrections, will not be visible anymore, the reviewer will see only the changes of branch-2.

Upvotes: 2

Related Questions