开发者

How to tell git to ignore subdirectories (which are part of the repository) for operations like git status

I have a repository including many subdirectories. I know for a fact that I won't make changes to some of them. Is there a way to tell 'git status' and other operations which scan the whole filesystem to not bother with these directories, thus s开发者_高级运维peeding up those operations?


I assume that by “[they] are part of the repository” (in your question’s title) that you mean the subdirectories you want to skip actually contain tracked files and that you want to avoid git status checking on the status of even those tracked files.

By default, git status will check all the tracked paths. It does not have any options for skipping a preset list of paths, but it does take pathspec arguments that can be used to specify the paths it should check (implicitly, the other paths will be skipped).

To directly use the available available functionality, you have to turn the problem around: How do I show the status only certain files and directories?

The answer is that you just list them on the command line. For example, if we only want to know about src, doc (both probably directories) and Makefile:

git status src doc Makefile

Typing that every time would be cumbersome, so you will likely want to use a shell script and/or a Git alias.

However, this only works well if you can define the set of paths you are interested in without saying something like “everything except X, Y, and Z”. Git does not include support for “everything except …” (yet1), so you will have to rely on something else to expand the list and give it to git status. Some shells have features that can help.

Example: Process all the entries in the current directory (including “dot” files/directories) as long as they do not match .V, .W (i.e. patterns that start with a dot), X, or Y:

  • zsh with the EXTENDED_GLOB option enabled (setopt EXTENDED_GLOB),

    git status ^(.V|.W|X|Y)(D)
    

    You can leave off the (D) if you also have GLOB_DOTS enabled.

  • bash with the dotglob and extglob options enabled (shopt -s extglob dotglob):

    git status !(.V|.W|X|Y)
    
  • ksh (the pattern for “dot” names is a bit odd due to emulating GLOB_DOTS):

    git status .!(|.|V|W) !(X|Y)
    

And again, you will probably want to wrap these up in a script (or Git alias) instead of typing them in directly.


1 There has been some discussion on the Git mailing about adding support for “negative” pathspecs (“everything except …”) that could let you do this without help from the shell, but there is no firm design or public code yet.


Maybe put them into .git/info/exclude, this is, basically, the same like .gitignore, but you don't touch the repository content.


The .gitignore file will take care of that.

Create it in the root of your repository. Documentation here:

http://git-scm.com/docs/gitignore


In your directory in which you have a git repo, create/edit the .gitignore file and add the following:

dir1/
dir2/

etc

Which will ignore dir1 and dir2.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜