部分樱桃采摘与Git的承诺 [英] Partly cherry-picking a commit with Git

查看:86
本文介绍了部分樱桃采摘与Git的承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理2个不同的分支:发布开发



我注意到我仍然需要将提交给发布分支的一些更改整合回开发分支。



问题是我不需要所有的提交,只有某些文件中的某些宏,所以简单的

  git cherry-pick bc66559 

不做招。



当我做一个

  git show bc66559 

我可以看到差异,但并不知道将这部分应用于当前工作树的好方法。

解决方案

$ c>( -p - patch 的同义词)。这提供了一种互动的方式来检查内容,让你决定是否每个人都应该进去,甚至让你手动编辑修补程序(如有必要)。



使用它与樱桃挑选组合:

  git cherry-pick -n< commit> #获取你的补丁,但不要提交(-n = --no-commit)
git reset#停止从樱桃选择提交中的更改
git add -p#使所有选项添加你想要的改变)
git commit#make commit!

(感谢Tim Henigan提醒我git-cherry-pick有一个--no-commit选项,并感谢Felix Rabe指出你需要重置!如果你只想让一些东西脱离提交,你可以使用 git reset< path> ... <

你当然可以提供指向 add -p 如果有必要。如果你从一个补丁开始,你可以用 apply 替换 cherry-pick






如果你真的想要一个 git cherry-pick -p< commit> 选项不存在),你可以使用

  git checkout -p< commit> 

这会将当前提交与您指定的提交进行比较,并允许您应用该差异个别。如果您所提交的提交合并了您不感兴趣的部分提交中的冲突,则此选项可能更有用。(但请注意, checkout 不同从> cherry-pick checkout 尝试应用< commit> 的内容, cherry-pick 应用指定提交从它的父级的差异,这意味着 checkout 可以应用的不仅仅是那个提交,这可能比你想要的要多。)


I'm working on 2 different branches: release and development.

I noticed I still need to integrate some changes that were committed to the release branch back into the development branch.

The problem is I don't need all of the commit, only some hunks in certain files, so a simple

git cherry-pick bc66559

does not do the trick.

When I do a

git show bc66559

I can see the diff but don't really know a good way of applying that partially to my current working tree.

解决方案

The core thing you're going to want here is git add -p (-p is a synonym for --patch). This provides an interactive way to check in content, letting you decide whether each hunk should go in, and even letting you manually edit the patch if necessary.

To use it in combination with cherry-pick:

git cherry-pick -n <commit> # get your patch, but don't commit (-n = --no-commit)
git reset                   # unstage the changes from the cherry-picked commit
git add -p                  # make all your choices (add the changes you do want)
git commit                  # make the commit!

(Thanks to Tim Henigan for reminding me that git-cherry-pick has a --no-commit option, and thanks to Felix Rabe for pointing out that you need to reset! If you only want to leave a few things out of the commit, you could use git reset <path>... to unstage just those files.)

You can of course provide specific paths to add -p if necessary. If you're starting with a patch you could replace the cherry-pick with apply.


If you really want a git cherry-pick -p <commit> (that option does not exist), your can use

git checkout -p <commit>

That will diff the current commit against the commit you specify, and allow you to apply hunks from that diff individually. This option may be more useful if the commit you're pulling in has merge conflicts in part of the commit you're not interested in. (Note, however, that checkout differs from cherry-pick: checkout tries to apply <commit>'s contents entirely, cherry-pick applies the diff of the specified commit from it's parent. This means that checkout can apply more than just that commit, which might be more than you want.)

这篇关于部分樱桃采摘与Git的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆