[英]How to stop tracking the content of a directory in git?
I have a directory named img
. 我有一个名为
img
的目录。 I want to that directory be exits on the repo, but always it should be empty. 我希望该目录在存储库上退出,但始终应为空。 I mean just the content of that directory should be untracked.
我的意思是只应该跟踪该目录的内容。
Here is a simplified of my project: 这是我的项目的简化:
myproject/
.git/
application/
public/
img/
img1.jpg
img2.jpg
vendor/
composer.json
composer.lock
I want to make it like this on repository: 我想在存储库中使它像这样:
myproject/
.git/
application/
public/
img/
vendor/
composer.json
composer.lock
How can I do that? 我怎样才能做到这一点?
According to some researches, I have to use .gitignore
file. 根据一些研究,我必须使用
.gitignore
文件。 But how? 但是如何? Should I make a
.gitignore
file on the root of project and write this in it? 我是否应该在项目的根目录下创建一个
.gitignore
文件并将其写入其中?
// .gitignore file
public/img/*
Or should I make a .gitignore
inside img
directory? 还是应该在
img
目录中创建.gitignore
? If yes should I write what thing in it? 如果是,我应该在里面写些什么?
Ok, now I got a new problem. 好的,现在我遇到了一个新问题。 Let's explain what I want to do exactly.
让我们解释一下我想确切地做什么。 Here is the scenario:
这是场景:
I develop my website locally and push it on the github repository. 我在本地开发我的网站,并将其推送到github存储库中。 Then I go in the server and do a
git pull
. 然后我进入服务器并执行
git pull
。 Also there is an img
directory that contains all user's avatars. 还有一个
img
目录,其中包含所有用户的头像。 All I'm trying to do is to keep the real-users avatar on img
directory. 我要做的就是将真实用户的头像保留在
img
目录中。 If I put the whole img
directory inside .gitignore
, then avatars won't be created (because of lack of img
directory) . 如果我将整个
img
目录放在.gitignore
,则不会创建头像(因为缺少img
目录) 。 If I exclude the content of img
directory, then all current user's avatars will be gone when I do a new git pull. 如果我排除
img
目录的内容,那么当我执行新的git pull时,所有当前用户的头像都将消失。 Any idea how can I handle such a situation? 知道如何处理这种情况吗?
Git doesn't track the directories, only the files. Git不跟踪目录,仅跟踪文件。
In order to add an empty directory in the repo you have to put a file in it and write the correct rules in the project's .gitignore
. 为了在仓库中添加一个空目录,您必须在其中放置一个文件,并在项目的
.gitignore
编写正确的规则。
But, because a Git repository allows placing a .gitignore
file in every directory you can put one in the directory you want to exclude and put these lines in it: 但是,由于Git存储库允许在每个目录中放置
.gitignore
文件,因此您可以在要排除的目录中放置一个.gitignore
文件,并将以下行放入其中:
# Ignore everything in this directory
*
# ... but do not ignore this file
!.gitignore
Add the new file to the repository and commit it. 将新文件添加到存储库并提交。
That's all. 就这样。
If you want to keep all the ignoring rules in a single place (in the .gitignore
file in the root directory of your project) then you can put an empty file in the directory to ignore and puts the rules above prefixed with the path into the .gitignore
file. 如果要将所有忽略规则放在一个位置(在项目根目录的
.gitignore
文件中),则可以将一个空文件放在该目录中以忽略,并将上述规则以路径开头作为前缀.gitignore
文件。
Usually, such an empty file is named .gitkeep
(the name doesn't matter, this one is used to express its purpose). 通常,这样一个空文件名为
.gitkeep
(名称无关紧要,该文件用于表达其目的)。 In this case, add to the .gitignore
file of the project: 在这种情况下,将添加到项目的
.gitignore
文件中:
/public/img/*
!/public/img/.gitkeep
If you have already added the files from public/img
to the repository (and committed them) you have to remove them first in order to not be tracked any more. 如果您已经将文件
public/img
的文件添加到了存储库中(并提交了文件),则必须先将其删除,以便不再对其进行跟踪。 Run 跑
git rm --cached public/img/*
and commit this change together with the changes in .gitignore
. 并将此更改与
.gitignore
更改一起提交。 The --cached
argument tells git rm
to not remove the files from the working tree but only stop tracking them. --cached
参数告诉git rm
不要从工作树中删除文件,而只是停止跟踪它们。
You should see a .gitignore
file in the working directory by issuing a ls -la
. 您应该通过发出
ls -la
在工作目录中看到一个.gitignore
文件。 If not create a .gitignore
file there(on the same level as the .git
file). 如果没有创建一个
.gitignore
文件(与.git
文件处于同一级别)。
In that file you can add the path to the img
folder as such: public/img/*
在该文件中,您可以将路径添加到
img
文件夹,如下所示: public/img/*
Git doesn't track directories, only files. Git不跟踪目录,仅跟踪文件。 What you can do is track any file in that directory, like a
.gitignore
, or even some file called .placeholder
. 您可以做的是跟踪该目录中的任何文件,例如
.gitignore
,甚至是名为.placeholder
文件。 You will also want to unversion the files in there but not remove the actual files, you can do this with git rm --cached file
您还需要取消版本化,但不删除实际文件,可以使用
git rm --cached file
# Untrack, but keep the existing files in img
git rm --cached myProject/public/img/*
# Create the placeholder file to keep the directory tracked
# You do not need this if any other versioned file exists
# in this directory or potentially any subfolders of this
# directory.
echo 'placeholder' > myProject/public/img/.placeholder
# Track the placeholder file in the img directory
git add myProject/public/img/.placeholder
# Commit your work
git commit -m 'YOUR COMMIT MESSAGE'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.