Git/清理仓库

来自康健生活
< Git

清理仓库

1. 切换到新的分支

git checkout --orphan latest_branch

2. 缓存所有文件(除了.gitignore中声明排除的)

git add -A

3. 提交跟踪过的文件(Commit the changes)

git commit -am "commit message"

4. 删除master分支(Delete the branch)

git branch -D master

5. 重命名当前分支为master(Rename the current branch to master)

git branch -m master

6. 提交到远程master分支 (Finally, force update your repository)

git push -f origin master

但实际(6.)的提交过程中出现错误:

Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 12 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (8/8), 1.05 KiB | 1.05 MiB/s, done.
Total 8 (delta 0), reused 3 (delta 0)
remote: GitLab: You are not allowed to force push code to a protected branch on this project.
To ssh://192.168.50.3:30001/root/tdren.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'ssh://git@192.168.50.3:30001/root/tdren.git'

提交失败(两个不相干的 git 库,一个是本地库,一个是远端库,本地推送到远端,远端告知无法合并),使用两个方法挽救:

  1. 从远端库拉下来代码,本地要加入的代码放到远端库下载到本地的库,再提交上去。
  2. 强制提交,不相干的分支进行强行合并。
git pull origin master --allow-unrelated-histories
git push --set-upstream origin master