git 忽略已签入目录的内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1329291/
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
Ignoring an already checked-in directory's contents?
提问by Felixyz
I have a git repository that's used only to hold graphics and sound files used in several projects. They are all in one directory without sub-directories. Now I just created a script to copy these assets over from another, structured directory, with several levels of sub-directories.
我有一个 git 存储库,仅用于保存多个项目中使用的图形和声音文件。它们都在一个目录中,没有子目录。现在我刚刚创建了一个脚本来从另一个结构化目录复制这些资产,该目录具有多个级别的子目录。
Now I only want the (source) hierarchical file structure to be tracked by git, and the (target) flat directory (with all the files in one pile) should be ignored.
现在我只希望 git 跟踪(源)分层文件结构,而(目标)平面目录(所有文件都在一堆)应该被忽略。
I've added the target directory to .gitignore, but git is still tracking changes in it. I thought if I commit the deletion of the old file in the target directory, git might stop tracking the new contents (copied in by the script), but it doesn't.
我已将目标目录添加到 .gitignore,但 git 仍在跟踪其中的更改。我想如果我提交删除目标目录中的旧文件,git 可能会停止跟踪新内容(由脚本复制),但事实并非如此。
How do I make git forget about the target directory?
如何让 git 忘记目标目录?
回答by Gordon Wilson
This command will cause git to untrack your directory and all files under it without actually deleting them:
此命令将导致 git 取消跟踪您的目录及其下的所有文件,而不会实际删除它们:
git rm -r --cached <your directory>
git rm -r --cached <your directory>
The -r
option causes the removal of all files under your directory.
该-r
选项会导致删除您目录下的所有文件。
The --cached
option causes the files to only be removed from git's index, not your working copy. By default git rm <file>
would delete <file>
.
该--cached
选项会导致文件仅从 git 的索引中删除,而不是您的工作副本。默认情况下git rm <file>
会删除<file>
.
回答by jone
If you need to have a tracked file (checked in), but do not want to track further changes of a file while keeping it in your local repository as well as in the remote repository, this can be done with:
如果您需要跟踪文件(签入),但不想在将文件保存在本地存储库和远程存储库中的同时跟踪文件的进一步更改,可以通过以下方式完成:
git update-index --assume-unchanged path/to/file.txt
After that any changes to this file will no longer show up in git status
.
之后,对此文件的任何更改将不再显示在git status
.
回答by Stef
For a subdirectory called blah/
added to git, both of the following seem to work to ignore new files in blah/
. Added to .gitignore:
对于名为blah/
added to git的子目录,以下两个似乎都可以忽略 .gitignore 中的新文件blah/
。添加到 .gitignore:
blah
blah/*
回答by Ced
TL;DR to clean up your git repository and make sure ALLyour ignored items are indeed ignored:
TL;DR 清理您的 git 存储库并确保您确实忽略了所有被忽略的项目:
git rm -r --cached .
git add .
git commit -m "Clean up ignored files"
Note: you don't need to specify a directory, this way your are cleaning the entire remote repo.
注意:您不需要指定目录,这样您就可以清理整个远程 repo。
To go further read this
要进一步阅读本文
回答by Steve
Not sure if this counts or makes me a bad person, but here it goes.
不确定这是否重要或使我成为一个坏人,但它就在这里。
- I added
*Generated*
to the root .gitignore file - I submitted the files I want to keep as GeneratedFile.Whatever.ext.CheckedIn
- I made a git hook on post checkout to call a powershell script doing this:
- 我添加
*Generated*
到根 .gitignore 文件 - 我提交了我想保留为 GeneratedFile.Whatever.ext 的文件。入住
- 我在结帐后创建了一个 git hook 来调用执行此操作的 powershell 脚本:
dir *.CheckedIn -Recurse | %{ copy $_.FullName "$($_.FullName)".Replace("CheckedIn","") -EA SilentlyContinue}
dir *.CheckedIn -Recurse | %{ copy $_.FullName "$($_.FullName)".Replace("CheckedIn","") -EA SilentlyContinue}
I do feel a little bad about myself... but it does in fact work.
我确实对自己感觉有点不好……但它确实有效。
回答by Felixyz
Ok, it seems that you must first do a check-in with the directory completely empty (neither old nor new files), and any files added thereafter will be ignored. If you remove the old files add and new ones before committing, the new ones are added to the repo although they should be ignored.
好吧,看来您必须首先在目录完全为空(既不是旧文件也不是新文件)的情况下进行签入,之后添加的任何文件都将被忽略。如果在提交之前删除旧文件添加和新文件,新文件将添加到存储库中,尽管它们应该被忽略。
At least, this worked for me in this situation. Would still be great if anyone could provide more insight on what's going on.
至少,在这种情况下,这对我有用。如果有人可以提供有关正在发生的事情的更多见解,那仍然会很棒。