本文是在使用了上一篇 Git-tool-git-repo-clean 提供的工具之后可能会遇到同步远程仓库问题,即远程仓库更新了,如何更新本地关联仓库的一种解决方案。
背景 假设远程服务器端存在一个仓库 repo-server, 开发者 A 和 B 分别克隆了 repo-a, repo-b 进行分布式协同开发。
开发者 A 在本地 repo_a 中提交了一些 commit,但是提交中不小心包含了比较大的非代码文件,这导致了仓库超出了最大容量限额,于是推送失败。于是他选择使用 git-clean-repo 工具清理提交历史中的大文件。
清理完成后,按照提示运行 git push origin --all --force 命令顺利推送到远程仓库。
问题 开发者 B 本地也进行了一些提交,但是此时如果想推送到远程,肯定会失败的,因为远程的合并基准以及发生改变:
1 2 3 4 5 6 7 8 9 10 $ git push origin master Enter passphrase for key '/home/git/.ssh/id_ed25519' : To gitee.com:cactusinhand/my-repo.git ! [rejected] master -> master (fetch first) error: failed to push some refs to 'gitee.com:cactusinhand/my-repo.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...' ) before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
解决方法 git pull --rebase = true
1 2 3 4 5 6 7 8 9 10 $ git pull --rebase=true Enter passphrase for key '/home/git/.ssh/id_ed25519' : remote: Enumerating objects: 8, done . remote: Counting objects: 100% (8/8), done . remote: Total 15 (delta 8), reused 8 (delta 8), pack-reused 7 Unpacking objects: 100% (15/15), 2.21 KiB | 188.00 KiB/s, done . From gitee.com:cactusinhand/my-repo + f4a81e18...b83e4cb8 master -> origin/master (forced update) d485b7a5..baa5db10 test -> origin/test (forced update) Successfully rebased and updated refs/heads/master.
这个命令会把远程所有的新增的数据 fetch 下来,并进行 rebase 操作。结果看起来就是 repo_b 的提交线性地在 repo_a 的提交(远程仓库的最新提交)之后,成功之后开发者 B 就可以正常推送他的提交了。
1 2 3 4 5 6 $ git status On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) nothing to commit, working tree clean
为了展示更为复杂一点的情况,上面的过程表明远程有两个分支 master, test 进行了更新。
因为当前分支(即 master 分支)已经 rebase 更新成功,这个时候需要切换到 test 分支继续 rebase 操作:
1 2 3 4 5 6 7 8 9 10 $ git checkout test $ git pull --rebase=true Enter passphrase for key '/home/git/.ssh/id_ed25519' : Updating d485b7a58..baa5db108 Fast-forward files/4004.c | 0 files/4005.c | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 files/4004.c create mode 100644 files/4005.c
操作成功之后,本地仓库 repo_b 的 test 分支就与远程的 test 分支也保持一致了:
1 2 3 4 5 $ git status On branch test Your branch is up to date with 'origin/test' . nothing to commit, working tree clean
rebase 冲突处理
如果这个过程出现合并冲突,则需要先解决冲突。解决的原则就是以远程的数据为主,我们需要丢弃掉本地不需要的文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 如果暂存区存在待提交但没有提交的文件,需要丢掉: $ git restore --staged files $ git restore files $ rm files $ git rebase --continue $ git status On branch test Your branch is up to date with 'origin/test' . nothing to commit, working tree clean
所有工作完成之后,本地仓库 repo_b 就可以向远程 repo-server 正常推送了。