提交后.gitignore
我在 Github 上托管了一个 git 存储库。提交许多文件后,我意识到我需要创建 .gitignore
并排除 .exe
、.obj
文件。
但是,它会自动从存储库中删除这些提交的文件吗?有什么办法可以强制吗?
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.
However, will it automatically remove these committed files from the repository? Is there any way to force that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,您不能仅仅因为已添加到 .gitignore 中就强制删除已在存储库中提交的文件,
您必须使用 git rm --cached 来删除存储库中不需要的文件。 ( --cached 因为你可能想保留本地副本但从存储库中删除。)所以如果你想从存储库中删除所有 exe
(请注意,星号 * 是从 shell 中引用的 - 这让 git 和不是 shell,展开文件和子目录的路径名)
No you cannot force a file that is already committed in the repo to be removed just because it is added to the
.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(Note that the asterisk * is quoted from the shell - this lets git, and not the shell, expand the pathnames of files and subdirectories)
如果您尚未推送更改:
If you have not pushed the changes already:
不会。即使使用现有的
.gitignore
,您也可以使用-f
(强制)标志暂存“忽略”的文件。如果它们的文件已经提交,它们不会被自动删除。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.我必须删除 .idea 和目标文件夹,在阅读了这对我有用的所有评论后:
然后推送到 master
I had to remove .idea and target folders and after reading all comments this worked for me:
and then push to master
不。
执行此操作的“最佳”方法是使用
git filter-branch
,如下所示:git-filter-branch 的手册页包含全面的示例。
注意您将重写历史。如果您发布了包含意外添加的文件的任何修订版,这可能会给这些公共分支的用户带来麻烦。通知他们,或者考虑一下您多么需要删除这些文件。
注意 在存在标签的情况下,请始终对
git filter-branch
使用--tag-name-filter cat
选项。它永远不会造成伤害,并且当您后来意识到您需要它时,它会帮您减轻头痛No.
The 'best' recipe to do this is using
git filter-branch
as written about here:The man page for git-filter-branch contains comprehensive examples.
Note You'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.
Note In the presence of tags, always use the
--tag-name-filter cat
option togit filter-branch
. It never hurts and will save you the head-ache when you realize later taht you needed it即使删除文件然后提交后,历史记录中仍然会保留这些文件。要删除它们,请考虑使用 BFG Repo-Cleaner。它是 git-filter-branch 的替代品。
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.