github 删除文件记录

编程

github 删除文件记录

目标:删除某一个文件的历史记录

大一的时候用过一次,当时花了很长时间找方法。有点复杂,用完就忘了。今天翻来覆去还没找到当时的方法。不过,现在找到好几个其他方法,都不是很好的解决办法。最后找到一个 GitHub 官方提示的还行的解决办法。

不过,这个办法,会将所有的该文件的记录都彻底删除。所以,操作前,需要备份该文件记录,以供恢复使用。

GitHub 链接

夜深了,直接写怎么用

在仓库目录运行命令

1
$ git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA' --prune-empty --tag-name-filter cat -- --all

eg.有仓库如下

1
2
3
4
5
6
7
8
9
10
11
12
repostory
├── .git
├── scr
│   └── main.c
├── makefile
├── tool
│   ├── qshell
│   │   └── qshell
│   └── util.c
├── run.sh
└── README.md

我们要删除 run.sh 的记录

则:

1
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch run.sh' --prune-empty --tag-name-filter cat -- --all

看到这种结果,就是成功了,最后在 forece push 即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 cf228ce847b9bfc9363333c96f6e82a18162c6ce (5/71) (6 seconds passed, remaining 79 predicted) rm 'run.sh'
Rewrite ca9a868ff6a37623376c4eedfeb87409b065eaa0 (6/71) (7 seconds passed, remaining 75 predicted) rm 'run.sh'
Rewrite bf33f8f726bf4e5e1ce2f5f245f53827a59d3277 (7/71) (9 seconds passed, remaining 82 predicted) rm 'run.sh'
Rewrite 8727feba9ce3acefb200f52935b3fdb26b9fe1d2 (8/71) (10 seconds passed, remaining 78 predicted) rm 'run.sh'
Rewrite d22f1c8886b1f564d964f0ea17a7af2d65e4bab2 (9/71) (11 seconds passed, remaining 75 predicted) rm 'run.sh'
Rewrite 6bbf509008587e20f347bc1a28b410bc383175a0 (32/71) (44 seconds passed, remaining 53 predicted) rm 'run.sh'
Rewrite 5414535ab42c2db87e18580728ba8f967912a907 (33/71) (46 seconds passed, remaining 52 predicted) rm 'run.sh'
Rewrite 03b2dea9a6d4288a9c6dad86fd4c5165095bf8f2 (34/71) (47 seconds passed, remaining 51 predicted) rm 'run.sh'
Rewrite ead406329c496ca8b643c610bbb3ad57caa74505 (35/71) (48 seconds passed, remaining 49 predicted) rm 'run.sh'
Rewrite df31802ba00536e8203f6a228ab5dd99dad8f233 (36/71) (49 seconds passed, remaining 47 predicted) rm 'run.sh'
Rewrite f4934cff4c58c747c0a5ea9b7fab3beef11e27e4 (37/71) (51 seconds passed, remaining 46 predicted) rm 'run.sh'
Rewrite c124342928cf0b42e1b60bf401d397bcd5dc8e8d (71/71) (98 seconds passed, remaining 0 predicted) rm 'run.sh'

Ref 'refs/heads/dev' was rewritten
Ref 'refs/heads/main' was rewritten
Ref 'refs/heads/tmp' was rewritten
Ref 'refs/remotes/origin/dev' was rewritten
WARNING: Ref 'refs/remotes/origin/main' is unchanged
Ref 'refs/stash' was rewritten
Ref 'refs/tags/v1' was rewritten
Ref 'refs/tags/v1.1.0' was rewritten
v1 -> v1 (86395f415eaeb212ba88984fc2f679f11acba901 -> 86395f415eaeb212ba88984fc2f679f11acba901)
v1.1.0 -> v1.1.0 (f1d55f6e88122ea5845ac11549b303afc5c89695 -> f1d55f6e88122ea5845ac11549b303afc5c89695)

不过,据 GitHub 提示,如果要彻底删除记录,需要联系 GitHub 提供仓库名称删除仓库缓存

Author: 哒琳

Permalink: http://blog.jieis.cn/2022/f3d72115-a62f-4e96-8832-da071805a97d.html

Comments