How can I transfer a separate commit from one branch A to branch B in Git? In this case, the commit contains files that have been changed in branch B.
3 answers
Git has a special cherry-pick command that allows you to insert changes from any commit into the current branch. It works like this:
git cherry-pick test The example above applies the last commit from the test branch to the current branch.
If desired, you can transfer more than one commit at a time. For example:
git cherry-pick 8fe1498 mega_fix~3 v1.0.1 It is clear from the example above that a commit can be referenced at once in several ways:
- Using the SHA commit hash (or its shortened version).
- Using the name of the branch (you can use the syntax
^<N>and~<N>). - Using the tag that is marked commit.
More information on how to refer to a commit is in the documentation for Git .
It should be noted that, unlike a merge ( merge ), when cherry-pick only changes are transferred, and not the commit itself completely. In this case, a new commit is created in the target branch (commits) containing the necessary changes.
- There is still an opportunity to write a more detailed answer. ) - Nick Volynkin ♦
- @NickVolynkin, thanks for the tip. Now I’ll think what else to add =) - Dmitriy Simushev
- Nothing is said about how to throw a commit out of a source branch. - 4per
- @ 4per, and this is a completely different task. Moreover, it is not always necessary to throw out a commit after cherry-pick;) - Dmitriy Simushev
- Apparently, I misunderstand the meaning of the word “transfer”, as well as the “transfer” which is in the question that @NickVolynkin has rigidly associated with this (; - 4per
Look at git cherry-pick, for example, here . This command allows you to pull a specific commit from one branch and apply it to the current branch. At the same time, it will become a new commit. Well, maybe then you will need to resolve the merge conflicts.
git cherry-pick <hash коммита> find a commit by its sha1 hash and inject it into the current branch
Read more here .