how do I push a commit in two branches?
I can’t use “git push”, because then it pushes to three branches, and i just want the commit in two of them..
Ive tried a “git merge HEAD –commit id from branch A–” in branch B, but then it takes everything from branch A and merges with branch B. I just want the last commit and not everything else merged with branch B.
Anyone know what to do?
Short answer
You can apply already existing commit to another branch using
cherry-pickcommand, and then push both branches usinggit push origin branchA branchB.Why pushing a commit in two branches might be useful
Assume you have a repository with this structure:
After some development (commits
A,B,C) project was released andv1-releasebranch was created (so that v1 can be supported with bugfixes and next version can be developed inmaster). CommitEwas used to specify version information (added release notes, etc). CommitDintroduced new feature, which is planned for the next version and should not appear inv1-release.Now, if a bug is found in
v1-release, it must be fixed in both branches, so that users can continue using v1 and it does not appear in the next version.After fixing the bug in
master, repository should look like this:Now commit
Fwith a bugfix must be applied tov1-releasebranch.How to actually do it
Commits cannot be exactly copied (since commit is a directory saved state), but you can apply changes made in commit to another commit.
cherry-pickcommand does exactly that. It applies changes made by a specified commit to the current branch, creating new commit:After this, repository should look like this:
Commit
Gintroduces same changes asF.You may have to resolve conflicts (exactly like after merge).
Error message
means that changes made by cherry-picked commit are already present in current branch. You probably forgot to checkout correct branch.
In case of errors or conflicts cherry-pick can be aborted using
git cherry-pick --abort.Finally, you can return to
masterbranch and push both branches to remote repository:Final repository structure: