我不太喜欢GIT,但遇到以下问题:我在GitHub上创建了一个新的存储库。然后,在项目文件夹中,执行以下命令:
1)我将初始化存储库设置到我的项目文件夹中:
git init
2)我建立了对我的GitHub存储库的引用:
git remote add origin https://github.com/AndreaNobili/SpringBoot-Excel-API.git
并获取参考信息,我得到了这一点:
developer@developer-virtual-machine:~/git/SOC-dashboard$ git remote -v
origin https://github.com/AndreaNobili/SpringBoot-Excel-API.git (fetch)
origin https://github.com/AndreaNobili/SpringBoot-Excel-API.git (push)
所以这似乎很好。
3)首先,我尝试推送项目代码:
developer@developer-virtual-machine:~/git/SOC-dashboard$ git push origin master
Username for 'https://github.com': [email protected]
Password for 'https://[email protected]@github.com':
To https://github.com/AndreaNobili/SpringBoot-Excel-API.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/AndreaNobili/SpringBoot-Excel-API.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
我插入了凭据,但获得了先前的错误消息。到底是什么意思?
4)我的想法是,在将我的代码推送到GitHub存储库之前,我必须执行一个远程存储库的拉动,我看到有.gitignore文件。
所以我尝试做:
developer@developer-virtual-machine:~/git/SOC-dashboard$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> master
我也尝试过这个:
developer@developer-virtual-machine:~/git/SOC-dashboard$ git pull origin master
Da https://github.com/AndreaNobili/SpringBoot-Excel-API
* branch master -> FETCH_HEAD
fatal: refusing to merge unrelated histories
那怎么了?我想念什么?如何尝试修复它并正确推送我的代码?
tl; dr当您在Github上进行回购时,已经在提交中添加.gitignore。git init
+git remote add
没有获取此提交或任何提交,您有一个空白的本地存储库。您的本地提交不是建立在远程提交的基础上,它们没有共同的祖先,它们是“无关的”。Git不会合并不相关的分支。
要对其进行修复,git fetch origin
请确保您具有远程的最新快照,并git rebase origin/master
在源的master分支之上重写本地提交。然后,您可以推动。
以后,请使用下载新的存储库git clone
。
$ git push origin master
Username for 'https://github.com': [email protected]
Password for 'https://[email protected]@github.com':
To https://github.com/AndreaNobili/SpringBoot-Excel-API.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/AndreaNobili/SpringBoot-Excel-API.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
所有信息都在那里,但是有点神秘。重要的一点是:
! [rejected] master -> master (non-fast-forward)
“快进”是指不需要合并即可更新分支。
例如。您克隆具有三个提交A,B和C的存储库。此时,您将获得该存储库的完整副本。
$ git clone <some repo>
origin
A - B - C [master]
local
A - B - C [master]
[origin/master]
origin/master
跟踪您看到远程存储库的master分支的最后一个位置。
您在本地添加了一些提交。
origin
A - B - C [master]
local
A - B - C [origin/master]
\
D - E [master]
你推。之所以会发生这种情况,是因为Git只需将D和E堆叠在E之上。这是“快速前进”的过程。
$ git push origin master
origin
A - B - C - D - E [master]
local
A - B - C - D - E [master]
[origin/master]
当您的历史分歧时,问题就来了。由于您进行了更改,当其他人提交到远程存储库时,就会发生这种情况。
让我们回到一个新的克隆。
$ git clone <some repo>
origin
A - B - C [master]
local
A - B - C [master]
您在本地添加了一些提交。
origin
A - B - C [master]
local
A - B - C - D - E [master]
有人推送了一些提交。
origin
A - B - C - F - G [master]
local
A - B - C [origin/master]
\
D - E [master]
您尝试推动,您将得到回报! [rejected] master -> master (non-fast-forward)
。您的历史分歧,需要合并。git push
不会这样做,您必须git pull
处理任何冲突。
$ git pull
origin
A - B - C - F - G [master]
local
A - B - C - F - G [origin/master]
\ \
D - E - M [master]
现在您可以推动了。
$ git pull
origin
A - B - C - F - G
\ \
D - E - M [master]
local
A - B - C - F - G [origin/master]
\ \
D - E - M [master]
那么,谁做出了不同的改变?你做到了 在Github上创建存储库时,它进行了初始提交以添加.gitignore文件。
如果您克隆了存储库,就可以了,就像上面一样。但是您改为初始化并添加了一个遥控器。
$ git init
$ git remote add origin https://github.com/AndreaNobili/SpringBoot-Excel-API.git
origin
A [master]
local
origin具有提交,但是您的本地存储库没有提交。如果您未明确告知Git,Git不会与遥控器进行对话。此时您必须这样做git pull
。或git clone
改为。
现在,您已经添加了更多提交。
$ git init
$ git remote add origin https://github.com/AndreaNobili/SpringBoot-Excel-API.git
origin
A [master]
local
B - C [master]
请注意,您的原始提交与本地提交之间没有关系。他们没有共同点。它们是“无关的”。
您尝试推动并获得成功的! [rejected] master -> master (non-fast-forward)
原因是历史分歧。
您尝试拉取,fatal: refusing to merge unrelated histories
因为您的本地提交与原始提交无关。Git不知道如何合并它们。
要解决此问题,请先从来源获取最新更改。
$ git fetch origin
origin
A [master]
local
B - C [master]
A [origin/master] # the unrelated commit fetched from origin
然后rebase
,在它们之上进行更改。rebase
基本上将您的更改复制到不相关的提交上。
$ git rebase origin/master
origin
A [master]
local
A [origin/master]
\
B1 - C1 [master]
现在您可以推动了。
(您也可以一步完成fetch + rebase:git pull --rebase origin master
。Agit pull
是fetch +合并,或使用--rebase是rebase。)
将来,请使用下载新的存储库git clone
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句