<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Git on 蛋蛋忧桑</title><link>https://dandansad.com/tags/git/</link><description>Recent content in Git on 蛋蛋忧桑</description><generator>Hugo -- gohugo.io</generator><language>zh</language><lastBuildDate>Tue, 04 Mar 2025 00:00:47 +0800</lastBuildDate><atom:link href="https://dandansad.com/tags/git/index.xml" rel="self" type="application/rss+xml"/><item><title>从 Git 历史提交中删除文件</title><link>https://dandansad.com/2025/03/04/%E4%BB%8E-git-%E5%8E%86%E5%8F%B2%E6%8F%90%E4%BA%A4%E4%B8%AD%E5%88%A0%E9%99%A4%E6%96%87%E4%BB%B6/</link><pubDate>Tue, 04 Mar 2025 00:00:47 +0800</pubDate><guid>https://dandansad.com/2025/03/04/%E4%BB%8E-git-%E5%8E%86%E5%8F%B2%E6%8F%90%E4%BA%A4%E4%B8%AD%E5%88%A0%E9%99%A4%E6%96%87%E4%BB%B6/</guid><description>&lt;h1 id="注意"&gt;&lt;a href="#%e6%b3%a8%e6%84%8f" class="header-anchor"&gt;&lt;/a&gt;&lt;strong&gt;注意&lt;/strong&gt;！！！
&lt;/h1&gt;&lt;p&gt;&lt;strong&gt;亲测！！！两个方案都会在删除提交记录的同时把原文件删除！！！一定要记得备份文件再操作！！！如果仓库非常重要的话建议操作前备份整个仓库！！！&lt;/strong&gt;&lt;/p&gt;
&lt;h1 id="方案一使用git-filter-repo工具"&gt;&lt;a href="#%e6%96%b9%e6%a1%88%e4%b8%80%e4%bd%bf%e7%94%a8git-filter-repo%e5%b7%a5%e5%85%b7" class="header-anchor"&gt;&lt;/a&gt;方案一：使用git filter-repo工具
&lt;/h1&gt;&lt;ol&gt;
&lt;li&gt;安装 git-filter-repo 工具&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;pip install git-filter-repo
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ol start="2"&gt;
&lt;li&gt;运行命令删除example.txt文件提交记录，&amp;ndash;path后跟文件路径，如“src/main/resources/1.txt”&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;git filter-repo --path example.txt --invert-paths
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h1 id="方案二使用git-filter-branch命令"&gt;&lt;a href="#%e6%96%b9%e6%a1%88%e4%ba%8c%e4%bd%bf%e7%94%a8git-filter-branch%e5%91%bd%e4%bb%a4" class="header-anchor"&gt;&lt;/a&gt;方案二：使用git filter-branch命令
&lt;/h1&gt;&lt;p&gt;运行命令删除example.txt文件提交记录，如果不在当前目录，请加上相对路径，如“src/main/resources/example.txt”：&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;git filter-branch --force --index-filter &amp;#39;git rm --cached --ignore-unmatch example.txt&amp;#39; --prune-empty --tag-name-filter cat -- --all
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h1 id="同步远程仓库"&gt;&lt;a href="#%e5%90%8c%e6%ad%a5%e8%bf%9c%e7%a8%8b%e4%bb%93%e5%ba%93" class="header-anchor"&gt;&lt;/a&gt;同步远程仓库
&lt;/h1&gt;&lt;p&gt;如果有远程 git 仓库，请在操作前先同步所有分支，删除文件后再执行以下命令。&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;git push origin --force --all&lt;/code&gt;：强制推送所有分支。&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;git push origin --force --tags&lt;/code&gt;：强制推送所有标签。&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;如果有 fork 仓库的话，除了让 fork 仓库的所有人删除仓库，否则就没办法了。&lt;/p&gt;
&lt;h1 id="参考文章"&gt;&lt;a href="#%e5%8f%82%e8%80%83%e6%96%87%e7%ab%a0" class="header-anchor"&gt;&lt;/a&gt;参考文章
&lt;/h1&gt;&lt;p&gt;&lt;a class="link" href="https://geek-docs.com/git/git-questions/1694_git_git_how_to_remove_file_from_historical_commit.html" target="_blank" rel="noopener"
 &gt;Git Git: 如何从历史提交中删除文件|极客教程&lt;/a&gt;&lt;/p&gt;</description></item></channel></rss>