合并两个类似的GIT存储库但排除特定目录

时间:2017-06-16 13:41:25

标签: git merge repository

我有2个具有相似目录结构的git存储库(例如A和B)但在合并分支“DEV”期间,从A到B,我想要排除A中存在但不存在的特定目录/文件/目录结构在B。

对于A和B中已经存在的每个文件,我想保留repo B的版本,我在每个包含此类文件的目录中都有一个.gitattributes文件,并为每个文件包含一行,例如{{ 1}}。

目前我遵循以下流程:

  1. 运行routes-repo.ts merge=ours
  2. 在项目B的目录根目录中运行git config merge.ours.driver true
  3. 运行git fetch A DEV
  4. 运行git fetch A DEV
  5. 以上确实同步了存储库,但是项目A中存在的所有新内容也会同步到repo B.同步只能从A到B完成,但不能反过来。

    如何从同步中排除特定文件/目录?

1 个答案:

答案 0 :(得分:0)

Apparently, looking at your work tree a file at a time, there are three cases:

1) Files that exist in both repos, but you want the version from Repo B; you have a solution for this

2) Certain files that exist only in A, which you want to leave out of the merge; you don't have a solution for this yet

3) Everything else, which can merge normally

So focusing on (2), you wonder (in comments) if .gitignore will help. It won't. The ignore rules only affect how git treats untracked files; it doesn't do anything on merges.

Because the files don't exist in Repo B, there will be no file merge, so using .gitattributes to set a merge handler won't work either.

So what will work?

You could issue the merge command with the --no-commit option.

git merge --no-commit A/DEV

This will tentatively complete the merge exactly the way it looks now, but it won't automatically generate a merge commit. Instead it will stop and let you make whatever changes you want. So

git rm -r a/directory/you/wanted/to/exclude
# ... etc
git commit

You might prefer something more automatic, especially if the merge will be repeated going forward. I don't have a perfect solution for that. You can expect that files you deleted during the 1st merge should "stay deleted", though you will get a merge conflict if Repo A has made further changes to the file. (You could try including a path like a/directory/you/wanted/to/exclude/**/* merge=ours in a .gitattributes file at the work tree root; it might help.)

But if Repo A creates new files in those directories between merges, then you're back to having to manually remove those in the merge process (because git thinks in terms of files, not directories).

You could possibly set up a merge hook in repo B, to at least warn you when clean-up is needed.

相关问题