【错误】git push : Updates were rejected because the tip of your current branch is behind

1 question

        ​ ​At first, I wanted to push the local project to an empty warehouse on the server. However, since I had operated the empty warehouse before and had committed, some problems occurred when I pushed it.

        I created a remote warehouse, then created and deleted files in the remote warehouse, and committed several times. The last commit was a delete operation, and it ended up being an empty warehouse. Then I completed the project in the local warehouse, added the project file, committed it, and then pushed it, the following error occurred:

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

2 reasons

        This situation usually means that there is a conflict. The latest status of the remote warehouse is not synchronized to the local. This will happen when the local is pushed.

        My remote warehouse has several submissions, and there is only one local submission. At this time, the local and remote branches are two parallel branches without any intersection (no separation, no merge), so I need to update the several submissions in the remote warehouse to local.

3 solutions

        The usual solution is to pull it down first and then push it, or force a push, as follows:

#1、
git pull <远程仓库名> <远程分支名>:<本地分支名>	#拉取
git push <远程仓库名> <本地分支名>:<远程分支名>	#推送
#2、
git push <远程仓库名> <本地分支名>:<远程分支名> -f	#强行推送(不推荐)(该方法会将远程仓库强行恢复为本地未修改版本,并基于提交进行修改,覆盖远程仓库最新内容)

        However, after testing here, I found that the two methods still reported the same error. I pull first and then push. Even though the branch diagram shows that the commits from the remote warehouse have been pulled down and the local commits are the latest, an error still occurs. Forced push still reports an error.

        So I thought, I might as well just pull down the remote repository, go to add, commit my local content, and then push. But I'm lazy and don't want to delete the local warehouse and start it again, so I use reset to go back. I first print the branch diagram, and then copy the ID of the previous submission of my latest local submission, which is the last submission in the remote warehouse. After that submission, the remote warehouse is empty. I use this ID to go back to that submission before proceeding. add, commit, push. as follows:

git log --graph --oneline --decorate --all	#在命令行查看分支图
git reset <提交id>	#混合回退,保留本地文件
git add .	#添加到暂存区
git commit -m "<提交备注>"	#提交到本地仓库
git push <远程仓库名> <本地分支名>:<远程分支名>	#推送到远程仓库

Then the push was successful!

Guess you like

Origin blog.csdn.net/Davidorzs/article/details/134348196
Recommended