Cannot checkout master branch from branch with submodule
Asked Answered
B

2

8

I have a directory in my repo (call it blah) which contains some files that I manualy copied from another repo (repo-blah). One day, I decide to get smart and make this blah directory a submodule, so that I don't have to manually recopy the files any time they change in repo-blah, and so that I can make changes to those files in my repo and have a gitish way of updating repo-blah.

I'd been working on the master branch, so I make another branch called sub-blah-branch where I remove the blah directory from the index:

$ git rm -r blah

and then create a submodule for it:

$ git submodule add uri/to/repo-blah blah

I commit everything, and make sure the repo is clean:

$ git status
# On branch sub-blah-branch
nothing to commit (working directory clean)

Then, whilst feeling pretty cool about all this submodule stuff, I try to checkout the master branch. But I get this:

$ git checkout master
error: The following untracked working tree files would be overwritten by checkout:
    blah/[every file that I had manually copied from repo-blah]
Please move or remove them before you can switch branches.
Aborting

Pro Git's chapter on submodules tells me that when switching from branches that have created submodules, the contents of those submodules aren't automatically removed, and instead become untracked once the repo switches branches. In this situation, I can't switch to master without doing rm -rf on the blah directory first.

Is there a better way to deal with this problem?

I'm using git version 1.7.9.5

Beryl answered 6/6, 2014 at 21:57 Comment(1)
Best thing to do is @VonC's top recommendation: you've got checkouts colliding in your worktree? Use another worktree. Clone's cheap, it's built for this. "cd ../x" is quicker than "git checkout x" too. Plus, there's something weirdly satisfying about pulling from local repos.Alvaroalveolar
G
6

That seems expected, considering that those files are:

  • tracked in master
  • tracked differently, as a submodule (gitlink entry in the index) in sub-blah-branch

So when you switch back in master, git tries to remove the submodule entry (blah/ folder) from the index, which it can't since you have untracked files.

You have various way to proceed (in "error: The following untracked working tree files would be overwritten by checkout - git")

But I would recommend either:

  • managing two clones, each one on a different branch.
  • or making sure you don't have any blah folder in master.
  • or making your blah submodule directly in master instead of in another branch

Make sure, by the way, to properly initialize your submodule once you have declared it:

git submodule update --init

And don't forget you can make that submodule follow a branch of the upstream repo if you want:
see "Git submodules: Specify a branch/tag".

Gourmandise answered 7/6, 2014 at 7:27 Comment(3)
what if you are trying to checkout master because what you want is to merge the blah branch into master?Strega
Then, just for that merge, you could do that from a separate clone directly set on master. Do the merge there, push, and you can go back to your original clone, fetch or pull.Gourmandise
What I have done in the past, maybe is not ideal, but I say git reset --hard master and then git checkout master followed by git merge bla-branch, I don't like that solution, but since it only happens once it might be ok.Strega
S
1

This is what I use to do when I substitute an old folder for submodule :

# Discarde the warning
git checkout -f the_branch_I_want
# Merge the branch
git merge the_branch_to_merge
# Go to the submodule
cd to/the/submodule
# Cancel the submodule changes (caused by the merge)
git checkout .
cd back/to/the/project/root

# You can recreate the branch...

Then after if you pull change (in preprod for example)

# Pull the changes
git pull
# Remove the previous existing folder
rm -rf the/old_module_folder
# Install the submodule
git submodule update --init --recursive
Sic answered 8/9, 2022 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.