部分选择使用 Git 提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1526044/
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
Partly cherry-picking a commit with Git
提问by oliver
I'm working on 2 different branches: releaseand development.
我正在 2 个不同的分支上工作:release和development。
I noticed I still need to integrate some changes that were committed to the releasebranch back into the developmentbranch.
我注意到我仍然需要将一些提交到发布分支的更改集成回开发分支。
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.
我可以看到差异,但真的不知道将其部分应用于我当前工作树的好方法。
回答by Cascabel
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.
您在这里想要的核心内容是git add -p
(-p
是 的同义词--patch
)。这提供了一种签入内容的交互方式,让您决定是否应该进入每个大块头,甚至可以让您在必要时手动编辑补丁。
To use it in combination with cherry-pick:
与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.)
(感谢 Tim Henigan 提醒我 git-cherry-pick 有一个 --no-commit 选项,感谢 Felix Rabe 指出你需要重置!如果你只想在提交中留下一些东西,您可以git reset <path>...
用来取消暂存这些文件。)
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
.
add -p
如有必要,您当然可以提供特定的路径。如果你已经从一个补丁,你可以替换cherry-pick
使用apply
。
If you really want a git cherry-pick -p <commit>
(that option does not exist), your can use
如果你真的想要一个git cherry-pick -p <commit>
(那个选项不存在),你可以使用
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.)
这会将当前提交与您指定的提交进行比较,并允许您单独应用该差异中的大块。如果您正在拉入的提交在您不感兴趣的部分提交中存在合并冲突,则此选项可能更有用。 (但是,请注意,这checkout
不同于cherry-pick
:checkout
尝试<commit>
完全应用的内容,cherry-pick
应用来自它的父级的指定提交。这意味着checkout
可以应用的不仅仅是那个提交,这可能比你想要的更多。)
回答by Mike Monkiewicz
I know I'm answering an old question, but it looks like there's a new way to do this with interactively checking out:
我知道我在回答一个老问题,但看起来有一种新的方法可以通过交互式签出来做到这一点:
git checkout -p bc66559
Credit to Can I interactively pick hunks from another git commit?
回答by Jay Swain
Assuming the changes you want are at the head of the branch you want the changes from, use git checkout
假设您想要的更改位于您想要更改的分支的头部,请使用 git checkout
for a single file :
对于单个文件:
git checkout branch_that_has_the_changes_you_want path/to/file.rb
for multiple files just daisy chain :
对于多个文件只是菊花链:
git checkout branch_that_has_the_changes_you_want path/to/file.rb path/to/other_file.rb
回答by Christian.D
Building on Mike Monkiewiczanswer you can also specify a single or more files to checkout from the supplied sha1/branch.
基于Mike Monkiewicz 的回答,您还可以指定一个或多个文件以从提供的 sha1/branch 中签出。
git checkout -p bc66559 -- path/to/file.java
This will allow you to interactively pick the changes you want to have applied to your current version of the file.
这将允许您以交互方式选择要应用于当前文件版本的更改。
回答by nyanpasu64
If you want to specify a list of files on the command line, and get the whole thing done in a single atomic command, try:
如果要在命令行上指定文件列表,并在单个原子命令中完成整个操作,请尝试:
git apply --3way <(git show -- list-of-files)
git apply --3way <(git show -- list-of-files)
--3way
: If a patch does not apply cleanly, Git will create a merge conflict so you can run git mergetool
. Omitting --3way
will make Git give up on patches which don't apply cleanly.
--3way
:如果补丁没有完全应用,Git 将创建合并冲突,因此您可以运行git mergetool
. 省略--3way
将使 Git 放弃应用不干净的补丁。
回答by adrock20
Use git format-patch
to slice out the part of the commit you care about and git am
to apply it to another branch
使用git format-patch
到出片的一部分提交您的关心和git am
将它应用到另一个分支
git format-patch <sha> -- path/to/file
git checkout other-branch
git am *.patch
回答by Kaz
If "partly cherry picking" means "within files, choosing some changes but discarding others", it can be done by bringing in git stash
:
如果“部分樱桃采摘”意味着“在文件内,选择一些更改但丢弃其他更改”,则可以通过引入git stash
:
- Do the full cherry pick.
git reset HEAD^
to convert the entire cherry-picked commit into unstaged working changes.- Now
git stash save --patch
: interactively select unwanted material to stash. - Git rolls back the stashed changes from your working copy.
git commit
- Throw away the stash of unwanted changes:
git stash drop
.
- 做完整的樱桃采摘。
git reset HEAD^
将整个精心挑选的提交转换为未暂存的工作更改。- 现在
git stash save --patch
:以交互方式选择要隐藏的不需要的材料。 - Git 从您的工作副本中回滚隐藏的更改。
git commit
- 扔掉不需要的更改:
git stash drop
.
Tip: if you give the stash of unwanted changes a name: git stash save --patch junk
then if you forget to do (6) now, later you will recognize the stash for what it is.
提示:如果您为不需要的更改的存储命名:git stash save --patch junk
那么如果您现在忘记执行 (6),稍后您将识别存储的内容。