简体   繁体   English

使用 gitignore 忽略(但不删除)文件

[英]using gitignore to ignore (but not delete) files

I have a tmp directory in my git repo I'd like to still exist, but be ignored.我的 git 存储库中有一个 tmp 目录,我希望仍然存在,但请忽略。 I added it to .gitignore , but git status still tells me about changes to files in that directory.我将它添加到.gitignore ,但git status仍然告诉我该目录中文件的更改。 I tried git rm -r --cached , but that removes it from the remote repo.我试过git rm -r --cached ,但这会将其从远程仓库中删除。 How can I stop tracking changes to this directory, but still allow it to exist?如何停止跟踪对此目录的更改,但仍允许它存在? I also need to do this for 1 file, but changes to that also show up in git status after .gitignore ing them.我还需要对 1 个文件执行此操作,但是在.gitignore对它们进行更改之后,对它们的更改也会显示在git status中。 What should I do?我应该怎么办?

Instead of .gitignore , you can update local git repository by running following command:您可以通过运行以下命令来更新本地 git 存储库,而不是.gitignore

git update-index --assume-unchanged <file>

In this case a file is being tracked in the origin repo.在这种情况下,原始存储库中正在跟踪文件。 You can modify it in your local repo and git will never mark it as changed.您可以在本地存储库中对其进行修改,git 永远不会将其标记为已更改。 Read more at:阅读更多:

Ignoring changes made to files while allowing them to exist is the exact purpose of .gitignore .在允许文件存在的同时忽略对文件所做的更改是.gitignore的确切目的。 So adding the files (or directories) to .gitignore is the only thing you have to do.因此,将文件(或目录)添加到.gitignore是您唯一需要做的事情。

But your problem is that git is already tracking the files you want to ignore and .gitignore doesn't apply to tracked files.但是您的问题是 git 已经在跟踪您要忽略的文件,并且.gitignore不适用于跟踪的文件。 The only way to stop this tracking is to tell git to remove them.停止此跟踪的唯一方法是告诉 git 删除它们。 By using git rm --cached , you prevent git from deleting your local files, but any other repository getting your changes will apply the removal.通过使用git rm --cached ,您可以防止 git 删除您的本地文件,但任何其他获得您更改的存储库都将应用删除。 I don't think there's a way to avoid that from your own repository.我认为没有办法从您自己的存储库中避免这种情况。 You must do something on the other repositories, or accept the files will be removed.您必须在其他存储库上执行某些操作,否则接受文件将被删除。

To prevent the removal on each other repository you can:为了防止在其他存储库上被删除,您可以:

  • (obviously) backup the files somewhere, pull the changes and restore the files, (显然)在某处备份文件,拉取更改并恢复文件,
  • or also git rm --cached the files and commit before pulling your changes.或者git rm --cached文件并在拉取更改之前提交。 Git will nicely merge the two removals without touching the already untracked files. Git 将很好地合并这两个删除,而不会触及已经未跟踪的文件。

Put a / at the end of the directory name in your .gitignore file, ie在你的.gitignore文件中的目录名末尾加上/ ,即

tmp/

If have tracked files inside that directory, you need to tell git to forget about them first (before you add the dir to the ignore list).如果在该目录中有跟踪文件,您需要告诉 git 先忘记它们(在将目录添加到忽略列表之前)。 Assuming you have nothing vital in there (ie that you can scratch it):假设您在那里没有任何重要的东西(即您可以刮擦它):

git rm -rf ./tmp/
git commit -m "untrack tmp dir"
mkdir tmp
echo tmp/ >> .gitignore
git add .gitignore ; git commit -m "add tmp/ to ignore list"

New files in that directory will not be tracked.该目录中的新文件将不会被跟踪。

The --cached option to git rm only works on the index (pending changes more or less). git rm--cached选项仅适用于索引(或多或少有待更改)。 It has no effect on the working tree or the status of what has been tracked or not.它对工作树或已跟踪内容的状态没有影响。

.gitignore has no effect on tracked files. .gitignore 对跟踪的文件没有影响。

What you want is to set the assume-unchanged bit on the files in the tmp/ directory.您想要的是在 tmp/ 目录中的文件上设置假设不变位。 There's a good explanation how to do that here: Git: untrack a file in local repo only and keep it in the remote repo这里有一个很好的解释: Git: untrack a file in local repo only and keep it in the remote repo

Also, one-liners for setting assume-unchanged on all files in a directory - git update-index --assume-unchanged on directory .此外,用于在目录中的所有文件上设置假设不变的单行 - git update-index --assume-unchanged on directory

It sounds like you are trying to track a file (eg index.php ), add it to a remote repository, then stop watching tracking it, while keeping the file in the remote (ie keep index.php unchanged on the remote repo while changing it locally).听起来您正在尝试跟踪文件(例如index.php ),将其添加到远程存储库,然后停止观看跟踪它,同时将文件保留在远程(即在更改时保持index.php在远程存储库上保持不变)它在本地)。

From what I understand, git cannot do this.据我了解,git 无法做到这一点。 You can either track a file, or not.您可以跟踪文件,也可以不跟踪。 If you track a file, it exists in the remote repo, and changes when you commit changes to it.如果您跟踪一个文件,它存在于远程仓库中,并在您提交更改时更改。 If you don't track a file, it doesn't exist in the remote repo.如果您不跟踪文件,则远程存储库中不存在该文件。

Because it is not possible to do exactly what you want with git, there are potentially other solutions, depending on your exact situation.由于 git 无法完全按照您的要求进行操作,因此可能还有其他解决方案,具体取决于您的具体情况。 For example, why do you not want index.php to change on remote when you change it locally?例如,当您在本地更改index.php时,为什么不希望远程更改它? Are there user-specific settings in the file?文件中是否有用户特定的设置? If this is the case, you can do:如果是这种情况,您可以这样做:

cp index.php index_template.php
git rm --cached index.php

Now edit index_template.php to be as you want it to appear on the remote repo.现在将 index_template.php 编辑为您希望它出现在远程仓库中的样子。 Add something to your README to tell the people using your repository that once they clone it, they must copy index_template.php to index.php and edit it to suit their needs.在您的 README 中添加一些内容,告诉使用您的存储库的人,一旦他们克隆它,他们必须将 index_template.php 复制到 index.php 并对其进行编辑以满足他们的需要。

git add index_template.php
git add README
git commit -m 'added template index.php file'
git push

When someone clones your repo, they must create their own index.php .当有人克隆你的仓库时,他们必须创建自己的index.php You've made it easy for them: simply copy index_template.php to index.php and revise it with computer-specific settings.您已经为他们简化了:只需将index_template.php复制到index.php并使用特定于计算机的设置进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM