多种方法解决This is usually caused by another repository pushing to the same ref的错误

1. 复现错误


今天使用git status查看文件状态,发现有一个文件未提交,如下代码所示:

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)

既然未提交,则首先使用git add将当前目录下修改的代码,从工作区添加到暂存区,如下代码所示:

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

接着使用git commit将缓存区内容添加到本地仓库,如下代码所示:

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

但使用git push origin master将本地版本库推送到远程服务器时,却报出如下错误:

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.

在这里插入图片描述

This is usually caused by another repository pushing to the same ref.

2. 分析错误


This is usually caused by another repository pushing to the same ref翻译成中文就是这通常是由另一个存储库推送到同一引用引起的

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

原来,我们在创建仓库时,都会勾选使用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.


  1. 接着,使用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文件初始化这个仓库,如下图所示:

在这里插入图片描述

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

互联网全栈开发实战

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值