git 将目录添加到 .gitignore 后从远程存储库中删除目录

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

Remove directory from remote repository after adding them to .gitignore

gitgithubgitignore

提问by janw

I committed and pushed some directory to github. After that, I altered the .gitignorefile adding a directory that should be ignored. Everything works fine, but the (now ignored) directory stays on github.

我提交并推送了一些目录到 github。之后,我修改了.gitignore文件,添加了一个应该被忽略的目录。一切正常,但(现在被忽略)目录保留在 github 上。

How do I delete that directory from github and the repository history?

如何从 github 和存储库历史记录中删除该目录?

回答by Mark Longair

The rules in your .gitignorefile only apply to untracked files. Since the files under that directory were already committed in your repository, you have to unstage them, create a commit, and push that to GitHub:

.gitignore文件中的规则仅适用于未跟踪的文件。由于该目录下的文件已经在您的存储库中提交,您必须取消暂存,创建提交,并将其推送到 GitHub:

git rm -r --cached some-directory
git commit -m 'Remove the now ignored directory "some-directory"'
git push origin master

You can't delete the file from your history without rewriting the history of your repository - you shouldn't do this if anyone else is working with your repository, or you're using it from multiple computers. If you still want to do that, you can use git filter-branchto rewrite the history - there is a helpful guide to that here.

您不能在不重写存储库历史记录的情况下从历史记录中删除文件 - 如果其他人正在使用您的存储库,或者您正在多台计算机上使用它,则不应这样做。如果您仍然想这样做,您可以使用git filter-branch重写历史记录 -这里有一个有用的指南

Additionally, note the output from git rm -r --cached some-directorywill be something like:

此外,请注意来自的输出git rm -r --cached some-directory将类似于:

rm 'some-directory/product/cache/1/small_image/130x130/small_image.jpg'
rm 'some-directory/product/cache/1/small_image/135x/small_image.jpg'
rm 'some-directory/.htaccess'
rm 'some-directory/logo.jpg'

The rmis feedback from git about the repository; the files are still in the working directory.

rm是来自 git 关于存储库的反馈;这些文件仍在工作目录中。

回答by Blundell

I do this:

我这样做:

git rm --cached `git ls-files -i --exclude-from=.gitignore` 
git commit -m 'Removed all files that are in the .gitignore' 
git push origin master

Which will remove all the files/folders that are in your git ignore, saving you have to pick each one manually

这将删除 git ignore 中的所有文件/文件夹,从而节省您必须手动选择每个文件/文件夹



This seems to have stopped working for me, I now do:

这似乎对我不起作用,我现在这样做:

 git rm -r --cached . 
 git add .
 git commit -m 'Removed all files that are in the .gitignore' 
 git push origin master

回答by eirenaios

As per my Answer here: How to remove a directory from git repository?

根据我在此处的回答:如何从 git 存储库中删除目录?

To remove folder/directory only from git repository and not from the local try 3 simple steps.

要仅从 git 存储库而不是从本地删除文件夹/目录,请尝试 3 个简单步骤。



Steps to remove directory

删除目录的步骤

git rm -r --cached FolderName
git commit -m "Removed folder from repository"
git push origin master


Steps to ignore that folder in next commits

在下一次提交中忽略该文件夹的步骤

To ignore that folder from next commits make one file in root named .gitignoreand put that folders name into it. You can put as many as you want

要从下一次提交中忽略该文件夹,请在根目录中创建一个名为.gitignore 的文件 ,并将该文件夹名称放入其中。你可以放任意多

.gitignorefile will be look like this

.gitignore文件看起来像这样

/FolderName


remove directory

remove directory

回答by Oncel Umut TURER

Blundell's first answer didn't work for me. However it showed me the right way. I have done the same thing like this:

布伦德尔的第一个答案对我不起作用。但是,它向我展示了正确的方法。我做了同样的事情:

> for i in `git ls-files -i --exclude-from=.gitignore`; do git rm --cached $i; done
> git commit -m 'Removed all files that are in the .gitignore'
> git push origin master

I advise you to check the files to be deleted first by running the below statement:

我建议您首先通过运行以下语句检查要删除的文件:

git ls-files -i --exclude-from=.gitignore

I was using a default .gitignore file for visual studio and I noticed that it was removing all log and bin folders in the project which was not my intended action.

我正在为 Visual Studio 使用默认的 .gitignore 文件,我注意到它正在删除项目中的所有日志和 bin 文件夹,这不是我的预期操作。

回答by efkan

Note: This solution works only with Github Desktop GUI.

注意:此解决方案仅适用于Github 桌面 GUI

By using Github Desktop GUIit is very simple.

通过使用Github 桌面 GUI,它非常简单。

  1. Move the folder onto another location (to out of the project folder) temporarily.

  2. Edit your .gitignorefile and remove the folder entry which would be remove master repository on the github page.

  3. Commit and Sync the project folder.

  4. Re-move the folder into the project folder

  5. Re-edit .gitignorefile.

  1. 暂时将文件夹移动到另一个位置(移出项目文件夹)。

  2. 编辑您的.gitignore文件并删除将在 github 页面上删除主存储库的文件夹条目。

  3. 提交并同步项目文件夹。

  4. 将文件夹重新移动到项目文件夹中

  5. 重新编辑.gitignore文件。

That's all.

就这样。

回答by Chris Aelbrecht

The answer from Blundell should work, but for some bizarre reason it didn't do with me. I had to pipe first the filenames outputted by the first command into a file and then loop through that file and delete that file one by one.

Blundell 的答案应该有效,但出于某种奇怪的原因,它与我无关。我必须首先将第一个命令输出的文件名通过管道传输到一个文件中,然后循环遍历该文件并逐个删除该文件。

git ls-files -i --exclude-from=.gitignore > to_remove.txt
while read line; do `git rm -r --cached "$line"`; done < to_remove.txt
rm to_remove.txt
git commit -m 'Removed all files that are in the .gitignore' 
git push origin master

回答by goofology

This method applies the standard .gitignore behavior, and does not require manually specifying the files that need to be ignored.

Can't use --exclude-from=.gitignoreanymore :/ - Here's the updated method:

General advice: start with a clean repo- everything committed, nothing pending in working directory or index, and make a backup!

此方法应用标准 .gitignore 行为,不需要手动指定需要忽略的文件

不能再使用--exclude-from=.gitignore了:/ - 这是更新的方法:

一般建议:从一个干净的 repo 开始- 提交的所有内容,工作目录或索引中没有任何未决的内容,并进行备份

#commit up-to-date .gitignore (if not already existing)
#this command must be run on each branch
git add .gitignore
git commit -m "Create .gitignore"

#apply standard git ignore behavior only to current index, not working directory (--cached)
#if this command returns nothing, ensure /.git/info/exclude AND/OR .gitignore exist
#this command must be run on each branch
git ls-files -z --ignored --exclude-standard | xargs -0 git rm --cached

#optionally add anything to the index that was previously ignored but now shouldn't be:
git add *

#commit again
#optionally use the --amend flag to merge this commit with the previous one instead of creating 2 commits.

git commit -m "re-applied modified .gitignore"

#other devs who pull after this commit is pushed will see the  newly-.gitignored files DELETED

If you also need to purge the newly-ignored files from the branch's commit history or if you don't want the newly-ignored files to be deleted from future pulls, see this answer.

如果您还需要从分支的提交历史记录中清除新忽略的文件,或者如果您不希望从未来 pull 中删除新忽略的文件,请参阅此答案

回答by Shaun Luttin

If you're working from PowerShell, try the following as a single command.

如果您使用 PowerShell,请尝试将以下作为单个命令。

PS MyRepo> git filter-branch --force --index-filter
>> "git rm --cached --ignore-unmatch -r .\\path\\to\\directory"
>> --prune-empty --tag-name-filter cat -- --all

Then, git push --force --all.

然后,git push --force --all

Documentation: https://git-scm.com/docs/git-filter-branch

文档:https: //git-scm.com/docs/git-filter-branch