20

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).

Braiam
  • 1
  • 11
  • 47
  • 78
Leif Andersen
  • 21,580
  • 20
  • 67
  • 100
  • 1
    Possible duplicate of [Push a commit in two branches with Git](http://stackoverflow.com/questions/4024095/push-a-commit-in-two-branches-with-git). It has a very good example with ASCII art. – jww May 02 '16 at 10:31
  • Possible duplicate of [Git: Commit to multiple branches at the same time](https://stackoverflow.com/questions/7259135/git-commit-to-multiple-branches-at-the-same-time) – Stevoisiak Aug 31 '17 at 19:28

2 Answers2

8

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)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • That would work, but I want to share B with some other people, so it needs to be public (and as such, I've already pushed it). Thank you though anyway. – Leif Andersen Dec 26 '10 at 00:15
5

the cherry-pick feature is a better way to do this, check answer at

Git: Commit to multiple branches at the same time

Community
  • 1
  • 1
spoutnik
  • 1,139
  • 8
  • 5