git cherry-pick 或从另一个分支合并特定目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19821749/
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-19 09:18:54  来源:igfitidea点击:

git cherry-pick or merge specific directory from another branch

git

提问by daleyjem

I've seen different posts on StackOverflow that explain cherry picking a bit, but the comments in their code aren't very specific as to what's a branch and what's a directory. Example git checkout A -- X Ydoesn't tell me much.

我在 StackOverflow 上看到过不同的帖子,这些帖子解释了樱桃的选择,但是他们代码中的注释对于什么是分支和什么是目录并不是很具体。例子git checkout A -- X Y并没有告诉我太多。

Basically I want this:

基本上我想要这个:

  • Create new branch featureAoff of master
  • Merge directory /tools/my-toolfrom branch devinto featureA
  • 创建新分支featureA的关master
  • 将目录/tools/my-tool从分支合并devfeatureA

回答by Ian

To answer the original question about how to cherry-pick some directories (as commits instead of a brute-force checkout), this is possible. Imagine that featureAhas diverged from masterand you want to bring over the tools/my-toolcommits.

要回答有关如何挑选某些目录(作为提交而不是蛮力结帐)的原始问题,这是可能的。想象一下,这featureA已经偏离了,master并且您想要带来tools/my-tool提交。

Assuming that you never made any commits that contain both stuff from /tools/my-tooland stuff from other directories

假设您从未进行任何包含/tools/my-tool来自其他目录和来自其他目录的内容的提交

This will get you the list of commits to masterin tools/my-tool(that are not already in featureA), in reverse-chronological order:

这将按时间倒序为您提供masterin tools/my-tool(尚未在 中featureA)的提交列表:

git log --no-merges featureA...master tools/my-tool

To say it another way:

换一种说法:

git log --no-merges source_branch...dest_branch my/firstpath my/secondpath [...]

To get just the commits you need in chronological order, you need to first reverse the order of the input lines (such as with tail -ror tac), then isolate the column for the commit hash (such as with cut):

要按时间顺序仅获取您需要的提交,您需要先颠倒输入行的顺序(例如 withtail -rtac),然后隔离提交哈希的列(例如 with cut):

git log --format=oneline --no-merges featureA...master tools/my-tool \
    | tail -r \
    | cut -d " " -f 1

And to do the whole operation at once, do this:

并立即完成整个操作,请执行以下操作:

git cherry-pick $(git log --format=oneline --no-merges featureA...master tools/my-tool | tail -r | cut -d " " -f 1)

回答by VonC

Note:

笔记:

  • git cherry-pickis about applying a full commit (or commits) to another branch. There is no notion of "path".
  • git checkoutis about updating the working tree (and HEADif no path is specified, effectively switching branches)

    git checkout [-p|--patch] [<tree-ish>] [--] <pathspec>...
    
  • git cherry-pick是关于将完整提交(或提交)应用到另一个分支。没有“路径”的概念。
  • git checkout是关于更新工作树(HEAD如果没有指定路径,有效地切换分支)

    git checkout [-p|--patch] [<tree-ish>] [--] <pathspec>...
    

When <paths>or --patchare given, git checkoutdoes not switch branches.
It updates the named paths in the working tree from the index file or from a named <tree-ish>(most often a commit). The <tree-ish>argument can be used to specify a specific tree-ish (i.e. commit, tag or tree) to update the index for the given paths before updating the working tree.

<paths>--patch给出时,git checkout不切换分支
它从索引文件或命名<tree-ish>(通常是提交)更新工作树中的命名路径。该<tree-ish>参数可用于指定特定的树形(即提交、标记或树)以在更新工作树之前更新给定路径的索引。

Your git checkout dev -- tools/my-toolupdates a specific path, but it isn't a "merge" or a "git cherry-pick".

git checkout dev -- tools/my-tool更新了特定路径,但它不是“合并”或“git cherry-pick”。

回答by Fehr

Jason Rudolph does a great job of summarising this scenario, and the accepted solution in this post:

Jason Rudolph 很好地总结了这个场景,以及这篇文章中公认的解决方案:

https://jasonrudolph.com/blog/2009/02/25/git-tip-how-to-merge-specific-files-from-another-branch/

https://jasonrudolph.com/blog/2009/02/25/git-tip-how-to-merge-specific-files-from-another-branch/

This is a good old question, and the above answers did a great job of answering it, but since I recently came across the issue, and his article stated it so concisely I thought I'd share it here.

这是一个很好的老问题,上面的答案很好地回答了它,但由于我最近遇到了这个问题,而且他的文章如此简洁地陈述了它,我想我会在这里分享它。

回答by funerr

You can use git checkout <from_branch> -- <files_to_bring>.
I'd do this: git checkout dev -- tools/my-tool

您可以使用git checkout <from_branch> -- <files_to_bring>.
我会这样做:git checkout dev -- tools/my-tool

Explanation: this tells git to replace (if something is there already) / insert files from the branch dev and the path tools/my-toolto your current branch and put those files in the relative path you gave.

解释:这告诉 git 替换(如果已经存在)/从分支 dev 插入文件和tools/my-tool当前分支的路径,并将这些文件放在您提供的相对路径中。