使用git推送代码到远程gitee仓库报错及解决办法

今天在使用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  git@gitee.com:mobius8086/algorithm-learning.git (fetch)
origin  git@gitee.com: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'.

执行成功!

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
See the 'Note about fast-forwards' in 'git push --help' for details是在git push命令的帮助文档中提到的一条注意事项。[1] 这个提示是关于在使用git push命令时可能出现的一种错误情况,即推送失败,提示"non-fast-forward"。 这种情况通常是因为你当前分支的最新提交落后于远程分支的提交,需要先合并远程分支的更改(例如使用git pull命令),然后再次尝试推送。如果想要了解更多关于fast-forward的详细信息,可以参考git push的帮助文档中的相关说明。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【git push报错】:See the ‘Note about fast-forwards‘ in ‘git push --help‘ for details](https://blog.csdn.net/scorpio_j/article/details/114756383)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [git 无法push远程仓库 Note about fast-forwards 问题解决](https://blog.csdn.net/weixin_42596434/article/details/88759295)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值