Updates were rejected
- 1. 雜話
- 2. 問題
- 3. 解決
- 3.1 拉去遠程的最新版本(A+C)
- 3.2 解決可能的沖突
- 3.3 提交
- 3.4 再次推送
1. 雜話
?大伙兒應該都用過Git吧,具體是個啥東西我就不說了哈。之前我在用git push的時候遇到了這個報錯,我仔細思考了一下,這個問題如果不出意外的話,那么應該就是出現了沖突。那咱們來說說這個問題應該怎么解決。
2. 問題
?這通常發生在git push指令的后面,我舉個例子哈。比如說原本你的遠程倉庫是版本A,比如說你在本地的版本做了修改B,但是同時呢,你又在GitHub的頁面上做了修改C。
?那么現在呢,你的本地版本是A+B,遠程倉庫的版本是A+C。那么這個時候你想把A+B推送過去,你以為是在覆蓋A(也就是只需要加上B就行),其實是在覆蓋A+C,所以就會有沖突。
?咱們來看一下典型的報錯:
error: failed to push some refs to 'https://gitee.com/xxxxxxxx'
To https://gitee.com/xxxxxxxx
! refs/heads/master:refs/heads/master [rejected] (fetch first)
hint: Updates were rejected because the remote contains work that you do
Done
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.
?說白了就是遠程倉庫已經包含了你沒有的本地更改,因此Git拒絕了你的推送請求。
3. 解決
3.1 拉去遠程的最新版本(A+C)
git pull origin <你的分支名,一般是master或者main>
3.2 解決可能的沖突
?如果拉取操作導致了沖突,就需要打開有沖突的文件(也就是B和C更改中都有但是不一樣的文件),手動解決沖突并保存文件。Git會在有沖突的文件中標記出沖突的部分:
?選擇要保留的更改,然后刪除沖突標記(<<<<<<< HEAD、=======、>>>>>>> remote/branch)。完成后保存文件。
3.3 提交
git add .
git commit -m "Update Again"
3.4 再次推送
git push origin <你的分支名,一般是master或者main>