4

I have a repo with a few branches. On one of them I deleted a directory, which does not really belong to the repo and added it back as a git submodule.

That's what I do:

  1. Clone the repo
  2. Switch to the branch with the submodule in GitHub Desktop
  3. git submodule init
  4. Until now I can switch the branches (between the ones with the submodule and no submodule without any issues).
  5. git submodule update to finally get the "content" of the submodule

So everything is right until I try to switch the branch in GitHub Desktop (to another branch without any git submodules). If I do so I get a checkout error claiming I have uncommitted changes: GitHubCheckoutIssue

Failed to checkout the branch

The following files in your working directory would be overwritten by a checkout: [...] Please commit your changes before you can switch branches.

However as I said I did not changed anything - the only thing I did was updating my submodules.

git status shows everything is all right:

$ git status
On branch patch-submodule
Your branch is up-to-date with 'origin/patch-submodule'.

nothing to commit, working directory clean

When I deinit my submodules I can also switch the branches again.

So what's wrong here? And how can I prevent this error or get around it?

rugk
  • 4,755
  • 2
  • 28
  • 55

2 Answers2

1

Thanks to the GitHub support I finally got a very useful reply.

Especially they directed me to Replaced third party code with git submodules, now I can't switch branches which exactly describes my issue - except of one difference: I did used the GitHub GUI and not the console.

And there is linked to git-scm where this is explained very nicely:

Switching branches with submodules in them can also be tricky. If you create a new branch, add a submodule there, and then switch back to a branch without that submodule, you still have the submodule directory as an untracked directory[.]

[...] You have to either move it out of the way or remove it, in which case you have to clone it again when you switch back—and you may lose local changes or branches that you didn’t push up.

This basically is all what this error messages meant in my case.

Community
  • 1
  • 1
rugk
  • 4,755
  • 2
  • 28
  • 55
0

You mentioned that you removed a directory from the repo and instead made it a git submodule.

Was this directory removed from the brach you attempting to check out?

What's likely happening is that the submodule contains one of the same files as is contained in the branch you are attempting to check out.

You update the submodule, which places the file(s) on your file system. When you attempt to checkout the branch, git tries to overwrite that submodule version of the file with the version that is still contained in the main repo on that particular branch. This could/would result in lost work, so git throws the error.

Make a clean checkout of that branch, then remove the files and commit the changes. Possibly add or merge the submodule changes to that branch as well.

pkamb
  • 33,281
  • 23
  • 160
  • 191
  • Yes I created a new branch based on the master branch with the directory included. Then I removed the directory and committed this change (did not pushed it AFAIK). Afterwards I added the git submodule and committed this change again. Then I tried to push this change, but failed as I described above. – rugk Nov 12 '15 at 17:34
  • Now redoing this on a new branch-. This time I made sure to sync my repos after deleting the files. – rugk Nov 12 '15 at 17:52
  • When I finally want to commit this change with GitHub Desktop it tells me: `Could not commit submodules: [...] Add them as submodules from Git Shell or remove their .git folders to add them as regular directories instead.` – rugk Nov 12 '15 at 18:30
  • So I've now committed this using the git shell and could also sync it in GitHub Desktop. However again I cannot switch my branch. I also discovered when I delete the whole content of the (submodule) directory (but leaving it as a submodule) GitHub Desktop shows me `Error` when switching the branch. After this if I switch the repo and switch back to the problematic one I can switch the branch successfully. So obviously this is a case for GitHub support. I'm contacting them. – rugk Nov 12 '15 at 18:44