cnblogs/dcrenl/git 删除所有提交下的某个文件.html
2024-09-24 12:43:01 +08:00

51 lines
2.1 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch upload/*' --prune-empty --tag-name-filter cat -- --all</p>
<div>&nbsp;</div>
<div>
<pre>git push origin master --force --all
</pre>
<pre><br />
</pre>
<pre><br />
</pre>
<pre><br />
</pre>
<pre><br />
我们常用的&nbsp;<code>git rm</code>&nbsp;仅对 Working Tree 构成影响,如果想永久的删除仓库中的文件或目录,那么就要用到&nbsp;<code>git filter-branch</code>&nbsp;命令了。&nbsp;<code>git filter-branch</code>&nbsp;会检索整个 Commit 历史,逐一改写 Commit Object重构整个 Tree。
</pre>
<div>
<div>
<pre><code>git&nbsp;filter-branch --tree-filter&nbsp;'rm -rf path/folder'&nbsp;HEAD git&nbsp;filter-branch --tree-filter'rm -f path/file'&nbsp;HEAD</code></pre>
</div>
</div>
<pre><br />也可以指定 检索的 Commit 历史的范围:</pre>
<div>
<div>
<pre><code>git&nbsp;filter-branch --tree-filter&nbsp;'rm -rf path/folder'&nbsp;347ae59..HEAD</code></pre>
</div>
</div>
<pre><br />最后,不要忘了向仓库强制推送所有的变化:</pre>
<div>
<div>
<pre><code>git push origin master&nbsp;--force</code></pre>
</div>
</div>
<pre><br />执行&nbsp;<code>git filter-branch</code>&nbsp;命令后,已经标记为删除的 Object 在本地仓库中要到过期后才会解除关联和进行垃圾回收。检查哪些 tags 和 branch 引用了这些 Object</pre>
<div>
<div>
<pre><code>git&nbsp;for-each-ref&nbsp;--format='delete %(refname)'&nbsp;refs/original</code></pre>
</div>
</div>
<pre><br />如果想立即解除关联,执行垃圾回收,可以这么做:</pre>
<div>
<div>
<pre><code>git&nbsp;for-each-ref&nbsp;--format='delete %(refname)'&nbsp;refs/original | git update-ref&nbsp;--stdin git reflog expire --expire=now --all git gc --prune=now</code></pre>
</div>
</div>
<pre><br />接下来,就是强制推送到仓库了:</pre>
<div>
<div>
<pre><code>git push origin&nbsp;--force --all&nbsp;git push origin&nbsp;--force --tags</code></pre>
</div>
</div>
<pre><br />不到迫不得已,不要轻易使用&nbsp;<code>git filter-branch</code>&nbsp;,因为它重构了整个 Tree</pre>
</div>