如何在保留(并忽略)本地文件的同时删除存储库中的文件? [英] How can I delete files in the repository while keeping (and ignoring) them locally?

查看:118
本文介绍了如何在保留(并忽略)本地文件的同时删除存储库中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我错误地将一些本地项目文件添加到git存储库中并提交/推送它们.

I erroneously added some local project files to a git repository and committed/pushed them.

我想从远程存储库中删除这些文件,将其保存在本地,并在以后的提交/推送中忽略它们.

I'd like to delete these files from the remote repository, keep them locally, and ignore them for future commits/pushes.

解决此问题的最佳方法是什么?

What's the best way to go about this?

推荐答案

最干净的解决方案如下:

The cleanest solution is the following:

  • git rm --cached本地其他文件(请注意--cached选项,将这些文件保留在您的工作目录中)
  • 将它们添加到您的.gitignore文件中,然后git commit -A -m "..."
  • 推送您的分支(不会重写任何历史记录,但以前的历史记录将保留对这些文件的引用).
  • git rm --cached the extra files locally (note the --cached option to keep those files in your working directory),
  • add them to your .gitignore file and git commit -A -m "..." after that,
  • push your branch (no history rewritten, but previous history will keep references to those files).

如果您认为没有太多人退出远程仓库(理想情况下,没有人),则可以:

If you think not too many people have pulled from your remote repo (ideally, none), you could:

  • 在本地修复提交历史记录
    (git rebase --interactive first-commit-with-files^:'^'引用您确实引入了错误文件的第一个提交的父提交.
    git rm --cached文件,然后重播其他命令,除非其中一些对相同文件进行了修改.
    此处的其他解决方案-git filter-branchgit rebase ),
  • 按-强制分支,
    (但随后,请做好准备向人们指出从UPSTREAM REBASE中恢复 git rebase 手册页的部分).
    (请参见此处的上游定义)
  • fix your commit history locally
    (git rebase --interactive first-commit-with-files^: the '^' referencing the parent commit of the first one where you did introduce the bad files.
    git rm --cached the files, then replay the other commmits unless some of them have also made modifications to the same files.
    Other solutions here -- git filter-branch or git rebase),
  • push --force your branch,
    (but then, be prepared to point out people to the RECOVERING FROM UPSTREAM REBASE section of the git rebase man page).
    (see definition of upstream here)

这篇关于如何在保留(并忽略)本地文件的同时删除存储库中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆