git push 报错的解决方案

系统管理员 2022-05-14 09:39 557阅读 0赞

<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项目上去

  1. 执行: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传输总是报错:

  1. Administrator@PC201611290918 MINGW64 /d/Git@OSC/SpringMVC (master)
  2. $ git remote add origin git@git.oschina.net:MaGary/SpringMVC.git
  3. Administrator@PC201611290918 MINGW64 /d/Git@OSC/SpringMVC (master)
  4. $ git push -u origin master
  5. Access denied.
  6. fatal: Could not read from remote repository.
  7. Please make sure you have the correct access rights
  8. and the repository exists.
  9. Administrator@PC201611290918 MINGW64 /d/Git@OSC/SpringMVC (master)
  10. $ git pull
  11. Access denied.
  12. fatal: Could not read from remote repository.
  13. 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);

  1. Administrator@PC201611290918 MINGW64 /d/Git@OSC/SpringMVC (master)
  2. $ git push -u origin master
  3. Username for 'https://git.oschina.net': fshengbing0327@163.com
  4. To https://git.oschina.net/MaGary/SpringMVC.git
  5. ! [rejected] master -> master (fetch first)
  6. error: failed to push some refs to 'https://git.oschina.net/MaGary/SpringMVC.git'
  7. hint: Updates were rejected because the remote contains work that you do
  8. hint: not have locally. This is usually caused by another repository pushing
  9. hint: to the same ref. You may want to first integrate the remote changes
  10. hint: (e.g., 'git pull ...') before pushing again.
  11. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
  12. Administrator@PC201611290918 MINGW64 /d/Git@OSC/SpringMVC (master)
  13. $ git push origin master
  14. Username for 'https://git.oschina.net': fshengbing0327@163.com
  15. To https://git.oschina.net/MaGary/SpringMVC.git
  16. ! [rejected] master -> master (non-fast-forward)
  17. error: failed to push some refs to 'https://git.oschina.net/MaGary/SpringMVC.git'
  18. hint: Updates were rejected because the tip of your current branch is behind
  19. hint: its remote counterpart. Integrate the remote changes (e.g.
  20. hint: 'git pull ...') before pushing again.
  21. hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决办法:

执行:git push -u origin master -f 以及 git push origin master

  1. Administrator@PC201611290918 MINGW64 /d/Git@OSC/SpringMVC (master)
  2. $ git push -u origin master -f
  3. Username for 'https://git.oschina.net': fshengbing0327@163.com
  4. Counting objects: 253, done.
  5. Delta compression using up to 4 threads.
  6. Compressing objects: 100% (204/204), done.
  7. Writing objects: 100% (253/253), 7.63 MiB | 1.43 MiB/s, done.
  8. Total 253 (delta 74), reused 0 (delta 0)
  9. remote: Resolving deltas: 100% (74/74), done.
  10. To https://git.oschina.net/MaGary/SpringMVC.git
  11. + 8cdf4d4...d7e93f6 master -> master (forced update)
  12. Branch master set up to track remote branch master from origin.
  13. Administrator@PC201611290918 MINGW64 /d/Git@OSC/SpringMVC (master)
  14. $ git push origin master
  15. Username for 'https://git.oschina.net': fshengbing0327@163.com
  16. 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失败

解决方案

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

  1. 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

发表评论

表情:
评论列表 (有 0 条评论,557人围观)

还没有评论,来说两句吧...

相关阅读