解決辦法:error: failed to push some refs to 'https://github.com/xxxx.git'

在github遠程創建倉庫後, 利用gitbash進行提交本地文件的時候出現如下錯誤:

[root@foundation38 demo]# git push -u origin master
Username for 'https://github.com': xuefeilong
Password for 'https://[email protected]': 
To https://github.com/xuefeilong/test.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/xuefeilong/test.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

在這裏插入圖片描述
解決辦法:

1: 進行push前先將遠程倉庫pull到本地倉庫
$ git pull origin master    #git pull --rebase origin master
$ git push -u origin master

2: 強制push本地倉庫到遠程 (這種情況不會進行merge, 強制push後遠程文件可能會丟失 不建議使用此方法)
$ git push -u origin master -f

3: 避開解決衝突, 將本地文件暫時提交到遠程新建的分支中
$ git branch [name]
# 創建完branch後, 再進行push
$ git push -u origin [name] 

我使用的是直接加入參數-f,但是不推薦比較粗暴:

[root@foundation38 demo]# git push -u origin master -f
Username for 'https://github.com': xuefeilong
Password for 'https://[email protected]': 
Counting objects: 13, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (13/13), 975 bytes | 0 bytes/s, done.
Total 13 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/xuefeilong/test.git
 + 5c057df...abfded5 master -> master (forced update)
Branch master set up to track remote branch master from origin.

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章