从 git 跟踪中删除文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24290358/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 18:12:37  来源:igfitidea点击:

Remove a folder from git tracking

gitgit-rm

提问by Stefan Diabo

I need to exclude a folder (name uploads) from tracking. I tried to run

我需要从跟踪中排除文件夹(名称上传)。我试着跑

git rm -r --cached wordpress/wp-content/uploads

and after that I added the path to .gitignore

之后我添加了 .gitignore 的路径

/wordpress/wp-content/uploads

but when I ran git statusthey show up as deleted. If I try to commit the changes, the files will be deleted, not only removed from tracking.

但是当我运行时,git status它们显示为已删除。如果我尝试提交更改,文件将被删除,而不仅仅是从跟踪中删除。

What am I doing wrong?

我究竟做错了什么?

I have also tried

我也试过

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

but this seems to untrack only files. But I need to remove an entire folder (including subfolders) from tracking.

但这似乎只能取消跟踪文件。但是我需要从跟踪中删除整个文件夹(包括子文件夹)。

回答by Tod Birdsall

I came across this question while Googling for "git remove folder from tracking". The OP's question lead me to the answer. I am summarizing it here for future generations.

我在谷歌搜索“git remove folder from tracking”时遇到了这个问题。OP的问题让我找到了答案。我在这里为子孙后代总结一下。

Question

How do I remove a folder from my git repository without deleting it from my local machine (i.e., development environment)?

如何从我的 git 存储库中删除文件夹而不从我的本地机器(即开发环境)中删除它?

Answer

回答

Step 1. Add the folder path to your repo's root .gitignorefile.

步骤 1. 将文件夹路径添加到您的 repo 的根.gitignore文件中。

path_to_your_folder/

Step 2. Remove the folder from your local git tracking, but keep it on your disk.

步骤 2. 从本地 git 跟踪中删除文件夹,但将其保留在磁盘上。

git rm -r --cached path_to_your_folder/

Step 3. Push your changes to your git repo.

第 3 步。将您的更改推送到您的 git 存储库。

The folder will be considered "deleted" from Git's point of view (i.e. they are in past history, but not in the latest commit, and people pulling from this repo will get the files removed from their trees), but stay on your working directory because you've used --cached.

从 Git 的角度来看,该文件夹将被视为“已删除”(即它们在过去的历史记录中,但不在最新的提交中,并且从该存储库中提取的文件将从他们的树中删除),但保留在您的工作目录中因为你用过--cached.

回答by qed

This works for me:

这对我有用:

git rm -r --cached --ignore-unmatch folder_name

--ignore-unmatchis important here, without that option git will exit with error on the first file not in the index.

--ignore-unmatch在这里很重要,如果没有该选项,git 将在第一个不在索引中的文件上出现错误退出。

回答by vitalets

To forget directory recursively add /*/*to the path:

要递归地忘记目录添加/*/*到路径:

git update-index --assume-unchanged wordpress/wp-content/uploads/*/*

Using git rm --cachedis not good for collaboration. More details here: How to stop tracking and ignore changes to a file in Git?

使用git rm --cached不利于协作。更多细节在这里:如何停止跟踪并忽略对 Git 中文件的更改?

回答by lukehillonline

I know this is an old thread but I just wanted to add a little as the marked solution didn't solve the problem for me (although I tried many times).

我知道这是一个旧线程,但我只想添加一点,因为标记的解决方案并没有为我解决问题(尽管我尝试了很多次)。

The only way I could actually stop git form tracking the folder was to do the following:

我实际上可以停止 git form 跟踪文件夹的唯一方法是执行以下操作:

  1. Make a backup of the local folder and put in a safe place.
  2. Delete the folder from your local repo
  3. Make sure cache is cleared git rm -r --cached your_folder/
  4. Add your_folder/to .gitignore
  5. Commit changes
  6. Add the backup back into your repo
  1. 备份本地文件夹并放在安全的地方。
  2. 从本地存储库中删除文件夹
  3. 确保清除缓存 git rm -r --cached your_folder/
  4. 添加your_folder/到 .gitignore
  5. 提交更改
  6. 将备份添加回您的仓库

You should now see that the folder is no longer tracked.

您现在应该看到该文件夹​​不再被跟踪。

Don't ask me why just clearing the cache didn't work for me, I am not a Git super wizard but this is how I solved the issue.

不要问我为什么只清除缓存对我不起作用,我不是 Git 超级向导,但这就是我解决问题的方法。

回答by user3750325

From the git documentation:

从 git 文档:

Another useful thing you may want to do is to keep the file in your working tree but remove it from your staging area. In other words, you may want to keep the file on your hard drive but not have Git track it anymore. This is particularly useful if you forgot to add something to your .gitignore file and accidentally staged it, like a large log file or a bunch of .a compiled files. To do this, use the --cached option:

您可能想做的另一件事是将文件保留在工作树中,但将其从暂存区中删除。换句话说,您可能希望将文件保存在硬盘驱动器上,但不再让 Git 跟踪它。如果您忘记向 .gitignore 文件添加某些内容并意外将其暂存,这将特别有用,例如大型日志文件或一堆 .a 编译文件。为此,请使用 --cached 选项:

$ git rm --cached readme.txt

So maybe don't include the "-r"?

所以也许不包括“-r”?