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
git cherry-pick or merge specific directory from another branch
提问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 Y
doesn't tell me much.
我在 StackOverflow 上看到过不同的帖子,这些帖子解释了樱桃的选择,但是他们代码中的注释对于什么是分支和什么是目录并不是很具体。例子git checkout A -- X Y
并没有告诉我太多。
Basically I want this:
基本上我想要这个:
- Create new branch
featureA
off ofmaster
- Merge directory
/tools/my-tool
from branchdev
intofeatureA
- 创建新分支
featureA
的关master
- 将目录
/tools/my-tool
从分支合并dev
到featureA
回答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 featureA
has diverged from master
and you want to bring over the tools/my-tool
commits.
要回答有关如何挑选某些目录(作为提交而不是蛮力结帐)的原始问题,这是可能的。想象一下,这featureA
已经偏离了,master
并且您想要带来tools/my-tool
提交。
Assuming that you never made any commits that contain both stuff from
/tools/my-tool
and stuff from other directories
假设您从未进行任何包含
/tools/my-tool
来自其他目录和来自其他目录的内容的提交
This will get you the list of commits to master
in tools/my-tool
(that are not already in featureA
), in reverse-chronological order:
这将按时间倒序为您提供master
in 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 -r
or tac
), then isolate the column for the commit hash (such as with cut
):
要按时间顺序仅获取您需要的提交,您需要先颠倒输入行的顺序(例如 withtail -r
或tac
),然后隔离提交哈希的列(例如 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-pick
is about applying a full commit (or commits) to another branch. There is no notion of "path".git checkout
is about updating the working tree (andHEAD
if 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--patch
are given,git checkout
does 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-tool
updates 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-tool
to your current branch and put those files in the relative path you gave.
解释:这告诉 git 替换(如果已经存在)/从分支 dev 插入文件和tools/my-tool
当前分支的路径,并将这些文件放在您提供的相对路径中。