今天在使用git推送本地仓库到远程时发生报错,特此记录。
错误过程
使用命令
git push -u origin master
推送本地仓库到远程仓库时发生如下报错:
To gitee.com:mobius8086/algorithm-learning.git
![rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'gitee.com:mobius8086/algorithm-learning.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards'in'git push --help'for details.
问题排查及解决
1.查看本地仓库和远程仓库是否建立联系
git remote -v
返回如下信息,本地和远程仓库联系正常
$ git remote -v
origin [email protected]:mobius8086/algorithm-learning.git (fetch)
origin [email protected]:mobius8086/algorithm-learning.git (push)
2.第一次推送的时候需要加上
-u
参数
git push -u origin master
-u
参数的作用是将本地仓库分支和远程仓库分支建立联系,origin是远程仓库默认名字,master是本地仓库默认名字,仅仅知道仓库名字不足够,因为一个仓库下面可能有多个分支,所以使用
-u
参数指定默认分支
返回如下信息:
$ git push -u origin "master"
To gitee.com:mobius8086/algorithm-learning.git
![rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'gitee.com:mobius8086/algorithm-learning.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
问题依旧。
3.本地仓库和远程仓库第一次同步,也就是没有pull过程
执行如下命令,将本地仓库和远程仓库同步
git git pull origin master
返回信息:
$ git pull origin master
From gitee.com:mobius8086/algorithm-learning
* branch master -> FETCH_HEAD
fatal: refusing to merge unrelated histories
依旧报错,refusing to merge unrelated histories(拒绝合并不相关的历史)
那么就需要考虑使用一种强制性的手段将本地和远程仓库合并了
4.使用rebase命令将本地和远程仓库同步
git pull --rebase origin master
返回信息
$ git pull --rebase origin master
From gitee.com:mobius8086/algorithm-learning
* branch master -> FETCH_HEAD
Successfully rebased and updated refs/heads/master.
看到了successfully,打开本地仓库查看变化,可以看到远程的内容被合并到了本地,所以接下来可以进行仓库推送了
git push --set-upstream origin master
返回信息:
$ git push --set-upstream origin master
Enumerating objects: 12, done.
Counting objects: 100% (12/12), done.
Delta compression using up to 12 threads
Compressing objects: 100% (10/10), done.
Writing objects: 100% (11/11), 392.84 KiB |7.70 MiB/s, done.
Total 11(delta 0), reused 0(delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:mobius8086/algorithm-learning.git
e7c3b04..3ed5762 master -> master
branch 'master'set up to track 'origin/master'.
执行成功!
版权归原作者 Mobius8086 所有, 如有侵权,请联系我们删除。