Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

git - Gitignore everything except a subfolder (with every content)?

I want to ignore everything, except a specific subfolder (and all of its contents!). I tried solutions from possible duplicate questions without any success.

I'd need something simple like:

*
!That/Very/Folder/*

But this is not working ??

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Your .gitignore almost works but it doesn't for a simple reason: the first rule (*) tells Git to ignore every file and directory in the root of the repository. Git honors it and ignores everything, including the That directory and its content. The "unignore" rules that follow do not match anything inside the That subdirectory because the That directory is ignored together with it content, and they don't have effect.

In order to tell Git to not ignore files and directories in a deeply nested sub-directory you have to write ignore and unignore rules to let it reach the enclosing sub-directory first and then add the rules you want.

Your .gitignore file should look like this:

### Ignore everything ###
*

# But do not ignore "That" because we need something from its internals...
!That/

# ... but ignore (almost all) the content of "That"...
That/*
# ... however, do not ignore "That/Very" because we need to dig more into it
!That/Very/

# ... but we don't care about most of the content of "That/Very"
That/Very/*
# ... except for "That/Very/Folder" we care
!That/Very/Folder/
# ... and its content
!That/Very/Folder/*

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...