删commit记录

记录一次收拾“把超大库放进仓库”的烂摊子

起因

git是放代码的地方,但是我很蠢地把Unity的库给放进去,而且每次提交把全部有用没用的更改全交了。这就是不好好看新手教程的下场啊。
首先,要是直接在github里删除的话,历史记录还在那儿,.git文件依然会特别大。
删了一个library文件夹,.git文件大小: 1.71GB -> 633 MB 。而library自己有多大呢?2.04 GB。。。
于是搜查资料,查到了,按教程做,结果————

1
2
3
4
5
6
7
8
9
10
 git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch Library' --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...

Cannot rewrite branches: You have unstaged changes.

嗯。。。但是我没保存的那些没用的东西,真是又多又没用。

解决方式

那就按这他给的链接搜索了。

在保证什么什么环境变量没问题的情况下。

1
pip install git-filter-repo

接下来,cd或者进入到文件夹,右键git bash。
这里教程也写了备份,但是我胆子大,没备份。不能这样。

1
cp -r your_repo your_repo_backup

然后就可以愉快地删除了。

1
git filter-repo --force --invert-paths --path 'Library'

清理并优化本地仓库

1
2
git reflog expire --all --expire-unreachable=now --update-reflogs
git gc --prune=now --aggressive

推送更改到远程仓库

1
2
3
4
5
6
git remote add origin <仓库URL>
或者
git remote set-url origin <仓库URL>

git push origin --force --all
git push origin --force --tags

重新clone,好好设置gitignore啊喂!终于理解去年为什么学长禁止我把美术资源的工程文件也push上了,唉。。。

于是去copy了学长的gitignore文件,以后就不会这么逆天了。仅针对unity引擎。

Unity gitignore模板
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# This .gitignore file should be placed at the root of your Unity project directory
#
# Get latest from https://github.com/github/gitignore/blob/main/Unity.gitignore
#
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Uu]ser[Ss]ettings/

# MemoryCaptures can get excessive in size.
# They also could contain extremely sensitive data
/[Mm]emoryCaptures/

# Recordings can get excessive in size
/[Rr]ecordings/

# Uncomment this line if you wish to ignore the asset store tools plugin
# /[Aa]ssets/AssetStoreTools*

# Autogenerated Jetbrains Rider plugin
/[Aa]ssets/Plugins/Editor/JetBrains*

# Visual Studio cache directory
.vs/

# Gradle cache directory
.gradle/

# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd
*.pdb
*.mdb
*.opendb
*.VC.db

# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
*.mdb.meta

# Unity3D generated file on crash reports
sysinfo.txt

# Builds
*.apk
*.aab
*.unitypackage
*.app
# Crashlytics generated file
crashlytics-build.properties

# Packed Addressables
/[Aa]ssets/[Aa]ddressable[Aa]ssets[Dd]ata/*/*.bin*

# Temporary auto-generated Android Assets
/[Aa]ssets/[Ss]treamingAssets/aa.meta
/[Aa]ssets/[Ss]treamingAssets/aa/*

小剧场

大佬:不过这 直接把library放进去了有点离谱
小w:草率了QAQ
大佬:其实有一个比较稳妥的办法,就是新创一个分支,没问题了再合并。这样做还可以减少多余的commit,就是比较麻烦。
小w:我多余的commit估计有几千个文件改动。。。毕竟那可是把library装进去了
大佬:哈哈

参考链接:

github上的安装说明
csdn的一篇博客
知乎教怎么用gitignore的