1. 程式人生 > >Push rejected: Push to origin/master was rejected

Push rejected: Push to origin/master was rejected

背景:我是在學習android的時候,遇到的這個問題,我在androidstudio寫完程式碼,執行完demo後,利用git進行commit and push 的時候出現的這個錯誤
解決方法:你需要從VCS中的git中pull一下你github中初始化的空倉庫,但是你執行VCS->Git->pull也是會報錯的,錯誤如下:

Git Pull Failed: fatal: refusing to merge unrelated histories

意思是git拒絕合併兩個不相干的東西
此時你需要在開啟Git Bash,然後進入相應的目錄,然後敲git命令:

$ git pull origin master --
allow-unrelated-histories

出現類似於這種資訊就說明pull成功了:

From https://github.com/danishlyy/HappyBirthday
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 README.md | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 README.md

然後你可以利用git status檢視一下當前倉庫的狀態,是不是所有的全部add並且commit,如果全部完成,那麼此時你就可以將本地倉庫中的推送到github中,使用如下的git命令:

$ git push -u origin master

完了,去重新整理github,就可以看到東西已經推送成功
注意:
可能你在推送的過程中會遇到問題

fatal: AggregateException encountered.
   ▒▒▒▒һ▒▒▒▒▒▒▒▒▒▒
Username for 'https://github.com':
remote: Anonymous access to danishlyy/HappyBirthday.git denied.
fatal: Authentication failed for 'https://github.com/danishlyy/HappyBirthday.git/'

原因是因為你的配置檔案沒有使用者名稱和密碼的配置,所以你需要重新輸入一下,如果你覺得麻煩,可以直接在配置檔案中進行配置。