簡體   English   中英

為什么在設置我的遙控器后無法將我的代碼推送到我的 GitHub 存儲庫?

[英]Why i can't push my code on my GitHub repositorty after set up my remote?

我不是很喜歡GIT ,我有以下問題:我在GitHub 上創建了一個新存儲庫。 然后,進入我的項目文件夾,我執行了以下命令:

1)我將init存儲庫設置到我的項目文件夾中:

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': 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.

我插入了我的憑據,但我收到了之前的錯誤消息。 究竟是什么意思?

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 上創建你的 repo 時,它已經有一個添加 .gitignore 的提交。 git init + git remote add沒有獲取這個或任何提交,你有一個空白的本地存儲庫。 您的本地提交不是建立在遠程提交之上的,它們沒有共同的祖先,它們是“無關的”。 Git 不會合並不相關的分支。

要修復它, git fetch origin以確保您擁有遠程的最新快照,以及git rebase origin/master以在源的主分支之上重寫您的本地提交。 然后就可以推了。

將來,使用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.

所有的信息都在那里,但它有點神秘。 重要的一點是:

 ! [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) ! [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 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) ! [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 git pull是 fetch + 合並,或者使用 --rebase 是 rebase。)

將來,使用git clone下載新的存儲庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM