I have a folder, which was a git repo. It contains some files and .gitmodules file. Now, when I do git init
and then git submodule init
, the latter command output is nothing. How can I help git to see submodules, defined in .gitmodules file without running git submodule add
by hand again?
Update: this is my .gitmodules file:
[submodule "vim-pathogen"]
path = vim-pathogen
url = git://github.com/tpope/vim-pathogen.git
[submodule "bundle/python-mode"]
path = bundle/python-mode
url = git://github.com/klen/python-mode.git
[submodule "bundle/vim-fugitive"]
path = bundle/vim-fugitive
url = git://github.com/tpope/vim-fugitive.git
[submodule "bundle/ctrlp.vim"]
path = bundle/ctrlp.vim
url = git://github.com/kien/ctrlp.vim.git
[submodule "bundle/vim-tomorrow-theme"]
path = bundle/vim-tomorrow-theme
url = git://github.com/chriskempson/vim-tomorrow-theme.git
and here is listing of this dir:
drwxr-xr-x 4 evgeniuz 100 4096 июня 29 12:06 .
drwx------ 60 evgeniuz 100 4096 июня 29 11:43 ..
drwxr-xr-x 2 evgeniuz 100 4096 июня 29 10:03 autoload
drwxr-xr-x 7 evgeniuz 100 4096 июня 29 12:13 .git
-rw-r--r-- 1 evgeniuz 100 542 июня 29 11:45 .gitmodules
-rw-r--r-- 1 evgeniuz 100 243 июня 29 11:18 .vimrc
so, definitely, it is in top level. the git directory is not changed, only git init
is done
To pull everything including the submodules, use the --recurse-submodules and the --remote parameter in the git pull command .
How do I clone a git repository so that it also clones its submodules? Running git clone $REPO_URL merely creates empty submodule directories. That command would be git clone --recurse-submodules --remote-submodules (Q3 2019 Git 2.23): it will clone and update the submodules in one command.
If an untracked directory is managed by a different git repository, it is not removed by default. The submod directory is a different git repository; if you want to remove it, Use -f option twice if you really want to remove such a directory . git clean -f -f -d submod does remove submod .
You can fix it by: either committing or undoing the changes/evolutions within each of your submodules, before going back to the parent repo (where the diff shouldn't report "dirty" files anymore). To undo all changes to your submodule just cd into the root directory of your submodule and do git checkout .
git submodule init
only considers submodules that already are in the index (i.e. "staged") for initialization. I would write a short script that parses .gitmodules
, and for each url
and path
pair runs:
git submodule add <url> <path>
For example, you could use the following script:
#!/bin/sh
set -e
git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
while read path_key local_path
do
url_key=$(echo $path_key | sed 's/\.path/.url/')
url=$(git config -f .gitmodules --get "$url_key")
git submodule add $url $local_path
done
This is based on how the git-submodule.sh
script itself parses the .gitmodules
file.
Expanding on @Mark Longair's answer, I wrote a bash script to automate steps 2 & 3 of the following process:
#!/bin/bash
set -e
rm -rf .git
git init
git config -f .gitmodules --get-regexp '^submodule\..*\.path$' > tempfile
while read -u 3 path_key path
do
url_key=$(echo $path_key | sed 's/\.path/.url/')
url=$(git config -f .gitmodules --get "$url_key")
read -p "Are you sure you want to delete $path and re-initialize as a new submodule? " yn
case $yn in
[Yy]* ) rm -rf $path; git submodule add $url $path; echo "$path has been initialized";;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done 3<tempfile
rm tempfile
Note: the submodules will be checked out at the tip of their master branch instead of the same commit as the boilerplate repo, so you'll need to do that manually.
Piping the output from git config into the read loop was causing problems with the prompt for input, so it outputs it to a temp file instead. Any improvements on my first bash script would be very welcome :)
Big thanks to Mark, https://stackoverflow.com/a/226724/193494, bash: nested interactive read within a loop that's also using read, and tnettenba @ chat.freenode.net for helping me arrive at this solution!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With