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

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

之后我添加了。gitignore的路径

/wordpress/wp-content/uploads

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

我做错了什么?

我也试过

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

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


当前回答

我也是一个初学者,下面是我的解决方案。这个问题应该很容易解决:

In your terminal, make sure you are in the correct folder. For me, it should like: my-MacBook:~ myUserName$ Input: git status It should give you a list of folders and files that git tracking now. Copy them. Input: code .gitignore Your default code editor will open this file, or you can find it and open manually. Paste the list of folders and files name from step 3. Make sure there is no tab or space at beginning of each line. Save and close this file (.gitignore) Go back to the terminal and input: git status If you see the untracked files only have: .gitignore, it means it works now. Input: git add .gitignore Input: git commit -m "what ever message you want to left here" Now you should be all set!

其他回答

我在谷歌搜索“git从跟踪中删除文件夹”时遇到了这个问题。记者的问题让我找到了答案。我在这里为子孙后代总结一下。

问题

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

回答

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

path_to_your_folder/

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

git rm -r --cached path_to_your_folder/

步骤3。将您的更改推到git回购。

从Git的角度来看,这个文件夹将被认为是“删除”的(例如,它们在过去的历史中,但不在最近的提交中,从这个回购中提取的人将从他们的树中删除文件),但仍然保留在你的工作目录中,因为你使用了—缓存。

从git文档中:

您可能想要做的另一件有用的事情是将该文件保留在工作树中,但将其从登台区域中删除。换句话说,您可能希望将文件保存在硬盘驱动器上,但不再让Git跟踪它。如果您忘记向.gitignore文件中添加一些内容,并意外地将其放置,例如一个大的日志文件或一堆.a编译文件,那么这将特别有用。要做到这一点,使用——cached选项:

$ git rm --cached readme.txt

所以也许不包括"-r"?

要递归地忘记目录,在路径中添加/*/*:

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

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

这对我来说很管用:

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

——ignore-unmatch在这里很重要,如果没有这个选项,git将在索引之外的第一个文件上退出并报错。

我也是一个初学者,下面是我的解决方案。这个问题应该很容易解决:

In your terminal, make sure you are in the correct folder. For me, it should like: my-MacBook:~ myUserName$ Input: git status It should give you a list of folders and files that git tracking now. Copy them. Input: code .gitignore Your default code editor will open this file, or you can find it and open manually. Paste the list of folders and files name from step 3. Make sure there is no tab or space at beginning of each line. Save and close this file (.gitignore) Go back to the terminal and input: git status If you see the untracked files only have: .gitignore, it means it works now. Input: git add .gitignore Input: git commit -m "what ever message you want to left here" Now you should be all set!