git - 如何挑选文件的特定提交?

标签 git git-cherry-pick

我有 2 个分支:FeatureBranchReleaseBranch

我确实从 FeatureBranchReleaseBranch 挑选了一个提交 c1 的文件。 后来我对 FeatureBranch 上的同一文件做了一些修改,并使用 id c2 提交。

我再次增强了 FeatureBranch 上的同一文件,并使用 ID c3 提交。

现在我不想将 c2 放到 ReleaseBranch 上,我只需要 c3。我尝试直接使用 c3 id 进行挑选,但 Git 返回了错误:

could not apply c3...

我知道 Git 中的索引是增量的,因为我不希望 c2 更改,而我只需要 c3。有什么办法可以实现这一点吗?

最佳答案

第一:您正在维护两个不同版本的代码,一个在 FeatureBranch 上,一个在 ReleaseBranch 上。您开始看到拥有两个“实时”版本代码的负面影响。
我不知道您的限制是什么,但“将功能集成到发布中,或放弃功能”应该成为您近期计划中的优先里程碑。


您在该文件上遇到了冲突。 git 无法直接应用 c3,因为 c2 引入了一些更改,这些更改将补丁 c2 -> c3 变成了无法应用的内容在发布上。

您必须手动编辑冲突来修复它,然后提交结果。

如果c2中的更改很容易丢弃,另一种方法是丢弃当前的cherry-pick,获取文件的完整内容,然后对其进行编辑以删除不需要的更改想要在发布时提交:

# -- from ReleaseBranch

# cancel the cherry-picking :
git cherry-pick --abort

# get the content of the file stored in c3 (will contain modifs from c1+c2+c3) :
git checkout c3 -- path/to/file

# edit the file to remove unwanted code ...

# add and commit
git add path/to/file
git commit

关于git - 如何挑选文件的特定提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32986219/

相关文章:

git - 当有人从我的 git 提交中挑选并提交了他们自己的提交时,我该如何 merge ?

git - Cherry pick 仅提交涉及特定文件的内容

git - 樱桃选择问题 : changes from previous commits are also applied

git - 带有 git + 开发人员连接的 Maven 发布插件

git - 如何设置 git 来处理需要不同代理的 Remote ?

git - 如何从 tmux.conf 导出函数

git - 提交时 `.git/CHERRY_PICK_HEAD` 有什么区别?

git - 为什么 git 的 cherry picking 不止一次提交会失败?

eclipse - Cherry pick 到 Eclipse git 中的另一个分支

python - 如何在 Python 包中安全地 "fake"模块