Merging/Copying 要从 Branch 掌握的特定文件

Merging/Copying Specific files to master from Branch

我运行按照以下命令查找在分支中更改但在主文件中没有更改的文件名:

git diff --name-status master..branchName

它给出了一个包含完整路径的文件名列表。

现在我已经在本地机器上克隆了 master 并想复制主控中那些更改过的文件。我说 copy 因为我不想合并 运行 冲突。

有没有办法获得某种在相关文件夹中有文件的补丁?而不是手动复制它们?

您可以将 git diff 命令的输出重定向到一个文件。

$ git diff master..branchName -- the/relevant/path1 the/relevant/path2 ... > my.patch

然后使用 git apply my.patch 一次应用所有更改。

另一种选择是签出您想要的 files/directories:

$ git checkout master
$ git chechout branchName -- the/relevant/path1 the/relevant/path2 ...

您想要的是让您的分支和存储库的工作目录和索引指向主提交。

方法是 git-reset:

git checkout yourBranch
git branch tempBranch   // It is allways safer create a temporal branch when you play with git-reset
git reset --soft master // This moves to master commit but does not touch your working directory and index area.