文章

总结:git 不常用命令

1
2
# 远程分支与本地分支有不相关的提交,合并远程分支
git pull origin main --allow-unrelated-histories
1
2
# 删除远程分之
git push origin --delete <branch_name>
1
2
# 更新远程分支列表
git remote update origin --prune
1
2
3
4
5
6
7
8
9
10
# 删除submodule

# Remove the submodule entry from .git/config
git submodule deinit -f .\3rd\xz-v5.8.1\

# Remove the submodule directory from the superproject's .git/modules directory
rm -rf .git/modules/3rd/xz-v5.8.1

# Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule
git rm -f 3rd/xz-v5.8.1

删除git记录中的大文件及blob

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 执行命令之前,保证仓库没有待提交的更改

# 清理
git gc

# 根据文件名查找大文件的完整路径
git rev-list --objects --all | grep <filename>

# 使用完整大文件路径删除大文件
git filter-branch --force --index-filter 'git rm -rf --cached --ignore-unmatch <full_path_filename>' --prune-empty --tag-name-filter cat -- --all

# 列出前5个大文件
# git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5 | awk '{print$1}')"

# 推送到远程仓库
git push origin --force --all

# 清除缓存
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now

# 查看当前Git对象统计信息
git count-objects -v

gitlab无法强行推送问题解决

需要owner设置如下:仓库的Settings -> Repository -> Protected branches 改成unprotected

本文由作者按照 CC BY 4.0 进行授权