-1

Suppose I am in branch A. How can I merge branches B and C without checking out another branch?

Can I use this command? git merge B C

  • Is there any reason to do this? Why not `commit` or `stash` your changes on A then `checkout` the branch you want to merge to? – youngson May 08 '23 at 09:46
  • @syoung No, I just want to learn. – mohamadi_arch May 08 '23 at 09:49
  • 1
    I think it's idiomatic and standard practice to use `checkout`, but these threads might suffice. [1](https://stackoverflow.com/questions/3216360/merge-update-and-pull-git-branches-without-using-checkouts) [2](https://stackoverflow.com/questions/54447800/git-merge-with-master-without-checkout-to-master) [3](https://stackoverflow.com/questions/6777629/merge-branches-without-checking-out-branch) – youngson May 08 '23 at 09:54

2 Answers2

0

Nope.... you can't. For these cases, you can use temporarily a new worktree:

git worktree add blahblah B
cd blahblah
git merge C
cd ..
git worktree remove blahblah
eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • 1
    Or just stash and then come back to what you are doing at the time in the current worktree as is pretty customary. – eftshift0 May 08 '23 at 09:58
0

How can I merge branches B and C without checking out another branch?

There is no such thing as merging branches B and C. Merging is into. You can merge B into C, or you can merge C into B. They are very different things.

Either way, you must be on the branch you are merging into. This is because Git needs to use the working tree to enact the merge.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • For what the working tree is and how switching branches relates to it, see my https://www.biteinteractive.com/picturing-git-conceptions-and-misconceptions/ For more about what Git merge is, see my https://www.biteinteractive.com/understanding-git-merge/. – matt May 08 '23 at 10:28