Git 创建包含两个分支之间目录差异的新分支

Git 创建包含两个分支之间目录差异的新分支,git,git-branch,Git,Git Branch,我有一个多分支的项目。使用以下命令,我可以看到存在于branch2上的提交,但不存在于branch1上: git log branch1..branch2 path/to/dir 我想从branch1创建一个新分支,其中包含执行上述命令时显示的提交 例如: 在branch1: git log path/to/dir commit1 commit2 git log path/to/dir commit1 commit2 commit3 commit4 在bra

我有一个多分支的项目。使用以下命令,我可以看到存在于
branch2
上的提交,但不存在于
branch1
上:

git log branch1..branch2 path/to/dir
我想从
branch1
创建一个新分支,其中包含执行上述命令时显示的提交

例如:

在branch1:

git log path/to/dir
  commit1
  commit2
git log path/to/dir
  commit1
  commit2
  commit3
  commit4    
在branch2上:

git log path/to/dir
  commit1
  commit2
git log path/to/dir
  commit1
  commit2
  commit3
  commit4    
然后,我上面提到的命令将返回以下内容:

git log branch1..branch2 path/to/dir
  commit3
  commit4
我想找到一种方法来创建一个新分支,它的基础是
branch1
,我们称之为包含这两个提交的
integrationbranch

git log
在这里可能不是正确的第一步,但它可以帮助我说明我想要实现的目标。我认为存在一种高级形式的
git merge
,它允许将一个分支部分合并到另一个分支中


我的另一个选择是首先在
branch1
的基础上创建
integration分支,然后从
branch2
仅合并
path/to/dir
的更改,如果我理解正确,您想创建一个新分支,该分支从
branch1
开始,只包含来自
branch2
的、接触
path/to/dir
的提交。最好的方法是使用
git cherry pick
。我会这样做:

// create a new branch to work on
$ git checkout -b newbranch branch1
// grab the commit IDs we want and cherry-pick them
$ git cherry-pick $(git log --pretty=format:%H --reverse branch1..branch2 -- path/to/dir)

如果未选择的提交尝试对那些违反修补程序先决条件的文件进行更改,因为已跳过了中间提交,则除了
path/to/dir
之外的文件中可能存在冲突。解决这一问题需要进行有趣的提交,生成仅限于对有趣文件所做更改的修补程序,并按顺序手动应用这些修补程序,如果我们在这里讨论大量提交,这可能会有点乏味…

不清楚您想要什么。首先,log命令的输出实际上是一个log。大概,您想要的是实际的提交,而不是日志消息。其次,您是否只需要匹配的提交?重新编写了问题并包含示例。现在明白了吗?是的,你理解正确了。我知道cherry picking,但错过了获取提交ID集的子shell。我会试一试,如果有效的话,我会把它标记为已回答。非常感谢。