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

后端 未结 2 1778
花落未央
花落未央 2020-12-20 08:26

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

相关标签:
2条回答
  • 2020-12-20 08:45

    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:\Users\vonc\prog\git\tests>git init i
    Initialized empty Git repository in C:/Users/vonc/prog/git/tests/i/.git/
    
    C:\Users\vonc\prog\git\tests>cd i
    
    C:\Users\vonc\prog\git\tests\i>mkdir .vim\colors
    
    C:\Users\vonc\prog\git\tests\i>touch .vim\colors\apprentice.vim
    
    C:\Users\vonc\prog\git\tests\i>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:\Users\vonc\prog\git\tests\i>sbt .gitignore
    /*
    
    C:\Users\vonc\prog\git\tests\i>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:\Users\vonc\prog\git\tests\i>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:\Users\vonc\prog\git\tests\i>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:\Users\vonc\prog\git\tests\i>git check-ignore -v -- .vim\colors\apprentice.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
    
    0 讨论(0)
  • 2020-12-20 09:00

    Your rules seem to be fine. Except *.swp would be covered by /* and the comment not being on its own line.

    As you can see all other .vim/colors are ignored.

    If your files have already been staged, you may need to unstage them and remove them from the repository, then readd them.

    git rm --cached ./.vim/colors/*
    git add ./.vim/colors/
    git commit -m"Unstaged .vim/colors"
    

    Final .gitignore to use

    /*
    !.gitignore
    
    !.bashrc
    !.bash_profile
    
    !.vimrc
    !.vim
    .vim/colors/*
    !.vim/colors/apprentice.vim
    
    0 讨论(0)
提交回复
热议问题