Git 删除指定文件的所有记录

Git About 1,857 words

需求

文件中包含密钥等信息,需彻底删除所有记录。不删除仓库,保留其他提交记录。

步骤

步骤一

要删掉记录的文件的路径是(相对于项目):

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch vue/learn-vue/package-lock.json' --prune-empty --tag-name-filter cat -- --all

输出:

WARNING: git-filter-branch has a glut of gotchas generating mangled history
         rewrites.  Hit Ctrl-C before proceeding to abort, then use an
         alternative filtering tool such as 'git filter-repo'
         (https://github.com/newren/git-filter-repo/) instead.  See the
         filter-branch manual page for more details; to squelch this warning,
         set FILTER_BRANCH_SQUELCH_WARNING=1.
Proceeding with filter-branch...

Rewrite 04b17acdb6fb9b58650fb96411db4a85c69cbc43 (729/729) (707 seconds passed, remaining 0 predicted)    rm 'vue/learn-vue/package-lock.json'

Ref 'refs/heads/master' was rewritten
Ref 'refs/remotes/origin/master' was rewritten
Ref 'refs/stash' was rewritten

步骤二

本地记录覆盖到GitHub(所有branch以及所有tags)。

覆盖branch

git push origin --force --all

输出:

Enumerating objects: 7010, done.
Counting objects: 100% (7010/7010), done.
Delta compression using up to 16 threads
Compressing objects: 100% (3355/3355), done.
Writing objects: 100% (7008/7008), 2.96 MiB | 2.49 MiB/s, done.
Total 7008 (delta 2374), reused 5637 (delta 2091), pack-reused 0
remote: Resolving deltas: 100% (2374/2374), done.
To https://github.com/xxx/xxx.git
 + d2e389f...8aa2b79 master -> master (forced update)

覆盖tags

git push origin --force --tags

输出:

Everything up-to-date

步骤三

强制解除对本地存储库中的所有对象的引用

git reflog expire --expire=now --all

垃圾收集

git gc --prune=now

输出:

Enumerating objects: 7949, done.
Counting objects: 100% (7949/7949), done.
Delta compression using up to 16 threads
Compressing objects: 100% (4178/4178), done.
Writing objects: 100% (7949/7949), done.
Total 7949 (delta 3051), reused 5912 (delta 2168), pack-reused 0
Views: 1,165 · Posted: 2023-03-31

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓
Today On History
Browsing Refresh