I have a project where I was originally using submodules for some dependent code. It turns out that submodules are not really appropriate for this project (and they are hard to use in practice), so I am converting each submodule to a subtree (using the new git-subtree
feature).
In my working repository, I have successfully removed each submodule and have added the old submodule repo as a subtree. No problem with this.
When I go to another clone and attempt to pull from the first one, I get the following error from the merge step:
error: The following untracked working tree files would be overwritten by merge:
sub/.gitignore
sub/Makefile
sub/README
sub/src/main.c
... and so on for all files in sub/
Aborting
It seems that this is because the files in sub/
never really existed in the main repository in the first place, and when Git applies the patch to update .gitmodules
it doesn't remove the directory with the submodule files. When processing the next commit, where Git tries to create the new files in sub/
that are now part of the main repository, all those files conflict with the still-existing files in sub/
.
The workaround I have found is to use rm -rf sub
before git pull
, which avoids this problem.
My question is, is there any command line switch I can use with git merge
that says "Overwrite any files that happen to exist in the working directory"? Even better would be a feature where git merge
would look at the contents of the existing file, and if the contents are identical to the file it was going to create anyway, suppress the error message and continue.
UPDATE: I have created Git repositories that demonstrate this problem, to show exactly what I'm talking about. To reproduce:
$ git clone https://github.com/ghewgill/q14224966.git
$ cd q14224966
$ git submodule init
$ git submodule update
$ git merge origin/branch
This should result in the error message
error: The following untracked working tree files would be overwritten by merge:
sub/Makefile
sub/README
sub/src/main.c
Please move or remove them before you can merge.
Aborting