Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replaced third party code with git submodules, now I can't switch branches

Here's the story: I have 2 git branches master and develop I'm currently on develop.

I've long since had the source files of a third party library included in my repo in the directory Vendor/MGTwitterEngine. This code was already merged into master.

Now, on branch develop, I've removed the library and replaced it with a git submodule and committed.

The problem is I can no longer switch back to the master branch. If I try, I get the following error:

The following untracked working tree files would be overwritten by checkout:
    Vendor/MGTwitterEngine/MGTwitterHTTPURLConnection.h
    Vendor/MGTwitterEngine/MGTwitterHTTPURLConnection.m
    Vendor/MGTwitterEngine/MGTwitterLibXMLParser.h
    Vendor/MGTwitterEngine/MGTwitterLibXMLParser.m
    Vendor/MGTwitterEngine/MGTwitterMessagesLibXMLParser.h
    Vendor/MGTwitterEngine/MGTwitterMessagesLibXMLParser.m
    Vendor/MGTwitterEngine/MGTwitterMessagesParser.h
    Vendor/MGTwitterEngine/MGTwitterMessagesParser.m
    ...
   Aborting

git thinks the submodule files are "untracked" and won't replace them with the tracked, non-submodule files in the same location.

How can I get around this issue?

like image 236
Christian Schlensker Avatar asked Oct 01 '11 07:10

Christian Schlensker


People also ask

Why you should not use git submodules?

This is because of some major drawbacks around git submodules, such as being locked to a specific version of the outer repo, the lacking of effective merge management, and the general notion that the Git repository itself doesn't really know it's now a multi-module repository.

How do I switch between branches in git Visual Studio?

Access the "Source Control" tab on the left side of VSCode. Click on the "three small dots" next to the refresh button. Click on the "Checkout to..." option. Choose the branch you want to switch to.

How do you swap branches?

You can use the git switch - command to undo any changes you make and return to your previous branch. If you instead want to keep your changes and continue from here, you can use git switch -c <new-branch-name> to create a new branch from this point.


1 Answers

Unfortunately, I think this is just one of the drawbacks of using submodules. These problems are described in a section called "Issues with Submodules" in Pro Git, but in short, the simplest workaround is to move the submodule directory out of the way before switching to the master branch:

mv Vendor Vendor.moved
git checkout master

Similarly, when you change to develop, you should do:

git checkout develop
mv Vendor.moved Vendor
like image 131
Mark Longair Avatar answered Oct 17 '22 05:10

Mark Longair