V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
git
Pro Git
Atlassian Git Tutorial
Pro Git 简体中文翻译
GitX
smdbh
V2EX  ›  git

git 误提交的文件怎么彻底删除?或是有什么最佳实践避免错误提交?

  •  
  •   smdbh · 263 天前 · 1745 次点击
    这是一个创建于 263 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我认为的彻底删除是 objects 里对应的文件也被删除,包括本地和远程的。最主要的还是远程目录上文件如何处理 ,自建 git 有的还和数据库绑定,手动操作了服务器上的 git 目录和数据库的不一致,仓库网页就打不开了

    11 条回复    2023-08-09 08:44:23 +08:00
    shentar
        1
    shentar  
       263 天前
    reset hard
    push force
    应该是彻底删除。如果你的 commit 还没被同步到其他分支,应该这样就可以了。
    liununu
        2
    liununu  
       263 天前 via iPhone
    1. Rebase -i 去 Drop commits ,再 Force Push 。但是其他人拉过更新,有可能再 Push 上去。
    2. 在 .gitignore 里面添加文件或目录之类的
    LeegoYih
        4
    LeegoYih  
       263 天前
    git reset head~
    git push -f
    Vegetable
        6
    Vegetable  
       263 天前   ❤️ 1
    先 drop commit
    之后 git gc ,其实本地无所谓,远程自建的 git 也都可以在设置定期 gc ,什么手动操作不存在的

    比如 gitlab https://docs.gitlab.com/ee/administration/housekeeping.html
    gitea 没找到文档,不过管理后台是有定时 gc 任务的。

    github 之类的托管网站自己也会执行,你不用担心这些,只要保证提交记录都清理干净就行
    Trim21
        7
    Trim21  
       263 天前
    首先确保对应的 commit 已经都删掉了,不在哪个 branch/tag 的历史里

    然后等服务器 gc 。
    ysc3839
        8
    ysc3839  
       263 天前 via Android
    可以用 git filter-repo 修改历史提交
    https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository
    删除后 force push
    但是曾经的 commit 不会被删除,需要 gc 才会删除,本地可以手动 gc ,但是远程仓库就比较麻烦了
    geelaw
        9
    geelaw  
       263 天前   ❤️ 1
    已经 push 到托管服务的 commit ,客户通常难以控制何时删除——在 GitHub 里,即使 force push 之后某个 commit 已经不可达,使用 commit hash 依然可以访问未被删除的 commit (见 #3 的链接)。

    最好的补救措施就是让错误提交的数据无意义。避免错误提交的方式,除了先写好 .gitignore 之外就是不要 git add .,而是复核修改之后再添加(我一般用 GUI 工具)。
    lee015
        10
    lee015  
       262 天前 via Android   ❤️ 1
    这个 bfg cleaner 工具可以彻底删除文件。原来有同事误提交并 push 了一个 dump 文件几个 G ,就是这个工具删除的,删完了看远端仓库大小也恢复正常了。
    https://rtyley.github.io/bfg-repo-cleaner/
    qwerthhusn
        11
    qwerthhusn  
       262 天前   ❤️ 1
    本地 reset 加上 force push

    远端执行一次 pruge gc 就行了(你要有 git 服务端的管理员权限)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2895 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 14:45 · PVG 22:45 · LAX 07:45 · JFK 10:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.