There are multiple methods on the whole network to solve the error that Updates were rejected because the remote contains work that you do not have locally

1. Reproduce the error


Today I used to git statusview the file status and found that there is a file that has not been submitted, as shown in the following code:

D:\project\test>git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        src/main/java/xxx/po/test.java

nothing added to commit but untracked files present (use "git add" to track)

Since it has not been submitted, first use git addthe code modified in the current directory to add it from the workspace to the temporary storage area, as shown in the following code:

D:\project\test>git add  src/main/java/xxx/po/test.java

Then use git committhe cache to add the content to the local warehouse, as shown in the following code:

D:\project\test>git commit -m "test"
[master 0b983e7] test
 1 file changed, 9 insertions(+)
 create mode 100644 src/main/test/po/test.java

But git push origin masterwhen pushing the local version library to the remote server, the following error is reported:

D:\project\test>git push
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
To https:xxx/test.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https:xxx/test.git'
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.

insert image description here

ie Updates were rejected because the remote contains work that you do not have locally.

2. Analysis errors


Updates were rejected because the remote contains work that you do not have locallyIt is translated into Chinese 更新被拒绝,因为远程包含您本地没有的工作.

In other words, we want to associate a local project with the remote warehouse and push it up, but why is this error reported?

It turns out that when we create a warehouse, we will check 使用Reamdme文件初始化这个仓库this operation, initialize a READMEfile, and configure to add an ignore file:
insert image description here

When clicking create repository, it will do an initial commit for us.

So, our warehouse has README.mand .gitignorefiles.

Next, we associate the local project with this warehouse and push the project to the warehouse.

When we associate local and remote, both ends have content, but the two contents are not connected.

When we push to or pull content from remote, there will be content that is not tracked.

Therefore, gitin the detailed errors you read in the newspaper, you will always be asked to pull first and then push, as shown in the following figure:

insert image description here

3. Fix bugs


Since you need to pull first and then push, you can use the following solution.

  1. First, use git pull --rebase origin masterthe command to pull, as shown in the following code:
D:\project\test>git pull --rebase origin master

warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
remote: Counting objects: 33, done.
remote: Compressing objects: 100% (25/25), done.
remote: Total 33 (delta 5), reused 0 (delta 0)
Unpacking objects: 100% (33/33), 23.26 KiB | 58.00 KiB/s, done.
From https:xxx/test
 * branch            master     -> FETCH_HEAD
   453fc37..97defce  master     -> origin/master
Successfully rebased and updated refs/heads/master.


  1. Next, use git push -u origin masterthe command to upload the code, as shown in the following code:
D:\project\test>git push -u origin master

warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
Enumerating objects: 22, done.
Counting objects: 100% (22/22), done.
Delta compression using up to 12 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (12/12), 898 bytes | 898.00 KiB/s, done.
Total 12 (delta 3), reused 0 (delta 0), pack-reused 0
To https:xxx/test.git
   97defce..c60a6e6  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

In this way, the push can be successful.

If this solution cannot solve your error, you can refer to the following solutions.

4. Other ways to resolve this error


To avoid this problem, keep the created warehouse as an empty warehouse with nothing.

That is, when creating a warehouse, do not check it 使用Readme文件初始化这个仓库, as shown in the following figure:

insert image description here

Then, clone it and use it. Next time you want to push it, you can push it directly.

If neither of these two methods can solve your error, please leave a message in the comment area.

Guess you like

Origin blog.csdn.net/lvoelife/article/details/130249786