如何将另一个分支中的特定文件添加到我的分支中而不进行合并?

3

我有两个分支:

-- rakib/test1
   - file1, file2, file3, file4, filex, filey
-- rakib/test2
   - file3, file4, file5, file6

我想把rakib/test1file1file2添加到rakib/test2中,但是执行git merge rakib/test1操作会产生以下不良影响:
  1. 覆盖/修改了file3, file4
  2. 我不想在rakib/test2中有filex, filey
也许我漏掉了一些非常明显的东西。例如,在mergeadd中是否有任何选项可以实现此目的?还是我必须使用其他方法?
谢谢。
[编辑] 我尝试从这个问题中使用git merge --no-commit --no-ff rakib/test1,它继续自动合并:
rakib.amin@rakibamin-mac:~/AndroidAuto$ git merge --no-commit --no-ff rakib/test1
Performing inexact rename detection: 100% (4347/4347), done.
Auto-merging xxxxxxxxxx
CONFLICT (add/add): Merge conflict in xxxxxx
....
Automatic merge failed; fix conflicts and then commit the result.

[编辑] 重复问题要求我逐个文件执行,我还想知道如何基于目录/正则表达式进行操作,因为这对我而言是一个常规操作,用于向旧项目发起PR(那些我已经几个月没碰过的项目)。如果您知道在从旧分支获取代码时进行清洁分支的更好做法,请添加说明,我将非常乐意学习。


您可以在下面的链接中找到解决方案:https://dev59.com/xmgu5IYBdhLWcg3w_L3J - arun
抱歉,我刚刚执行了 git merge --no-commit --no-ff rakib/test1 命令,但似乎自动合并仍在进行中,导致出现合并冲突。 - user2453382
1
可能是重复的问题,参考如何从另一个分支获取单个文件 - phd
https://stackoverflow.com/search?q=%5Bgit%5D+files+another+branch - phd
1个回答

4
您可以尝试以下命令,将另一个分支中特定的文件复制到您的分支中。在此情况下,您只是从源分支检出一个文件,而不是合并整个分支。请注意,在这种情况下,您本地的文件将被完全覆盖为源分支的文件。"最初的回答"。
git checkout rakib/test2  # checkout your target branch 
git checkout rakib/test1 -- file1 # checkout only one file from source branch
git checkout rakib/test1 -- file2 # checkout only one file from source branch

最初的回答
这也适用于目录。
git checkout rakib/test1 -- dirname

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接