IT评测·应用市场-qidao123.com

标题: 全网多种方法办理Updates were rejected because the remote contains work [打印本页]

作者: 梦见你的名字    时间: 2024-9-8 17:08
标题: 全网多种方法办理Updates were rejected because the remote contains work
1. 复现错误


本日利用git status查看文件状态,发现有一个文件未提交,如下代码所示:
  1. D:\project\test>git status
  2. On branch master
  3. Your branch is up to date with 'origin/master'.
  4. Untracked files:
  5.   (use "git add <file>..." to include in what will be committed)
  6.         src/main/java/xxx/po/test.java
  7. nothing added to commit but untracked files present (use "git add" to track)
复制代码
既然未提交,则首先利用git add将当前目录下修改的代码,从工作区添加到暂存区,如下代码所示:
  1. D:\project\test>git add  src/main/java/xxx/po/test.java
复制代码
接着利用git commit将缓存区内容添加到当地堆栈,如下代码所示:
  1. D:\project\test>git commit -m "test"
  2. [master 0b983e7] test
  3. 1 file changed, 9 insertions(+)
  4. create mode 100644 src/main/test/po/test.java
复制代码
但利用git push origin master将当地版本库推送到远程服务器时,却报出如下错误:
  1. D:\project\test>git push
  2. warning: ----------------- SECURITY WARNING ----------------
  3. warning: | TLS certificate verification has been disabled! |
  4. warning: ---------------------------------------------------
  5. warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
  6. warning: ----------------- SECURITY WARNING ----------------
  7. warning: | TLS certificate verification has been disabled! |
  8. warning: ---------------------------------------------------
  9. warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
  10. To https:xxx/test.git
  11. ! [rejected]        master -> master (fetch first)
  12. error: failed to push some refs to 'https:xxx/test.git'
  13. hint: Updates were rejected because the remote contains work that you do
  14. hint: not have locally. This is usually caused by another repository pushing
  15. hint: to the same ref. You may want to first integrate the remote changes
  16. hint: (e.g., 'git pull ...') before pushing again.
  17. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
复制代码

即Updates were rejected because the remote contains work that you do not have locally。
2. 分析错误


Updates were rejected because the remote contains work that you do not have locally翻译成中文就是更新被拒绝,因为远程包含您当地没有的工作。
换句话说,我们想把本身当地的某个项目,关联到远程堆栈并推送上去,但为什么报这个错误呢?
原来,我们在创建堆栈时,都会勾选利用Reamdme文件初始化这个堆栈这个操纵,初始了一个README文件,并配置添加了忽略文件:

当点击创建堆栈时,它会帮我们做一次初始提交。
于是,我们的堆栈就有了README.m和.gitignore文件。
接着,我们把当地项目关联到这个堆栈,并把项目推送到堆栈时。
我们在关联当地与远程时,两头都是有内容的,但这两份内容并没有联系。
当我们推送到远程大概从远程拉取内容时,都会存在没有被跟踪的内容。
于是,你看git报的详细错误中,总是会让你先拉取再推送,如下图所示:

3. 办理错误


既然必要先拉取,再推送,便可以利用如下办理方法。
  1. D:\project\test>git pull --rebase origin master
  2. warning: ----------------- SECURITY WARNING ----------------
  3. warning: | TLS certificate verification has been disabled! |
  4. warning: ---------------------------------------------------
  5. warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
  6. warning: ----------------- SECURITY WARNING ----------------
  7. warning: | TLS certificate verification has been disabled! |
  8. warning: ---------------------------------------------------
  9. warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
  10. remote: Counting objects: 33, done.
  11. remote: Compressing objects: 100% (25/25), done.
  12. remote: Total 33 (delta 5), reused 0 (delta 0)
  13. Unpacking objects: 100% (33/33), 23.26 KiB | 58.00 KiB/s, done.
  14. From https:xxx/test
  15. * branch            master     -> FETCH_HEAD
  16.    453fc37..97defce  master     -> origin/master
  17. Successfully rebased and updated refs/heads/master.
复制代码
  1. D:\project\test>git push -u origin master
  2. warning: ----------------- SECURITY WARNING ----------------
  3. warning: | TLS certificate verification has been disabled! |
  4. warning: ---------------------------------------------------
  5. warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
  6. warning: ----------------- SECURITY WARNING ----------------
  7. warning: | TLS certificate verification has been disabled! |
  8. warning: ---------------------------------------------------
  9. warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
  10. Enumerating objects: 22, done.
  11. Counting objects: 100% (22/22), done.
  12. Delta compression using up to 12 threads
  13. Compressing objects: 100% (8/8), done.
  14. Writing objects: 100% (12/12), 898 bytes | 898.00 KiB/s, done.
  15. Total 12 (delta 3), reused 0 (delta 0), pack-reused 0
  16. To https:xxx/test.git
  17.    97defce..c60a6e6  master -> master
  18. Branch 'master' set up to track remote branch 'master' from 'origin'.
复制代码
云云,便可以推送成功。
如果这种办理方法无法办理你的错误,可以参考如下办理方法。
4. 办理该错误的其他方法


想要制止这种题目,就要保持创建的堆栈是一个空堆栈,什么都没有。
也就是在创建堆栈时,不要勾选利用Readme文件初始化这个堆栈,如下图所示:

然后,克隆下来利用,下次要推送,即可直接推送。
如果这两种方法都无法办理你的错误,烦请在评论区留言。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4