全网多种方法解决error: failed to push some refs to ‘xxx‘

迷南。 2023-09-26 21:21 90阅读 0赞

文章目录

    1. 复现错误
    1. 分析错误
    1. 解决错误
    1. 解决该错误的其他方法

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.

在这里插入图片描述

failed to push some refs to 'https:xxx/test.git'

2. 分析错误

failed to push some refs to 'https:xxx/test.git'翻译成中文就是未能将某些引用推送到“https:xxx/test.git”

换句话说,我们想把自己本地的某个项目,关联到远程仓库并推送上去,但为什么报这个错误呢?

原来,我们在创建仓库时,都会勾选使用Reamdme文件初始化这个仓库这个操作,初始了一个README文件,并配置添加了忽略文件:
在这里插入图片描述

当点击创建仓库时,它会帮我们做一次初始提交。

于是,我们的仓库就有了README.m.gitignore文件。

接着,我们把本地项目关联到这个仓库,并把项目推送到仓库时。

我们在关联本地与远程时,两端都是有内容的,但这两份内容并没有联系。

当我们推送到远程或者从远程拉取内容时,都会存在没有被跟踪的内容。

于是,你看git报的详细错误中,总是会让你先拉取再推送,如下图所示:

在这里插入图片描述

3. 解决错误

既然需要先拉取,再推送,便可以使用如下解决方法。

  1. 首先,使用git pull --rebase origin master命令拉取,如下代码所示:

    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.
  2. 接着,使用git push -u origin master命令上传代码,如下代码所示:

    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’.

如此,便可以推送成功。

如果这种解决方法无法解决你的错误,可以参考如下解决方法。

4. 解决该错误的其他方法

想要避免这种问题,就要保持创建的仓库是一个空仓库,什么都没有。

也就是在创建仓库时,不要勾选使用Readme文件初始化这个仓库,如下图所示:

在这里插入图片描述

然后,克隆下来使用,下次要推送,即可直接推送。

如果这两种方法都无法解决你的错误,烦请在评论区留言。

发表评论

表情:
评论列表 (有 0 条评论,90人围观)

还没有评论,来说两句吧...

相关阅读