[Git] push branch error error: failed to push some refs to...

Article directory

error message

  • Sample code:
➜ git:(test) git push origin test
 ! [rejected]          test -> test (fetch first)
hint: Updates were rejected because the remote contains work that you do
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.
  • translate:
!(拒绝 ]           测试→首先测试(获取)
提示:更新被拒绝,因为远程包含您所做的工作
提示:没有本地。这通常是由另一个存储库推送引起的
提示:相同的ref。你可能想要首先集成远程更改
提示:(例如,'git pull…')然后再推。
提示:请参阅'git push—help'中的'Note about fast-forward '获取详细信息。

Solution

Analysis: This is because you did not pull the latest code before pushing the branch, resulting in inconsistent versions.
Solution: Roll back to the code before the merge, pull the latest code, and then push the branch

Note: This solution is applicable to the merge operation between two branches. For example, if the test branch rolls back the version, the merged code on the test will be lost. After your test branch can be pulled successfully, you need to merge the code on the development branch again Merge into test. So remember to keep this version of the code and then roll back to the previous version, wait for the pull to succeed, and then re-merge the code

  • View the history version of the last 2 commits
➜  git:(test) git log -2			# 查看最近2次提交的历史版本
commit da20b931f4e04a61f0f9b4e4726a2e907e566fc6
Merge: 33df706e 6018c237
Author: 流星
Date:   Wed Jan 19 09:58:40 2022 +0800

    第二版

commit 6018c237278f5265e78314049d6642e493ebdb56
Author: 流星
Date:   Wed Jan 19 09:57:50 2022 +0800

    第一版

  • rollback to previous version
➜  git:(test) git reset --hard 6018c237278f5265e78314049d6642e493ebdb56
  • Then pull to update the branch
➜  git:(test) git pull origin test
  • re-merge current branch
➜  git:(test) git merge dev
  • Finally push up
➜  git:(test) git push origin test

Guess you like

Origin blog.csdn.net/qq_45677671/article/details/122574511