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

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

问题描述

我最近开始使用 Git,但在做一件事时遇到了麻烦.如何在不跟踪目录内容的情况下跟踪目录?

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

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

上传/*.*

也试过(忽略整个目录):

上传/

这个目录也可能包含子目录(uploads/thumbs/uploads/videos/),我希望能够跟踪这些而不是他们的文件.

这可以用 Git 实现吗?我到处找了也没找到答案.

解决方案

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

上传/*

你可以这样做:

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

如果你忘记了 -f 你会看到:

<前>$ git add 上传/.placeholder您的 .gitignore 文件之一将忽略以下路径:上传如果您真的想添加它们,请使用 -f.致命:未添加文件

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

<前># 在分支主## 初始提交## 要提交的更改:# (使用 "git rm --cached ..." 取消暂存)## 新文件:upload/.placeholder#

显然你可以这样做:

$ touch upload/images/.placeholder$ git add -f 上传/图片/.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天全站免登陆