How to use conditions in gitignore

2019-03-06 06:20发布

I'm managing several pdf and graffle files with git. I want to add pdf files to repository only if graffle with same filename does not exists, e. g.

only add foo.pdf and bar.graffle into repository in the directory

$ ls
foo.pdf
bar.pdf
bar.graffle

If such target can't be completed only with gitignore, is there any work arounds exist?

bar.pdf can only be generated with bar.graffle through several steps in GUI, so I want to keep them on commit.

标签: git gitignore
1条回答
smile是对你的礼貌
2楼-- · 2019-03-06 07:05

Note that .gitignore only exists as a hint to git as to which files, directories or file types will be ignored by git add.

However, .gitignore will not stop any file to be added into repository if you use git add -f.

Long story short, .gitignore is rather simplistic mechanism that helps to avoid accidentally adding typical junk files (like editor backups, object files, executables, etc.), but it is not sophisticated enough to be capable of what you are asking for - like changing behaviour depending on some external conditions.

You can be careful not to add some files if you don't want them, or add ignored files with git add -f and thus overriding existing simple rules.

Alternatively, you can create some script and register it as git macro like git addc that will do such advanced filtering.

查看更多
登录 后发表回答