Merge error after converting Git submodule to subtree

后端 未结 4 1345
时光说笑
时光说笑 2021-02-07 05:36

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

4条回答
  •  独厮守ぢ
    2021-02-07 06:19

    I know your question was specific to merge, but I've had similar problems with merging git submodules. I think this solution will work with your problem, even if it's not directly addressing the merge question.

    I've found that by forcibly checking out the branch you want to merge, then going back to master, everything works out with submodules.

    To get things working in your example:

    $ git clone https://github.com/ghewgill/q14224966.git
    $ cd q14224966
    $ git submodule init
    $ git submodule update
    $ git checkout -f origin/branch
    $ git checkout master
    $ git merge origin/branch
    

    This works because it's basically doing your rm -rf step for you. Granted, this is a bit roundabout, and maybe not worth doing if you only have the one submodule like your example does. But I've found it to be quite the timesaver when working in a project with many submodules.

    Also, as has been pointed out in the comments, if you want to avoid making changes to the work tree, you can use this:

    $ git clone https://github.com/ghewgill/q14224966.git
    $ cd q14224966
    $ git submodule init
    $ git submodule update
    $ git reset origin/branch
    $ git reset --hard master
    

    This works in roughly the same way, but avoids checking out other files in the process. I haven't had a chance to use this in the wild, but it seems like a sound method.

    There's also $ git merge -s subtree origin/branch. It works with your example, but I've had unexpected results with it when more than one submodule is involved. You might have better luck, though.

提交回复
热议问题