Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push a commit in two branches with Git

Tags:

git

branch

push

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?

like image 783
Madoc Avatar asked Oct 26 '10 13:10

Madoc


People also ask

Can two branches have the same commit?

Yes, two branches can point to the same commits.

How do I push multiple branches to a remote?

git push origin will push from all tracking branches up to the remote by default. git push origin my-new-branch will push just your new branch.

Can git work with two branches at once?

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.


1 Answers

Short answer

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


Why pushing a commit in two branches might be useful

Assume you have a repository with this structure:

A--B--C--D  ← master ← HEAD       \--E  ← v1-release 

After some development (commits A, B, C) project was released and v1-release branch was created (so that v1 can be supported with bugfixes and next version can be developed in master). Commit E was used to specify version information (added release notes, etc). Commit D introduced new feature, which is planned for the next version and should not appear in v1-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:

A--B--C--D--F  ← master ← HEAD       \--E     ← v1-release 

Now commit F with a bugfix must be applied to v1-release branch.

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-pick command does exactly that. It applies changes made by a specified commit to the current branch, creating new commit:

git checkout v1-release git cherry-pick F 

After this, repository should look like this:

A--B--C--D--F  ← master       \--E--G  ← v1-release ← HEAD 

Commit G introduces same changes as F.

You may have to resolve conflicts (exactly like after merge).

Error message

The previous cherry pick was now empty ...

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 master branch and push both branches to remote repository:

git checkout master git push origin master v1-release 

Final repository structure:

A--B--C--D--F  ← master ← HEAD       \--E--G  ← v1-release 
like image 187
max Avatar answered Oct 04 '22 22:10

max