How to ignore changes in git submodules

Recently while setting up my vimfiles for version control I encountered a problem with git submodules. The plugins are included as git submodules and loaded up with pathogen. For those interested in details on how this works, there is a Vimcast available on this topic. Some of the plugins need to generate helptags or other files that will mark the working tree dirty. There is a very simple way to prevent this, but it is a little tricky to find out what is needed to make it work, that’s why I decided to write a blog post about how to do this.

In my opinion one should not change anything within submodules but rather change it in the original repository and then update the submodule. So we are going to ignore dirty and untracked files within all our submodules.

One method is to provide a flag to git status, telling it what you want it to ignore in submodules. The command git help status reveals how to do this:

     Ignore changes to submodules when looking for changes.
     <when> can be either "untracked", "dirty" or "all", which
     is the default. When "untracked" is used submodules are
     not considered dirty when they only contain untracked
     content (but they are still scanned for modified content).
     Using "dirty" ignores all changes to the work tree of
     submodules, only changes to the commits stored in the
     superproject are shown (this was the behavior before 1.7.0).
     Using "all" hides all changes to submodules (and suppresses
     the output of submodule summaries when the
     config option status.submodulesummary is set).

I find typing in the flag very annoying. Of course you can create an alias for this, but I don’t like that either. There must be a better way. And there is. Once you added a submodule there will be a file named .gitmodules in the root of your repository. The entries in this file look basically like this:

[submodule "bundle/fugitive"]
	path = bundle/fugitive
	url = git://

Just add a single line to it and be happy:

[submodule "bundle/fugitive"]
	path = bundle/fugitive
	url = git://
        ignore = dirty

Does not work? Did not work for me at first either. It took me about half an hour to figure out my git version is to old. Ubuntu 10.04 comes with git or something. I just downloaded and compiled as described here (I chose the 3rd way) and suddenly it worked like intended.

Hint: If you want the manpages for git to be installed as well you have to run sudo make install-man. They are necessary to be able to use git help <command>. This may require the additional packages asciidoc and docbook2x, you can just install them with sudo apt-get install asciidoc docbook2x.

  • Tom Ryder

    Extremely helpful, thank you very much Nils!

  • Anonymous

    Your welcome! I’m very happy it is useful to someone!

  • Sunny

    Excellent!!! I’ve been having this issue for months

  • Nils Haldenwang

    Thx for the comment, I’m glad I could help.

  • Scott Armit

    Thank you! We were hunting for this.

  • Anonymous

    You’re welcome! Have fun with it! :)

  • Peter C. Host

    I have been having the exact same problem (pathogen & Vim dotfiles). Thx for taking the time to write this thorough –ignore-submodules post !

  • Anonymous

    Another solution is to use core.excludesfile, which works like a global .gitignore. I prefer this because then I can specifically exclude helptags files, and still see other changes I might want to know about.

  • Ernesto García

    Thanks a lot man! You saved my afternoon today with this tip :)

  • Anonymous

    Thank you! I usually don’t comment but you saved me from lots of annoying “git status” command results.

blog comments powered by Disqus