Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

gitignore - Git ignore - How do you override an exception to an ignore-all?

I want to create a git repo of my bash settings and plugins and whatnot. I have ignored everything (line 0) and then manually added the files/folders that I want in the repo. (I have to do it this way because the repo is in my ~ folder.) I want to ignore all colour profiles in the .vim/colors/ directory, but I do want to include the one file that I am using (apprentice.vim).

The .vim/colors/* line doesn't seem to work - it doesn't ignore any of the files at all. Using !!.vim/colors/* doesn't work either. How am I supposed to make it override all previous rules and ignore the colors folder, then still allow the apprentice.vim file to be ignored?

/*

*.swp

!.gitignore

!.bashrc
!.bash_profile

!.vimrc
!.vim
.vim/colors/* # Don't include all of the other color schemes
!.vim/colors/apprentice.vim
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue was the # comment on the same line as .vim/colors/*, but here is an alternative.

The main rule for gitignore is:

It is not possible to re-include a file if a parent directory of that file is excluded.

That means:
(assuming the elements are not already versioned, in which case you need to git rm --cached them first):

  • you need to ignore all the files recursively: that is '**'
  • exclude all the folders recursively: those are '**/'
  • exclude the file you want (which will work because its parent folder is not ignored as well)

Result:

/**
!/**/
!exclude what you want to *not* be ignored
# for instance
.vim/colors/* # Don't include all of the other color schemes
!.vim/colors/apprentice.vim

Check what is and is not ignored with git check-ignore -v (the -v is important):

git check-ignore -v -- afile

It is easier than un-ignoring a sub-folder manually, especially when the sub-folder content to un-ignore is several level deep: to exclude a file in a/b/c, you need first to un-ignore !/a, then !/a/b, then !/a/b/c)


Illustration/test:

C:Usersvoncproggitests>git init i
Initialized empty Git repository in C:/Users/vonc/prog/git/tests/i/.git/

C:Usersvoncproggitests>cd i

C:Usersvoncproggitestsi>mkdir .vimcolors

C:Usersvoncproggitestsi>touch .vimcolorsapprentice.vim

C:Usersvoncproggitestsi>git st
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .vim/

nothing added to commit but untracked files present (use "git add" to track)

Simple .gitignore with /* rule:

C:Usersvoncproggitestsi>sbt .gitignore
/*

C:Usersvoncproggitestsi>git st
On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)

Lets add !.gitignore.
.gitignore can now be tracked.

C:Usersvoncproggitestsi>git st
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore

nothing added to commit but untracked files present (use "git add" to track)

But if I add:

.vim/colors/*
!.vim/colors/apprentice.vim

.vim all content is still ignored:

C:Usersvoncproggitestsi>git st
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore

nothing added to commit but untracked files present (use "git add" to track)

Let's check why with git check-ignore:

C:Usersvoncproggitestsi>git check-ignore -v -- .vimcolorsapprentice.vim
.gitignore:1:/* ".vim\colors\apprentice.vim"

Adding !.vim works, because it un-ignore the folder, allowing the other rules within that folder to apply.

Still, this is simpler:

/**
!/**/
!.gitignore
.vim/colors/*
!.vim/colors/apprentice.vim

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...