从git tracking中删除文件夹

本文翻译自:Remove a folder from git tracking

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 status they show up as deleted. 但是当我运行git status它们会显示为已删除。 If I try to commit the changes, the files will be deleted, not only removed from tracking. 如果我尝试提交更改,则文件将被删除,不仅会从跟踪中删除。

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. 但我需要从跟踪中删除整个文件夹(包括子文件夹)。

#1楼

参考:https://stackoom.com/question/1dv1y/从git-tracking中删除文件夹

#2楼

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. 换句话说,您可能希望将文件保留在硬盘驱动器上,但不要让Git跟踪它。 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. 如果您忘记在.gitignore文件中添加内容并意外地将其添加到某个日志文件或一堆.a编译文件中,这将非常有用。 To do this, use the --cached option: 为此,请使用--cached选项:

$ git rm --cached readme.txt

So maybe don't include the "-r"? 所以也许不包括“-r”?

#3楼

I came across this question while Googling for "git remove folder from tracking". 我在谷歌搜索“git从跟踪中删除文件夹”时遇到了这个问题。 The OP's question lead me to the answer. OP的问题引出了我的答案。 I am summarizing it here for future generations. 我在这里为后代总结一下。

Question

How do I remove a folder from my git repository without deleting it from my local machine (ie, development environment)? 如何从我的git存储库中删除文件夹而不从本地计算机(即开发环境)中删除它?

Answer 回答

Step 1. Add the folder path to your repo's root .gitignore file. 步骤1.将文件夹路径添加到repo的root .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 (ie 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

#4楼

This works for me: 这对我有用:

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

--ignore-unmatch is important here, without that option git will exit with error on the first file not in the index. --ignore-unmatch在这里很重要,如果没有那个选项,git将在不在索引中的第一个文件上以错误退出。

#5楼

To forget directory recursively add /*/* to the path: 要忘记目录递归地将/*/*添加到路径:

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

Using git rm --cached is not good for collaboration. 使用git rm --cached不利于协作。 More details here: How to stop tracking and ignore changes to a file in Git? 更多细节: 如何停止跟踪并忽略Git中文件的更改?

#6楼

Step 2.5: Commit your changes: 步骤2.5:提交您的更改:

>git commit

If you push without doing this first it does nothing! 如果你没有这样做就先推,它什么都不做!

没有账号? 忘记密码?

社交账号快速登录