Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commit to multiple branches at the same time with git

Tags:

git

I have two branches A and B in a project that I am working on. B differs from A by a single commit, which is a section of the code completely independent from what I'm working on for the next while (aka, I will have many commits I want to push to both branch A and B).

Is there any way in git that I can commit to both branch A and branch B at the same time, without having to commit it to one branch, checkout the other, and try to cherry pick out the commit(s).

like image 240
Leif Andersen Avatar asked Dec 25 '10 23:12

Leif Andersen


People also ask

Can I push same commit to multiple branches?

You can apply already existing commit to another branch using cherry-pick command, and then push both branches using git push origin branchA branchB .

Can you work on 2 git branches at the same time?

You can have many branches in your repository, but only one of these will be "checked out" as the working-tree so that you can work on it and make changes. git worktree adds the concept of additional working trees. This means you can have two (or more) branches checked-out at once.

Does git support commits across the multiple branches?

b) GIT does not support 'commits' across multiple branches or tags. Subversion allows the creation of folders at any location in the repository layout.

How does git work with multiple branches?

Git offers a feature referred to as a worktree, and what it does is allow you to have multiple branches running at the same time. It does this by creating a new directory for you with a copy of your git repository that is synced between the two directories where they are stored.


1 Answers

You could:

  • make all your commits on A
  • rebase B on top of A (if you haven't pushed B already, that is)

That way, B will include all commits from A, plus its single commit.


If you have shared B (pushed to a common remote repo), the idea is more to add any commit made on A to B (that is, "on top of B).

The simplest way would be to merge A on B, if you don't mind having only one commit on B representing all commits from A.
I would prefer that to any solution involving cherry-picking would mean different SHA1 for each commit recreated on B, which would make any future merge back to A complicated (because Git would go back a long way to find a common ancestor)

like image 63
VonC Avatar answered Sep 22 '22 10:09

VonC