2

Is there a way in Git to converge 2 branches with a single command?

Suppose I have a branch feature1 where I'm developing a feature and another branch feature2 where I'm developing another feature.

Now I would like to converge the 2 features since the development of feature1 needs something developed on feature2 branch and vice-versa.

AFAIK, I can achieve this by doing 2 merge, checking out feature1, merge feature2 into it, then checking out feature2 and merge feature1 into it.

Is there a way to make the 2 branches converging into 1 point of merge with a single command? Next they continue again in their 2 separate branches.

6
  • You can just checkout feature1 and merge from feature2. Then forget (or even better delete) feature2 Commented Aug 24, 2018 at 12:34
  • You could merge feature2 into feature1 and that would be your convergence point. Then you point your feature2 branch to it and here you go Commented Aug 24, 2018 at 12:35
  • @black_fm: what do you mean with "you point your feature2 branch to it"? Delete branch `feature2' after merge and create it again? Commented Aug 24, 2018 at 12:39
  • @ErniBrown: if I delete feature2 and then I create it again (since development will continue on it even after merge) what happen to other people working on it after I pushed it? Commented Aug 24, 2018 at 12:40
  • @ABCplus a complete answer is a little complex, let just say that when you delete a branch from the remote (suppose origin) you are just deleting its ref, not all the commits the branch is made of. If someone push again feature2 to origin then another feature2 ref will be created (if allowed), with the same history of the old one. Be careful when deleting a branch someone else is working on Commented Aug 24, 2018 at 12:59

1 Answer 1

5

No this feature is not available with git out of the box. However you can create an alias:

git config --global alias.sync="!git checkout $1 && git merge $2 && git checkout $2 && git merge $1"

Then you would just do:

git sync feature1 feature2

And it would do the two merges for you

Sign up to request clarification or add additional context in comments.

1 Comment

Keep in mind that by default doing 2 merges should work fine, but if you've configured git to not use fast-forwards then this will make a messy history. Another option, after the first merge, is to directly relocate the other branch. There are several ways and I don't know a reason to prefer a specific one, but git branch -f $2 would be one option

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.