git push 报错的解决方案

<1>git 遇到的困难

一、使用git在本地创建一个项目的过程
    首先得在github或者git@OSC上创建一个hello-world项目。本地才能进行推送
    $ makdir ~/hello-world    //创建一个项目hello-world
    $ cd ~/hello-world       //打开这个项目
    $ git init             //初始化 
    $ touch README
    $ git add README        //更新README文件
    $ git commit -m 'first commit'     //提交更新,并注释信息“first commit” 
    $ git remote add origin git@github.com:defnngj/hello-world.git     //连接远程github项目  当然 origin还可以 origin_new
    $ git push -u origin master     //将本地项目更新到github项目上去   

    执行:git push -u origin master -f 以及 git push origin master  
二、其他常用命令:
git remote -v  //查看地址
git config --list  //查看配置
git status  //查看状态
git remote rm origin  //删除origin这个push地址
rm -rf  ~/.ssh  删除.ssh 文件和文件夹
rm -f ~/.ssh 删除文件
    
三、git push遇到的问题:
本来想用 ssh传输的,结果用ssh传输总是报错:

Administrator@PC201611290918 MINGW64 /d/Git@OSC/SpringMVC (master)
$ git remote add origin git@git.oschina.net:MaGary/SpringMVC.git

Administrator@PC201611290918 MINGW64 /d/Git@OSC/SpringMVC (master)
$ git push -u origin master
Access denied.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Administrator@PC201611290918 MINGW64 /d/Git@OSC/SpringMVC (master)
$ git pull
Access denied.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights

先在gitHUb或则Git@OSC上创建项目。后来我改用 https地址提交项目:
https://git.oschina.net/MaGary/SpringMVC.git/

重新设置remote地址: git remote add origin https://git.oschina.net/MaGary/SpringMVC.git

接着进行 push操作:结果报错:(git push -u origin master 以及 git push  origin master)度有错:都是:![rejected]  master -> master(fetch first);  ![rejected]  master -> master(non-fast-forward); 

Administrator@PC201611290918 MINGW64 /d/Git@OSC/SpringMVC (master)
$ git push -u origin master
Username for 'https://git.oschina.net': fshengbing0327@163.com
To https://git.oschina.net/MaGary/SpringMVC.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://git.oschina.net/MaGary/SpringMVC.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.

Administrator@PC201611290918 MINGW64 /d/Git@OSC/SpringMVC (master)
$ git push origin master
Username for 'https://git.oschina.net': fshengbing0327@163.com
To https://git.oschina.net/MaGary/SpringMVC.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://git.oschina.net/MaGary/SpringMVC.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 push -u origin master -f 以及 git push origin master

Administrator@PC201611290918 MINGW64 /d/Git@OSC/SpringMVC (master)
$ git push -u origin master -f
Username for 'https://git.oschina.net': fshengbing0327@163.com
Counting objects: 253, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (204/204), done.
Writing objects: 100% (253/253), 7.63 MiB | 1.43 MiB/s, done.
Total 253 (delta 74), reused 0 (delta 0)
remote: Resolving deltas: 100% (74/74), done.
To https://git.oschina.net/MaGary/SpringMVC.git
 + 8cdf4d4...d7e93f6 master -> master (forced update)
Branch master set up to track remote branch master from origin.

Administrator@PC201611290918 MINGW64 /d/Git@OSC/SpringMVC (master)
$ git push origin master
Username for 'https://git.oschina.net': fshengbing0327@163.com
Everything up-to-date

 

<2>git push 的错误failed to push some refs to

当我们在github版本库中发现一个问题后,你在github上对它进行了在线的修改;或者你直接在github上的某个库中添加readme文件或者其他什么文件,但是没有对本地库进行同步。这个时候当你再次有commit想要从本地库提交到远程的github库中时就会出现push失败的问题。

如下图所示 
我在github库中对某个文件进行了在线的编辑,并且没有同步到本地库,之后我在本地库添加了文件test.txt,并想提交到github,出现以下错误:error:failed to push some refs to。 
gitpush失败

解决方案

这个问题是因为远程库与本地库不一致造成的,那么我们把远程库同步到本地库就可以了。 
使用指令

git pull --rebase origin master
  • 1

这条指令的意思是把远程库中的更新合并到本地库中,–rebase的作用是取消掉本地库中刚刚的commit,并把他们接到更新后的版本库之中。

如图: 
gitpush

下面我用图形象的解释下错误情况的发生和解决

gitpush失败2 
git pull –rebase origin master意为先取消commit记录,并且把它们临时 保存为补丁(patch)(这些补丁放到”.git/rebase”目录中),之后同步远程库到本地,最后合并补丁到本地库之中。 
gitpush失败3 
接下来就可以把本地库push到远程库当中了。 
gitpush失败4

 

 

 

  • 7
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 当执行git push命令时,可能会遇到不同的报错。其中,一种报错是“fatal: remote error: You can’t push to 目标地址 Use 目标地址”\[1\],这通常是由于目标地址不正确导致的。另一种报错是“fatal: unable to access 目标地址: Failed connect to github.com:443; Connection timed out”\[2\],这可能是由于网络连接超时导致的。还有一种报错是“error: failed to push some refs to 'git@xxx.xx.git’”\[3\],这通常是由于多人开发时出现代码冲突导致的。解决这些问题的方法包括检查目标地址是否正确、检查网络连接是否正常以及解决代码冲突等。 #### 引用[.reference_title] - *1* *2* [【经验分享】git push出现的三种报错解决方案](https://blog.csdn.net/Sunnyside_/article/details/118358214)[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^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [git push 报错failed to push some refs to‘xxx](https://blog.csdn.net/weixin_45811256/article/details/125989719)[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^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值