0

On my project, there are two branches I'm working on (Develop and Release), each of which makes generous use of submodules. The Develop branch uses about twice as many submodules as Release, because it is where we test ideas.

When I switch branches from Develop to Release, the directories of Develop-specific submodules stay where they are, and so they become untracked. This makes things a bit confusing for me, because I do occasionally need to add or remove submodules from Release as well, and the git status message becomes a long list of untracked modules, some of which I want to use and some I don't.

What I would like to do is remove all untracked submodules from my project as soon as I switch from Develop to Release, so that I'm working with a "clean slate", (IE no untracked submodules sitting in my working directory).

I have found several solutions to removing individual submodules one at a time, such as here: How do I remove a submodule?

However solutions such as this assume that the git submodules are in use and being tracked (which they are not), and it is also a pain in the neck to remove them one at a time when I'm working with something like 15-20 submodules.

I have also tried piping linux commands like so:

git ls-files --others --exclude-standard | rm -rf

But the command does not appear to do anything. I have also tried using the same with git rm -rf, to no avail.

Does anyone know if there is an easy to remove all untracked git submodules from a working directory? Any advice anyone can share on this matter would be greatly appreciated. Thank you!

Community
  • 1
  • 1
SemperCallide
  • 1,950
  • 6
  • 26
  • 42
  • Possible duplicate of http://stackoverflow.com/questions/61212/how-do-i-remove-local-untracked-files-from-my-current-git-branch?rq=1 ? Remove untracked directories with `git clean -df` – J. Titus Apr 04 '16 at 17:15
  • @J.Titus From my understanding, that solution does not work with submodules, since submodules don't work the same as regular directories within the work tree. – SemperCallide Apr 04 '16 at 17:22
  • If the directories are untracked, technically they're not submodules at that point. Do they exist in your `.gitmodules` file? – J. Titus Apr 04 '16 at 17:26
  • @J.Titus The directories in question do not exist in .gitmodules file, and so you may be right. However when I run 'git clean -f -n' to show which untracked files would be removed, none of them are listed, or any of the files within then. This has lead me to believe they are being treated as submodules and not regular directories, but I may be wrong about that. – SemperCallide Apr 04 '16 at 17:32
  • Since the entire directories are untracked, you'll need to run with the `-d` flag also. – J. Titus Apr 04 '16 at 17:34
  • @J.Titus So when I run 'git clean -f -n -d' I get N messages of "Would skip repository " for each of the N untracked submodule directories that I'm trying to remove. Based on this message, it would seem that git is considering these directories to be repos, and not treating them like normal untracked files to be removed. – SemperCallide Apr 04 '16 at 17:47
  • Well, darn. Sorry I couldn't be of any help, then. – J. Titus Apr 04 '16 at 17:49
  • @J.Titus Thanks anyway! – SemperCallide Apr 04 '16 at 17:52
  • Ah, sob-modules... Check for a `.git` file (with contents being the path to the repository) in the "untracked" submodule directory, that's probably what is keeping them around. In the old days that would be a `.git` directory with the repository in it but since git 1.7.10 or so it's now a file with the path. – torek Apr 04 '16 at 17:55
  • @torek you are correct! Removing the .git file from the directory allows me to 'git clean' it. So now the only thing is making an easy way to remove all .git files from untracked directories. Working on a bash script now, but open to suggestions if there is an easier way. – SemperCallide Apr 04 '16 at 18:08
  • I don't think there is an easier way, although I do my best to avoid sobmodules on general principles so I might just be missing something. Blindly removing all `.git` files is bad since you still do have some active submodules. It seems like git probably should clean these up on its own when switching branches, but maybe there's something else there I'm missing as well. – torek Apr 04 '16 at 18:13

1 Answers1

1

With some advice from helpful folks in the comments section, I determined that there is no obvious, non-tedious solution to this problem. Instead I created a bash script that does the job for me. Here it is, in case anyone else has the same issue:

#!/bin/bash

clear
git ls-files --others --directory --exclude-standard
echo
read -r -p "Are you sure you want to remove these untracked submodules? [y/N] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
then
    git ls-files --others --directory --exclude-standard | while read line; do
    rm $line/.git &> /dev/null;
  done
  git clean -d  -f
fi

Easy steps for people not super comfortable with Bash scripts:

Step 1: Copy the above script into a file and name it 'cleanUSM'. Save the file to /usr/bin. If you are having trouble saving it or finding /usr/bin, just save it to your current directory, and then use 'sudo mv cleanUSM /usr/bin/cleanUSM' to get it where it needs to go.

Step 2: From within your root directory, run the command 'cleanUSM'

Thanks to everyone who contributed!

SemperCallide
  • 1,950
  • 6
  • 26
  • 42