git push代码出现! [rejected] 解决方式

git push代码出现如下错误:

root@instance-myaj5rsw:~/web_v2.2# git push origin web_v2.2 
[email protected]'s password: 
To root@ip:/refs/web.git
 ! [rejected]        web_v2.2 -> web_v2.2 (non-fast-forward)
error: failed to push some refs to 'root@ip:/refs/web.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and 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.

git pull之后还是报一样的错

root@instance-myaj5rsw:~/web_v2.2# git pull origin web_v2.2
root@ip's password: 
From ip:/refs/web
 * branch            web_v2.2 -> FETCH_HEAD
Already up-to-date.

解决方式:

1. 强制覆盖,强行让本地分支覆盖远程分支,这个方式比较暴力,不建议使用,我使用过一次,导致代码丢失,而且也没解决问题,以后我估计不会再用这个方式。

git push -f

2、先把git的东西fetch到你本地然后merge后再push。(使用这个方式,我这个报错情况并没有解决)

$ git fetch
$ git merge

3. 先把本地的修改文件备份,然后checkout分支,branch切换分支,再git pull完提交,就OK了

root@instance-myaj5rsw:~/web_v2.2# git checkout web_v2.2
error: Your local changes to the following files would be overwritten by checkout:
	.idea/web.iml
Please, commit your changes or stash them before you can switch branches.
Aborting
root@instance-myaj5rsw:~/web_v2.2# git checkout .idea/web.iml

重新checkout、branch、pull分支

root@instance-myaj5rsw:~/web_v2.2# git checkout web_v2.2
root@instance-myaj5rsw:~/web_v2.2# git branch web_v2.2 
root@instance-myaj5rsw:~/web_v2.2# git pull origin web_v2.2 

再次把备份文件add、commit、push 就成功啦

 

参考链接:

https://blog.csdn.net/cuomer/article/details/81142159

 

你可能感兴趣的:(git)