为什么设置了远程程序后,我无法将代码推送到GitHub存储库上? [英] Why i can't push my code on my GitHub repositorty after set up my remote?

查看:35
本文介绍了为什么设置了远程程序后,我无法将代码推送到GitHub存储库上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 GIT 不太满意,并且遇到以下问题:我在 GitHub 上创建了一个新的存储库.然后,在我的项目文件夹中,执行以下命令:

I am not so into GIT and I have the following problem: I created a new repository on GitHub. Then, into the folder of my project, I have done the following commands:

1)我将初始化存储库设置到我的项目文件夹中:

1) I set up the init repository into my project folder:

git init

2)我设置了对我的GitHub存储库的引用:

2) I set up the reference to my GitHub repository:

git remote add origin https://github.com/AndreaNobili/SpringBoot-Excel-API.git

并获取参考信息,我得到了这一点:

And retrieving the reference information I obtain this:

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)首先,我尝试推送我的项目代码:

3) First I tried to push my project code:

developer@developer-virtual-machine:~/git/SOC-dashboard$ git push origin master
Username for 'https://github.com': nobili.andrea@gmail.com
Password for 'https://my.mail@gmail.com@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.

我插入了凭据,但获得了先前的错误消息.到底是什么意思?

I inserted my credentials but I obtain the previous error message. What exactly means?

4)我的想法是我必须对远程存储库执行 pull ,然后再将我的代码推送到GitHub存储库中(我看到有 .gitignore 文件.

4) My idea is that I have to perform a pull of the remote repository before push my code (into the GitHub repository I see that there is the .gitignore file.

所以我试图做:

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

那怎么了?我想念什么?我该如何解决它并正确推送我的代码?

So what is wrong? What am I missing? How can I try to fix it and correctly push my code?

推荐答案

tl; dr .gitignore. git init + git remote add 没有获取此提交或任何提交,您有一个空白的本地存储库.您的本地提交不是建立在远程提交的基础上,它们没有共同的祖先,它们是无关的".Git不会合并不相关的分支.

tl;dr When you made your repo on Github it already had a commit in it to add .gitignore. git init + git remote add did not fetch this or any commits, you had a blank local repository. Your local commits were not built on top of the remote commits, they had no ancestor in common, they were "unrelated". Git will not merge unrelated branches.

要修复此问题,请 git fetch origin 来确保您具有远程的最新快照,并 git rebase origin/master 来重写本地提交.起源的主分支.然后,您可以进行推送.

To fix it, git fetch origin to ensure you have the latest snapshot of the remote, and git rebase origin/master to rewrite your local commits on top of the origin's master branch. Then you can push.

将来,请使用 git clone 下载新的存储库.

In the future, download a new repository using git clone.

$ git push origin master
Username for 'https://github.com': nobili.andrea@gmail.com
Password for 'https://my.mail@gmail.com@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.

所有信息都在那里,但是有点神秘.重要的一点是:

All the information is there, but it's a bit cryptic. The important bit is this:

 ! [rejected]        master -> master (non-fast-forward)

快进"是指无需合并即可更新分支.

A "fast-foward" is when there is no merge required to update a branch.

例如.克隆具有三个提交A,B和C的存储库.此时,您将获得该存储库的完整副本.

For example. You clone a repository that has three commits, A, B, and C. You get a complete copy of the repository at that moment.

$ git clone <some repo>

origin
A - B - C [master]

local
A - B - C [master]
          [origin/master]

origin/master 跟踪您看到远程存储库的master分支的最后一个位置.

origin/master tracks the last place you saw the remote repository's master branch.

您在本地添加了一些提交.

You add a few commits locally.

origin
A - B - C [master]

local
A - B - C [origin/master]
         \
          D - E [master]

你推.之所以会发生这种情况,是因为Git只需将D和E堆叠在E之上.这是快速前进"的事情.

You push. This push can happen because Git only has to stack D and E on top of E. It is a "fast-foward".

$ git push origin master

origin
A - B - C - D - E [master]

local
A - B - C - D - E [master]
                  [origin/master]


当您的历史记录分歧时,问题就来了.自从您进行更改以来,当其他人提交到远程存储库时就会发生这种情况.


The problem comes when your histories have diverged. This happens when someone else commits to the remote repository since you've made changes.

让我们回到一个新的克隆中.

Let's go back to a fresh clone.

$ git clone <some repo>

origin
A - B - C [master]

local
A - B - C [master]

您在本地添加了一些提交.

You add a few commits locally.

origin
A - B - C [master]

local
A - B - C - D - E [master]

其他人会推送一些提交.

Someone else pushes some commits.

origin
A - B - C - F - G [master]

local
A - B - C [origin/master]
         \
          D - E [master]

您尝试推送,您将获得![拒绝]主控->主人(非快进).您的历史记录已 不同 ,需要合并. git push 不会这样做,您必须 git pull 并处理任何冲突.

You try to push and you'll get ! [rejected] master -> master (non-fast-forward). Your histories have diverged and need to be merged. git push will not do that, you must git pull and deal with any conflicts.

$ 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文件的初始提交.

如果您克隆了存储库,就可以了,就像上面一样.但是您改为初始化并添加了一个遥控器.

If you'd cloned the repository you'd be fine, like above. But you instead initialized and added a remote.

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

origin has a commit, but your local repository has none. Git does not talk to remotes without your explicitly telling it to. You'd have to git pull at this point. Or do git clone instead.

现在,您已经添加了更多提交.

Now you've added more commits.

$ git init
$ git remote add origin https://github.com/AndreaNobili/SpringBoot-Excel-API.git

origin
A [master]

local
B - C [master]

请注意,您的原始提交与本地提交之间没有任何关系.他们没有共同点.它们是无关的".

Note that there is no relation between your commits on origin and your local commits. They have no commit in common. They're "unrelated".

您尝试推动并获取![拒绝]主控->掌握(非快进),因为历史有所分歧.

Your try to push and get ! [rejected] master -> master (non-fast-forward) because history has diverged.

您尝试获取致命错误:拒绝合并无关的历史记录,因为您的本地提交与原始提交无关.Git不知道如何合并它们.

You try to pull and get fatal: refusing to merge unrelated histories because your local commits and the commits on origin are unrelated. Git does not know how to merge them.

要解决此问题,请先从来源获取最新更改.

To fix this, first fetch the latest changes from origin.

$ git fetch origin

origin
A [master]

local
B - C [master]

A [origin/master]   # the unrelated commit fetched from origin

然后重新设置您的更改. rebase 基本上将您的更改复制到不相关的提交中.

Then rebase your changes on top of them. rebase basically copies your changes onto an unrelated commit.

$ git rebase origin/master

origin
A [master]

local
A [origin/master]
 \
  B1 - C1 [master]

现在您可以进行推送了.

And now you can push.

(您也可以一步完成fetch + rebase: git pull --rebase origin master . git pull 是fetch + merge,或--rebase一个rebase.)

(You can also do the fetch + rebase in one step: git pull --rebase origin master. A git pull is a fetch + a merge, or with --rebase a rebase.)

将来,请使用 git clone 下载一个新的存储库.

In the future, download a new repository with git clone.

这篇关于为什么设置了远程程序后,我无法将代码推送到GitHub存储库上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆