Git 报错 Updates were rejected because the remote contains work that you do

Table of contents

Git 报错 Updates were rejected because the remote contains work that you do

1. This happens on the command line

2. The same error occurs in idea, and the solution is the same as above


Git 报错 Updates were rejected because the remote contains work that you do

This error report is really unbearable to me. Every time whether it is a 'command line' or an idea submission, there will be such a mentally explosive problem. However, every time there is a repeated search for a solution, this time I really can't stand it, so I have this article, I hope it can also help you with an explosive mentality.

1. This happens on the command line

Such problems occur in command line execution because of the wrong submission process:

git init //初始化仓库
git add .(文件name)                  //添加文件到本地暂存
git commit -m “first commit”        //添加文件描述信息
git remote add origin    远程仓库地址 //链接远程仓库
git push -u origin master          //把本地仓库的文件推送到远程仓master                                      分支

In this way, the error message prompted by the title appears, as shown in the figure below:image-20210604105941769

After the same problem occurred many times, I already know the cause of this problem. This is because the content of the remote warehouse is inconsistent with the content of the remote warehouse after the local new library is created (the remote warehouse has some content that is not available locally). The problem is well known, but the solution is misremembered every time, so let’s record it.

The correct submission process is as follows:

git init                           //初始化仓库
git add .(文件name)                //添加文件到本地 
git commit -m “first commit”      //添加文件描述信息
git remote add origin  远程仓库地址 //链接远程仓库 
git pull origin master           // 把本地仓库的变化连接到远程仓库master                                     分支
git push -u origin master        //把本地仓库的文件推送到远程仓库master                                    分支

 

When performing the fifth step above, a new error may appear, which is also a common error. The error message is as follows:image-20210604110514047

Don't panic, this is because the file version has not been updated in time. The two branches are two different versions with different submission histories. The decisive way is to add a command after the original command:

git pull origin master --allow-unrelated-histories

Then in some cases, entering the above command can solve the problem, and in some cases, he will report a new error, as follows:

image-20210604110855717 

It is really troublesome to check the solution on the Internet, but the reason is known, that is, there is a file conflict and there is no update, then it is easy to handle, re-enter the following command to solve the problem:

 image-20210604111243288

2. The same error occurs in idea, and the solution is the same as above

The place to enter the command is below the idea, there is a terminal, after clicking, you can enter the above command.

Let me say another kind of my error: 

The background is that now I want to migrate the code from one code base to another code base. The team leader gave me a new address and asked me to push the code, and it is also because the remote master and the local master do not have a common commit history (to put it bluntly, there is no Common ancestors, not brothers, hahaha), after executing the following command, you can submit and push it, which roughly means that you don’t care whether there is a common root...

git pull origin master --allow-unrelated-histories

 

 Excerpt:  Git error Updates were rejected because the remote contains work that you do_very cute new blog-CSDN blog

Guess you like

Origin blog.csdn.net/qq_39706515/article/details/131212637
Recommended