目录
背景
目前通过Git进行版本控制的软件源代码托管服务平台主要有github、gitlab、gitee等,相对于其他平台使用gitlab的公司偏多。一般情况下leader不会给我们添加删除仓库的权限,所以大多时候想要清理一个仓库还是有些困难。正常情况下一个远程仓库会一直使用,不会涉及到清空仓库和清空评论的,除非:
- 远程仓库由于历史原因存在一些乱七八糟的文件;
- 远程仓库中由于某次失误将生产环境中的账号密码进行了上传;
- 远程仓库为测试仓库,需求不一样测试代码也不同,需要同步更新但是又不希望展示此前相关代码和提交记录;
新建测试仓库
测试仓库一共有三个分支分别是master,zhangsan_dev,common_dev,添加四个文件并提交五条commits为本文后续操作提供实验条件。
文章将介绍两种方式来清空远程仓库(remote repository)与历史提交记录(commits),不管采用哪种方式都建议操作之前删除master以外的其他分支。
解锁分支保护
需要特别注意的是:清空远程仓库和commit信息的过程中需要强推到master分支,需要暂时解除master分支保护,否则gitlab禁止push:
[w3sun@w3sundeMacBook-Pro ~] git push -f origin master Enumerating objects: 6, done. Counting objects: 100% (6/6), done. Delta compression using up to 8 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (6/6), 547 bytes | 547.00 KiB/s, done. Total 6 (delta 0), reused 0 (delta 0), pack-reused 0 remote: GitLab: You are not allowed to force push code to a protected branch on this project. To https://git.your_company_domain.com:9073/namespace/fast-dataset.git ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'https://git.your_company_domain.com:9073/namespace/fast-dataset.git'
清空远程仓库与提交记录
基于新分支清空远程仓库
[w3sun@w3sundeMacBook-Pro ~] git branch -a common_dev * master zhangsan_dev remotes/origin/common_dev remotes/origin/master remotes/origin/zhangsan_dev
1.新建分支
以master为基础创建一个名为target的新分支(新分支名字就定义为target而不是模糊举例),使用git checkout –orphan新建的分支是一个无历史提交记录的新分支。
[w3sun@w3sundeMacBook-Pro ~] git checkout --orphan target Switched to a new branch 'target' [w3sun@w3sundeMacBook-Pro ~] git log fatal: your current branch 'target' does not have any commits yet
2.添加文件
[w3sun@w3sundeMacBook-Pro ~] git add .
3.提交变更
[w3sun@w3sundeMacBook-Pro ~] git commit -m 'Delete old repository and commits' [target (root-commit) 4314afd] Delete old repository and commits 4 files changed, 4 insertions(+) create mode 100644 Copyright.md create mode 100644 License.md create mode 100644 README.md create mode 100644 pom.xml
4.删除master本地分支
[w3sun@w3sundeMacBook-Pro ~] git branch -D master Deleted branch master (was 6eeac7c).
5.重命名target为master分支
[w3sun@w3sundeMacBook-Pro ~] git branch -m master
6.强制更新远程仓库
[w3sun@w3sundeMacBook-Pro ~] git push -f origin master Enumerating objects: 6, done. Counting objects: 100% (6/6), done. Delta compression using up to 8 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (6/6), 547 bytes | 547.00 KiB/s, done. Total 6 (delta 0), reused 0 (delta 0), pack-reused 0 To https://git.your_company_domain.com:9073/namespace/fast-dataset.git + 6eeac7c...4314afd master -> master (forced update)
从WebUI上可以看到远程仓库已经被清空,commit同时变成了一条符合预期。
基于原分支清空远程仓库
1.删除所有文件
将远程仓库clone到本地后,需要删除本地仓库所有文件并将变更提交到远程仓库,这一步只会清空仓库并不会对提交记录产生影响。
[w3sun@w3sundeMacBook-Pro ~] ls Copyright.md License.md README.md pom.xml [w3sun@w3sundeMacBook-Pro ~] rm -fr *
2.推送变更记录
[w3sun@w3sundeMacBook-Pro ~] git add . [w3sun@w3sundeMacBook-Pro ~] git commit -m 'Delete all files from repository' [master bfea96d] Delete all files from repository 4 files changed, 4 deletions(-) delete mode 100644 Copyright.md delete mode 100644 License.md delete mode 100644 README.md delete mode 100644 pom.xml [w3sun@w3sundeMacBook-Pro ~] git push Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Delta compression using up to 8 threads Compressing objects: 100% (1/1), done. Writing objects: 100% (2/2), 207 bytes | 207.00 KiB/s, done. Total 2 (delta 0), reused 0 (delta 0), pack-reused 0 To https://git.your_company_domain.com:9073/namespace/fast-dataset.git 83818d3..bfea96d master -> master
3.初始化.git并新增文件
[w3sun@w3sundeMacBook-Pro ~] rm -fr .git [w3sun@w3sundeMacBook-Pro ~] git init hint: Using 'master' as the name for the initial branch. This default branch name hint: is subject to change. To configure the initial branch name to use in all hint: of your new repositories, which will suppress this warning, call: hint: hint: git config --global init.defaultBranch <name> hint: hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and hint: 'development'. The just-created branch can be renamed via this command: hint: hint: git branch -m <name> Initialized empty Git repository in /Users/w3sun/project/fast-dataset/.git/ [w3sun@w3sundeMacBook-Pro ~] vi README.md
4.提交变更清空记录
[w3sun@w3sundeMacBook-Pro ~] git add . [w3sun@w3sundeMacBook-Pro ~] git commit -m 'Delete remote repository commits' [master (root-commit) d2d7386] Delete remote repository commits 1 file changed, 1 insertion(+) create mode 100644 README.md [w3sun@w3sundeMacBook-Pro ~] git remote add origin https://git.your_company_domain.com:9073/namespace/fast-dataset.git [w3sun@w3sundeMacBook-Pro ~] git push -f --set-upstream origin master Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Writing objects: 100% (3/3), 238 bytes | 238.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 To https://git.your_company_domain.com:9073/namespace/fast-dataset.git + bfea96d...d2d7386 master -> master (forced update) Branch 'master' set up to track remote branch 'master' from 'origin'.
上述方式同样可以清空远程仓库和历史commit,效果和第一种是一样的,实际工作中可以根据实际情况决定采用哪种方式。
其他问题
1.There is no tracking information for the current branch.
[w3sun@w3sundeMacBook-Pro ~] git branch --set-upstream-to=origin/master master #本地关联远程分支即可
参考资料
转载请注明:雪后西塘 » Git清空远程仓库与提交记录