Git: .gitignore in subdirectories

大兔子大兔子 提交于 2019-12-08 03:45:16

问题


I'm trying to manage my neovim's init.vim with git. I want to ignore everything except the init.vim in the same folder as the .gitignoreand one kalisi.vim in the directory

/bundle/vim-airline/autoload/airline/themes/

This is my current .gitignore:

# Ignore everything
*

# Exceptions
!.gitinore
!init.vim

!bundle
bundle/*
!bundle/vim-airline
bundle/vim-airline/*
!bundle/vim-airline/autoload
bundle/vim-airline/autoload/*
!bundle/vim-airline/autoload/airline
bundle/vim-airline/autoload/airline/*
!bundle/vim-airline/autoload/airline/themes
bundle/vim-airline/autoload/airline/themes/*
!bundle/vim-airline/autoload/airline/themes/kalisi.vim

My thoughts about this:

  • Ignore everything: *

  • Except this .gitignore: !.gitgnore

  • And the init.vim in the same directory: !init.vim

  • Also don't ignore the folder bundle: !bundle

  • But everything in it: bundle/*

  • Except the folder vim-airline: !vim-airline

I think you get the idea...

But if I execute git status now only get bundle/ as untracked file. Should't I get kalisi.vim or bundle/vim-airline/autoload/airline/themes/kalisi.vim?

I'm hoping for a more elegant way to be honest. I also heard about placing multiple .gitignore's in the directories, but the subdirectories are all projects with it's own .gitignore and this would create a huge amount of work to only not-ignore the right .gitignore.

I hope someone has an idea what to do, currently it just seems like the bundle/ directory is tracked, but not the content I want...


回答1:


As I mentioned in "How do I add files without dots in them (all extension-less files) to the gitignore file?", there is one rule to remember with .gitignore:

It is not possible to re-include a file if a parent directory of that file is excluded. (†)
(: unless certain conditions are met in git 2.7+)

That means, when you exclude everything ('*'), you have to white-list folders ('/**/'), before being able to white-list files.

# Ignore everything
*

# Exceptions
!.gitinore
!init.vim
!/**/
!bundle/vim-airline/autoload/airline/themes/kalisi.vim

The OP LastSecondsToLive actually took a simpler approach:

I created a commit with bundle/vim-airline/autoload/airline/themes/kalisi.vim then I switched my .gitignore back to:* !.gitinore !init.vim to ignore everything, but since bundle/vim-airline/autoload/airline/themes/kalisi.vim is already tracked, changes will get tracked in the future.



来源:https://stackoverflow.com/questions/33983210/git-gitignore-in-subdirectories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!