git 使用 gitignore 忽略(但不删除)文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6317169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using gitignore to ignore (but not delete) files
提问by 21312312
I have a tmp directory in my git repo I'd like to still exist, but be ignored. I added it to .gitignore
, but git status
still tells me about changes to files in that directory. I tried git rm -r --cached
, but that removes it from the remote repo. 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. What should I do?
我的 git repo 中有一个 tmp 目录,我希望它仍然存在,但被忽略。我将它添加到.gitignore
,但git status
仍然告诉我该目录中文件的更改。我试过了git rm -r --cached
,但这会将它从远程仓库中删除。如何停止跟踪对此目录的更改,但仍允许它存在?我还需要为 1 个文件执行此操作,但是git status
在.gitignore
ing之后也会显示对这些文件的更改。我该怎么办?
回答by ducin
Instead of .gitignore
, you can update local git repository by running following command:
代替.gitignore
,您可以通过运行以下命令来更新本地 git 存储库:
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. Read more at:
在这种情况下,原始存储库中正在跟踪文件。您可以在本地存储库中修改它,git 永远不会将其标记为已更改。阅读更多信息:
- http://blog.pagebakers.nl/2009/01/29/git-ignoring-changes-in-tracked-files/- was reported dead at some time (sorry, not mine)
- http://archive.robwilkerson.org/2010/03/02/git-tip-ignore-changes-to-tracked-files/- another one covering the same topic
回答by Micha?l Witrant
Ignoring changes made to files while allowing them to exist is the exact purpose of .gitignore
. So adding the files (or directories) to .gitignore
is the only thing you have to do.
忽略对文件所做的更改,同时允许它们存在是.gitignore
. 因此,添加文件(或目录).gitignore
是您唯一要做的事情。
But your problem is that git is already tracking the files you want to ignore and .gitignore
doesn't apply to tracked files. The only way to stop this tracking is to tell git to remove them. By using git rm --cached
, you prevent git from deleting your local files, but any other repository getting your changes will apply the removal. 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.
但是您的问题是 git 已经在跟踪您要忽略的文件,.gitignore
并且不适用于跟踪的文件。停止这种跟踪的唯一方法是告诉 git 删除它们。通过使用git rm --cached
,您可以防止 git 删除您的本地文件,但任何其他获取您更改的存储库都将应用删除。我认为没有办法从您自己的存储库中避免这种情况。您必须对其他存储库执行某些操作,否则将删除文件。
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 beforepulling your changes. Git will nicely merge the two removals without touching the already untracked files.
- (显然)在某处备份文件,提取更改并恢复文件,
- 或者在拉取更改之前
git rm --cached
提交文件和提交。Git 将很好地合并两个删除,而不会触及已经未跟踪的文件。
回答by Mat
Put a /
at the end of the directory name in your .gitignore
file, i.e.
将 a/
放在.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). Assuming you have nothing vital in there (i.e. that you can scratch it):
如果在该目录中跟踪了文件,则需要先告诉 git 忘记它们(在将目录添加到忽略列表之前)。假设你没有任何重要的东西(即你可以刮擦它):
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). It has no effect on the working tree or the status of what has been tracked or not.
仅适用于索引的--cached
选项git rm
(或多或少的待定更改)。它对工作树或已跟踪或未跟踪内容的状态没有影响。
回答by Gabe Kopley
.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. 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
您想要的是在 tmp/ 目录中的文件上设置假设不变位。这里有一个很好的解释: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。
回答by Cypress Frankenfeld
It sounds like you are trying to track a file (e.g. index.php
), add it to a remote repository, then stop watching tracking it, while keeping the file in the remote (i.e. keep index.php
unchanged on the remote repo while changing it locally).
听起来您正在尝试跟踪文件(例如index.php
),将其添加到远程存储库,然后停止监视跟踪它,同时将文件保留在远程(即index.php
在本地更改时在远程存储库上保持不变)。
From what I understand, git cannot do this. 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.
据我了解,git 无法做到这一点。您可以跟踪文件,也可以不跟踪。如果你跟踪一个文件,它存在于远程仓库中,并在你提交更改时更改。如果您不跟踪文件,则远程存储库中不存在该文件。
Because it is not possible to do exactly what you want with git, there are potentially other solutions, depending on your exact situation. For example, why do you not want index.php
to change on remote when you change it locally? Are there user-specific settings in the file? If this is the case, you can do:
因为不可能用 git 做你想做的事情,所以可能有其他的解决方案,这取决于你的具体情况。例如,为什么index.php
在本地更改时不想远程更改?文件中是否有用户特定的设置?如果是这种情况,您可以这样做:
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. 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.
现在编辑 index_template.php 以使其出现在远程存储库中。在你的自述文件中添加一些内容,告诉使用你的存储库的人,一旦他们克隆它,他们必须将 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
. You've made it easy for them: simply copy index_template.php
to index.php
and revise it with computer-specific settings.
当有人克隆您的存储库时,他们必须创建自己的index.php
. 您让他们轻松了:只需复制index_template.php
到index.php
并使用特定于计算机的设置进行修改。