git .gitignore 提交后
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6535362/
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
.gitignore after commit
提问by Madhur Ahuja
I have a git repository hosted on Github. After committing many files, I am realizing that I need to create .gitignore
and exclude .exe
, .obj
files.
我在 Github 上托管了一个 git 存储库。提交许多文件后,我意识到我需要创建.gitignore
和排除.exe
,.obj
文件。
However, will it automatically remove these committed files from the repository? Is there any way to force that?
但是,它会自动从存储库中删除这些提交的文件吗?有什么办法可以强制吗?
回答by manojlds
No you cannot force a file that is already committed in the repo to be removed just because it is added to the .gitignore
不,您不能仅仅因为将已在 repo 中提交的文件添加到 .gitignore
You have to git rm --cached
to remove the files that you don't want in the repo. ( --cached since you probably want to keep the local copy but remove from the repo. ) So if you want to remove all the exe's from your repo do
您必须git rm --cached
删除不需要的文件。( --cached 因为您可能想保留本地副本但从 repo 中删除。)因此,如果您想从 repo 中删除所有 exe,请执行
git rm --cached /\*.exe
(Note that the asterisk * is quoted from the shell - this lets git, and not the shell, expand the pathnames of files and subdirectories)
(请注意,星号 * 是从 shell 引用的 - 这让 git 而不是 shell 扩展文件和子目录的路径名)
回答by KingCrunch
However, will it automatically remove these committed files from the repository?
但是,它会自动从存储库中删除这些提交的文件吗?
No. Even with an existing .gitignore
you are able to stage "ignored" files with the -f
(force) flag. If they files are already commited, they don't get removed automatically.
不。即使是现有的,.gitignore
您也可以使用-f
(force) 标志暂存“忽略”的文件。如果它们的文件已经提交,它们不会被自动删除。
git rm --cached path/to/ignored.exe
回答by slal
If you have not pushed the changes already:
如果您尚未推送更改:
git rm -r --cached .
git add .
git commit -m 'clear git cache'
git push
回答by Emanuele
I had to remove .idea and target folders and after reading all comments this worked for me:
我不得不删除 .idea 和 target 文件夹,在阅读了所有对我有用的评论后:
git rm -r .idea
git rm -r target
git commit -m 'removed .idea folder'
and then push to master
然后推到主人
回答by sehe
However, will it automatically remove these committed files from the repository?
但是,它会自动从存储库中删除这些提交的文件吗?
No.
不。
The 'best' recipe to do this is using git filter-branch
as written about here:
做到这一点的“最佳”方法是使用git filter-branch
这里写的:
The man page for git-filter-branch contains comprehensive examples.
git-filter-branch 的手册页包含全面的示例。
NoteYou'll be re-writing history. If you had published any revisions containing the accidentally added files, this could create trouble for users of those public branches. Inform them, or perhaps think about how badly you need to remove the files.
注意您将重写历史记录。如果您发布了任何包含意外添加文件的修订,这可能会给这些公共分支的用户带来麻烦。通知他们,或者考虑一下您需要删除文件的程度。
NoteIn the presence of tags, always use the --tag-name-filter cat
option to git filter-branch
. It never hurts and will save you the head-ache when you realize later taht you needed it
注意如果存在标签,请始终使用--tag-name-filter cat
选项git filter-branch
。它永远不会受到伤害,并且当您稍后意识到您需要它时,它会为您省去头疼的麻烦
回答by Ash
Even after you delete the file(s) and then commit, you will still have those files in history. To delete those, consider using BFG Repo-Cleaner. It is an alternative to git-filter-branch.
即使在您删除文件然后提交之后,您仍然会在历史记录中保留这些文件。要删除这些,请考虑使用BFG Repo-Cleaner。它是 git-filter-branch 的替代品。