Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
565 views
in Technique[技术] by (71.8m points)

branch - Push a commit in two branches with Git

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...