Git:将特定提交移动到另一个分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14697801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:26:04  来源:igfitidea点击:

Git: move specific commits to another branch

gitgit-commit

提问by johnode

A have repository with two branches.

有两个分支的存储库。

Master branch commits:

主分支提交:

c1, c2, c3, c4, c5, c6, c7, ..., c15, ...

c1, c2, c3, c4, c5, c6, c7, ..., c15, ...

Staging Branch commits:

暂存分支提交:

c1, c2, c3, c4, c5, c6, c7

c1、c2、c3、c4、c5、c6、c7

I want to move all commits from Master branch after c7to staging branch

我想在c7之后将所有提交从 Master 分支移动到暂存分支

and then revert Master branch

然后恢复主分支

with

git reset --hard c7-hash

How to move/copy specific commits from one branch to another ?

如何将特定提交从一个分支移动/复制到另一个分支?

回答by Jonathan Wakely

In the case you've described, where all commits on the staging branch are also on the master branch, it's very easy:

在您描述的情况下,暂存分支上的所有提交也都在主分支上,这非常简单:

git checkout staging
git merge master
git checkout master
git reset --hard c7-hash

The merge will be a fast-forward.

合并将是一个快进。

In the general case, you can use git cherry-pick c8 c9 c10 c11 c12 c13 c14 c15to cherry pick individual commits to the current branch. A shorter way to cherry pick all commits that are on master but not the current branch is git cherry-pick ..master, and there are other examples shown by git help cherry-pick

在一般情况下,您可以使用git cherry-pick c8 c9 c10 c11 c12 c13 c14 c15挑选单个提交到当前分支。一种更短的方式来挑选所有在 master 上而不是当前分支上的提交,git cherry-pick ..master还有其他例子显示git help cherry-pick