如何.gitignore文件夹中的所有文件/文件夹,但不是文件夹本身? [重复]

时间:2023-01-14 11:08:35

Possible Duplicate:
How do I add an empty directory to a git repository

可能重复:如何将空目录添加到git存储库

I want to check in a blank folder. How can I do this?

我想检查一个空白文件夹。我怎样才能做到这一点?

2 个解决方案

#1


326  

You can't commit empty folders in git. If you want it to show up, you need to put something in it, even just an empty file.

你不能在git中提交空文件夹。如果你想要它出现,你需要在其中放入一些东西,甚至只是一个空文件。

For example, add an empty file called .gitkeep to the folder you want to keep, then in your .gitignore file write:

例如,将一个名为.gitkeep的空文件添加到要保留的文件夹中,然后在.gitignore文件中写入:

# exclude everything
somefolder/*

# exception to the rule
!somefolder/.gitkeep 

Commit your .gitignore and .gitkeep files and this should resolve your issue.

提交.gitignore和.gitkeep文件,这应该可以解决您的问题。

#2


969  

put this .gitignore into the folder, then git add .gitignore

把这个.gitignore放到文件夹中,然后git add .gitignore

*
*/
!.gitignore

The * line tells git to ignore all files in the folder, but !.gitignore tells git to still include the .gitignore file. This way, your local repository and any other clones of the repository all get both the empty folder and the .gitignore it needs.

*行告诉git忽略文件夹中的所有文件,但是!.gitignore告诉git仍然包含.gitignore文件。这样,您的本地存储库和存储库的任何其他克隆都将获得空文件夹和它所需的.gitignore。

Edit: May be obvious but also add */ to the git ignore to also ignore sub folders.

编辑:可能很明显,但也添加* /到git忽略也忽略子文件夹。

#1


326  

You can't commit empty folders in git. If you want it to show up, you need to put something in it, even just an empty file.

你不能在git中提交空文件夹。如果你想要它出现,你需要在其中放入一些东西,甚至只是一个空文件。

For example, add an empty file called .gitkeep to the folder you want to keep, then in your .gitignore file write:

例如,将一个名为.gitkeep的空文件添加到要保留的文件夹中,然后在.gitignore文件中写入:

# exclude everything
somefolder/*

# exception to the rule
!somefolder/.gitkeep 

Commit your .gitignore and .gitkeep files and this should resolve your issue.

提交.gitignore和.gitkeep文件,这应该可以解决您的问题。

#2


969  

put this .gitignore into the folder, then git add .gitignore

把这个.gitignore放到文件夹中,然后git add .gitignore

*
*/
!.gitignore

The * line tells git to ignore all files in the folder, but !.gitignore tells git to still include the .gitignore file. This way, your local repository and any other clones of the repository all get both the empty folder and the .gitignore it needs.

*行告诉git忽略文件夹中的所有文件,但是!.gitignore告诉git仍然包含.gitignore文件。这样,您的本地存储库和存储库的任何其他克隆都将获得空文件夹和它所需的.gitignore。

Edit: May be obvious but also add */ to the git ignore to also ignore sub folders.

编辑:可能很明显,但也添加* /到git忽略也忽略子文件夹。