Git本地仓库和远程仓库冲突解决
阅读原文时间:2022年01月12日阅读:7

场景描述:

在本地创建了一个git repo,并且执行了,git init命令,创建了.gitignore文件,或者README.md文件;

在远程创建了一个git repo,创建时也初始化了.gitignore文件,或者README.md文件;

有一天你在本地编写了一些代码,想把本地代码提交上去。你做了如下操作:

  • 绑定远程仓库

    git remote add origin ssh://127.0.0.1:29418/springcloud.git

  • 推送代码到远程仓库

    git push origin master

然后你发现不让上传并报了如下错误

! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'ssh://admin@127.0.0.1:29418/springcloud.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

提示你执行git pull,于是你执行了,但是好像没有什么用,并向你抛出了另一个异常

$ git pull gitblit master
From ssh://127.0.0.1:29418/springcloud
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

提示你绑定了一个不相关的历史版本,这个时候可以执行以下命令

git pull gitblit master --allow-unrelated-histories

终于可以了,O(∩_∩)O,但是还要处理冲突呢!

From ssh://127.0.0.1:29418/springcloud
 * branch            master     -> FETCH_HEAD
CONFLICT (add/add): Merge conflict in README.md
Auto-merging README.md
CONFLICT (add/add): Merge conflict in .gitignore
Auto-merging .gitignore
Automatic merge failed; fix conflicts and then commit the result.

注意此时git本地仓库的状态是master|MERGING,需要你处理冲突,当然,上面已经提示了在merge的过程中,你需要处理冲突,处理冲突后需要你再重新执行git add命令来再次添加一遍冲突文件,然后执行git commit命令,此处的git commit不需要指定文件,要不然会报错

$ git commit .gitignore
fatal: cannot do a partial commit during a merge.

提示你在合并过程中不能指定部分的文件,好了接下来就可以愉快的git push了。