5

So I am fairly new to the whole git topic. But what I am trying to do is syncing my dotfiles across multiple machines. I know there are plenty of tutorials out there and I got the basic concept. But my question is a tick more specific.

First things first: I am using Windows (for now) and Vim. On top of that I use vundle to manage my plugins. Now the plugins are git-repos themselves. The directory ~/.vim (which is the main repo I want to sync) contains them.

So my question is, how do I initialize a submodule of an existing repo?

What I got now is a bunch of green folders in my ~/.vim/bundle/... which represent the other repos. The git status command reports them as untracked content (and no its not my .gitignore doing some funky stuff).

You might want to take a look at my repo if you are unfamiliar with the green folder "phenomenon".

P.S. One of the subfolders says modified content instead of untracked content, even though I didn't touch it. I have no idea why.

perror
  • 7,071
  • 16
  • 58
  • 85
Sensei
  • 598
  • 1
  • 4
  • 13

3 Answers3

5

If all of your bundles are just basic clones with no modifications, you can do something like this: From the root of your dotfiles repo:

for d in `find .vim/bundle/ -maxdepth 1 -mindepth 1 -type d`
do
    pushd $d
    export url=$(git config --get remote.origin.url)
    popd
    git submodule add $url $d
done

That should work well enough - if any bundle isn't a repository, or if you've added it manually, then it'll just pass over that directory.

Then you can then do a git submodule foreach git pull to update them all, and add them to update your dotfiles repo's references.

naught101
  • 18,687
  • 19
  • 90
  • 138
  • 1
    This is exactly what I needed. Thanks. BTW, I also added some checks to make sure that a submodule was a git repo before attempting to add it. That way I could use it on a folder where some subfolders were repos and some weren't – mattgately Jul 28 '16 at 13:49
1

Each time you see modified content without any explicit modification, check global settings (core.autocrlf, core.filemode) which can make changes on checkout.)

For each directory within .vim/bundle/, you need to:

  • go to that module: cd .vim/bundle/aModule
  • initialize a git repo (git init .), commit and push to a new git repo on GitHub
  • got back to the parent repo: cd .. (you are in .vim/bundle/)
  • git submodule add https:///github.com/tairun/aModule aModule (note that this submodule can track the latest of a branch, if you want to)

Then git submodule update --init --remote.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for your answer. Needed some time to test everything. So far everything has worked really well, even though I didn't do exactly as you told me to. I am still not familiar with a lot of chained commands like `git submodule update --init --remote`, so I can only guess what they do. I may come back at a later time and share my steps. But basically what I did was (from the top of MY repo) `git submodule add git@github.com:user/repo.git bundle/aBundle`. When I get an `index` error I ran `git rm --cache bundle/aBundle`. From there I'd do a git `submodule update --init`. – Sensei Apr 27 '13 at 15:25
  • @Sensei: you probably should post that as a separate answer. The question perhaps wasn't entirely clear that your vundle plugins are git repos *cloned from the web*, and you're trying to replace them with a basic submodule. – naught101 Feb 03 '15 at 13:16
0

Because the work seems repetitive for me, I would like to share the following rather long git alias:

git config --global alias.convertGitRepoToSubmodule '!f() { if [ "$#" = 0 ]; then echo "Usage: convertGitRepoToSubmodule <submodule_directory...>" >&2; return 1; fi; cd -- "${GIT_PREFIX:-.}"; for i; do origin="$(cd "$i" && git config --get remote.origin.url)"; ( set -x; git submodule add "$origin" "$i"; ); done; }; f'

Then just do:

git convertgitrepotosubmodule $(find .vim/bundle/ -maxdepth 1 -mindepth 1 -type d)

and it should call git submodule add <the origin> <the dir> on all directories.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111