如何使用Git跟踪目录而不是他们的文件? [英] How to track directories but not their files with Git?

查看:98
本文介绍了如何使用Git跟踪目录而不是他们的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Git,只是有一件事情遇到麻烦。如何跟踪目录而不追踪其内容?



例如,我正在开发的网站允许上载。我想跟踪uploads目录,以便在分支等时创建,但显然不是其中的文件(在开发分支中的测试文件或主文件中的真实文件)。



。在我的.gitignore中,我有以下内容:

 uploads /*.*

还尝试了(忽略整个目录):

上传/ 

此目录还可能包含子目录(uploads / thumbs / uploads / videos /)我想要跟踪这些但不是他们的文件。



是这可能与Git?我已经搜索到任何地方,没有找到答案。

解决方案

Git不跟踪目录,它跟踪文件,所以要实现这个您需要至少跟踪一个文件。因此,假设您的 .gitignore 文件看起来像这样:

  upload / * 

您可以这样做:

  $ touch upload / .placeholder 
$ git add -f upload / .placeholder

如果您忘记了 -f ,您将看到:

 
$ git add upload / .placeholder
以下路径被其中一个.gitignore文件忽略:
upload
如果您真的要添加它们,请使用-f。
致命:没有添加文件

然后当您执行 git status 你会看到:

 
#在分支主人

#初始提交

#要提交的更改:
#(使用git rm --cached ...to unsage)

#新文件:upload / .placeholder

显然你可以这样做:

  $ touch upload / images / .placeholder 
$ git add -f upload / images / .placeholder


I've recently started using Git and am having trouble with just one thing. How can I track directories without tracking their contents?

For example the site I'm working on allows uploads. I want to track the uploads directory so that it is created when branching, etc. but obviously not the files within it (test files whilst in develop branch or the real files in master).

In my .gitignore I have the following:

uploads/*.*

Have also tried (which ignores the whole directory):

uploads/

This directory may also contain sub directories (uploads/thumbs/ uploads/videos/) I would like to be able to track these but not their files.

Is this possible with Git? I've searched everywhere without finding an answer.

解决方案

Git doesn't track directories, it tracks files, so to acheive this you need to track at least one file. So assuming your .gitignore file looks something like this:

upload/*

You can do this:

$ touch upload/.placeholder
$ git add -f upload/.placeholder

If you forget the -f you'll see:

$ git add upload/.placeholder
The following paths are ignored by one of your .gitignore files:
upload
Use -f if you really want to add them.
fatal: no files added

Then when you do git status you'll see:

# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached ..." to unstage)
#
#   new file:   upload/.placeholder
#

Obviously you can then do:

$ touch upload/images/.placeholder
$ git add -f upload/images/.placeholder

这篇关于如何使用Git跟踪目录而不是他们的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆