文章目录
- 1. 复现错误
- 2. 分析错误
- 3. 解决错误
- 4. 解决该错误的其他方法
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.
即! [rejected] master -> master (fetch first)
。
2. 分析错误
! [rejected] master -> master (fetch first)
翻译成中文就是![已拒绝] mater -> master(先获取)
。
换句话说,我们想把自己本地的某个项目,关联到远程仓库并推送上去,但为什么报这个错误呢?
原来,我们在创建仓库时,都会勾选使用Reamdme文件初始化这个仓库
这个操作,初始了一个README
文件,并配置添加了忽略文件:
当点击创建仓库时,它会帮我们做一次初始提交。
于是,我们的仓库就有了README.m
和.gitignore
文件。
接着,我们把本地项目关联到这个仓库,并把项目推送到仓库时。
我们在关联本地与远程时,两端都是有内容的,但这两份内容并没有联系。
当我们推送到远程或者从远程拉取内容时,都会存在没有被跟踪的内容。
于是,你看git
报的详细错误中,总是会让你先拉取再推送,如下图所示:
3. 解决错误
既然需要先拉取,再推送,便可以使用如下解决方法。
- 首先,使用
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.
- 接着,使用
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文件初始化这个仓库
,如下图所示:
然后,克隆下来使用,下次要推送,即可直接推送。
如果这两种方法都无法解决你的错误,烦请在评论区留言。