目录变为子模块后合并

时间:2015-08-04 23:50:01

标签: git version-control merge git-submodules git-merge

我发现使用git子模块时,我经常遇到在包含给定子模块的提交和代表与普通目录相同的代码的提交之间合并的问题。小复制例子:

# Create one project, to be used as a subproject later on
git init a
cd a
echo aaa > aa
git add -A
git commit -m a1
cd ..

# Create a second project, containing a as a normal directory initially
git init b
cd b
mkdir a b
echo aaa > a/aa
echo bbb > b/bb
git add -A
git commit -m b1

# Replace directory with submodule
git rm -r a
git submodule add ../a a
git commit -m b2

# Try to create branch from the pre-submodule state of affairs
git checkout -b branch HEAD^

这已经出错了:

error: The following untracked working tree files would be overwritten by checkout:
    a/aa
Please move or remove them before you can switch branches.
Aborting

为了避免错误,我首先取消初始化所有子模块:

# Create feature brach starting at version without submodule
git submodule deinit .
git checkout -b branch HEAD^
echo abc > b/bb
git commit -a -m b3

如您所见,功能分支与子模块完全无关,修改了一组不同的文件。这使整个问题特别烦人。

# Try to merge the feature branch
git checkout master
git merge branch

这再次失败,错误消息我不完全理解:

CONFLICT (file/directory): There is a directory with name a in branch. Adding a as a~HEAD
Automatic merge failed; fix conflicts and then commit the result.

如果我在git submodule update --init之前执行git merge branch,我会收到同样的错误。我在目录树和a~HEAD的输出中都没有看到任何git status,其内容如下:

On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")

Changes to be committed:

    modified:   b/bb

Unmerged paths:
  (use "git add <file>..." to mark resolution)

    added by us:     a

如果按照建议git add a,我会收到另一个错误:

error: unable to index file a
fatal: updating files failed

如果我在合并之前git submodules update --init,那么我可以成功git add a。但是如果我忘记这样做,然后在合并后尝试这样做,我会收到以下错误消息:

Submodule 'a' (…/a) registered for path 'a'
Skipping unmerged submodule a

如何从这种情况中恢复过来?git merge --abort以外的其他内容,因为我也希望将其用于git rebase之类的内容,以及在某些情况下(不知道如何重现)我甚至无法彻底中止合并,而是不得不进行硬重置。

我怎样才能首先避免它?是否有一些神奇的设置可以让git在合并期间使用子模块与目录做正确的事情,这样我就不必手动后处理只修改与子模块无关的文件的合并?

2 个答案:

答案 0 :(得分:3)

不,你不应该添加一个,合并不应该改变它。你应该跑的是

THREE.ImageUtils.crossOrigin = '';

所以你忽略的“chage”。

PS:显然git只是检查目录的存在,所以如果你

git reset a
在合并之前,它会成功。不确定这是不是你想要的。

答案 1 :(得分:0)

当我合并包含需要转换为子模块的文件夹的代码时,我遇到了类似的问题。

我通过使用 string[] lines = File.ReadAllLines(@"d:\Processed_Text.txt"); DataTable dt = new DataTable(); dt.Columns.Add("IP Address"); dt.Columns.Add("Date"); dt.Columns.Add("Time"); dt.Columns.Add("GMT"); dt.Columns.Add("Method"); dt.Columns.Add("Resource"); dt.Columns.Add("Port"); dt.Columns.Add("Bytes"); dt.Columns.Add("Services"); dt.Columns.Add("Destination"); dt.Columns.Add("Operating System"); foreach (string line in lines) { string[] split = line.Split(' ', ':'); split = split.Where(r => !string.IsNullOrWhiteSpace(r)).ToArray(); Foo foo = new Foo(); foo.IPAddress = split[0]; foo.Date = split[1]; foo.Time = string.Format("{0}:{1}:{2}", split[2], split[3], split[4]); foo.GMT = split[5]; foo.Method = split[6]; foo.Resource = split[7]; foo.Port = split[8]; foo.Bytes = split[9]; foo.Service = split[10]; foo.Destination = split[11]; foo.OS = split[12]; dt.Rows.Add(foo.IPAddress, foo.Date, foo.Time, foo.GMT, foo.Method, foo.Resource, foo.Port, foo.Bytes, foo.Service, foo.Destination, foo.OS); } using (StreamWriter sw = new StreamWriter(@"d:\Seperated_Text.txt")) foreach (DataRow dr in dt.Rows) { Console.WriteLine(string.Format("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10}", dr[0], dr[1], dr[2], dr[3], dr[4], dr[5], dr[6], dr[7], dr[8], dr[9], dr[10])); sw.WriteLine(String.Join(",", dr.ItemArray)); sw.WriteLine(dr); } Console.ReadLine(); } 从新源代码中删除文件夹来解决它,并BFG --delete-folders来清理存储库,然后我将git gc没有任何冲突。