[英]Git push rejected "non-fast-forward"
我是git
的新手,但目前正在使用它來管理我們在團隊環境中的代碼。 我遇到了一些變基問題,我使用以下方法修復了它們:
git checkout --ours filename.txt
git add filename.txt
git rebase --continue
現在我想推送我的更改,因此運行以下命令:
$ git push origin feature/my_feature_branch
給我以下錯誤:
To ssh://git@coderepo.com:7999/repo/myproject.git
! [rejected] feature/my_feature_branch -> feature/my_feature_branch (non-fast-forward)
error: failed to push some refs to 'ssh://git@coderepo.com:7999/repo/myproject.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
我該怎么做才能擺脫這個錯誤?
注意:我盡可能避免使用--force
選項。
看起來,有人在你最后一次git fetch
和git push
之間推送了新的提交。 在這種情況下,您需要重復您的步驟並再一次 rebase my_feature_branch
。
git fetch
git rebase feature/my_feature_branch
git push origin feature/my_feature_branch
在git fetch
之后,我建議使用gitk --all
檢查情況。
可能您在變基之前沒有獲取遠程更改,或者有人推送了新的更改(當您變基並嘗試推送時)。 嘗試以下步驟:
#fetching remote 'feature/my_feature_branch' branch to the 'tmp' local branch
git fetch origin feature/my_feature_branch:tmp
#rebasing on local 'tmp' branch
git rebase tmp
#pushing local changes to the remote
git push origin HEAD:feature/my_feature_branch
#removing temporary created 'tmp' branch
git branch -D tmp
試試這個命令
$ git push -f -u origin <name of branch>
即$ git push -f -u origin master
我有這個問題! 我試過:git fetch + git merge,但沒有解決! 我試過:git pull,也沒有解決
然后我嘗試了這個並解決了我的問題(類似於工程師的回答):
git fetch origin master:tmp
git rebase tmp
git push origin HEAD:master
git branch -D tmp
我遲到了,但我在github 幫助頁面中找到了一些有用的說明,並想在這里分享。
有時,Git 無法在不丟失提交的情況下對遠程存儲庫進行更改。 發生這種情況時,您的推送將被拒絕。
如果其他人與您推送到同一分支,Git 將無法推送您的更改:
$ git push origin master
To https://github.com/USERNAME/REPOSITORY.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/USERNAME/REPOSITORY.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again. See the
'Note about fast-forwards' section of 'git push --help' for details.
您可以通過獲取遠程分支上所做的更改並將其與您在本地所做的更改合並來解決此問題:
$ git fetch origin
# Fetches updates made to an online repository
$ git merge origin YOUR_BRANCH_NAME
# Merges updates made online with your local work
或者,您可以簡單地使用git pull
同時執行這兩個命令:
$ git pull origin YOUR_BRANCH_NAME
# Grabs online updates and merges them with your local work
我有一個類似的問題,我解決了它: git pull origin
就我而言,對於完全相同的錯誤,我也不是唯一的開發人員。
所以我同時提交並推送我的更改,在Commit
對話框彈出窗口的底部可以看到:
...但是我犯了一個巨大的錯誤,就是忘記點擊Fetch
按鈕來查看我是否有最新的,而我沒有。
提交成功執行,但不是推送,而是給出了相同的錯誤; ...即使其他開發人員沒有更改與我相同的文件,我也無法提取最新的文件,因為出現了相同的錯誤。
大多數時候我更喜歡堅持使用 Sourcetree 的GUI(圖形用戶界面) 。 這個解決方案可能並不理想,但這就是讓我重新開始的原因,而不用擔心我可能會丟失我的更改或損害其他開發人員的最新更新。
右鍵單擊您之前的提交以撤消本地提交的更改,然后選擇Reset current branch to this commit
,如下所示:
一旦所有加載微調器消失並且 Sourcetree 完成加載上一個提交,在窗口的左上角,單擊Pull
按鈕...
...然后會彈出一個對話框,然后單擊右下角的OK
按鈕:
拉完最新版后,如果沒有出現任何錯誤,請跳到第 4步(下面的下一步)。 否則,如果您此時發現任何合並沖突,就像我對Web.config
文件所做的那樣:
...然后單擊頂部的Stash
按鈕,將出現一個對話框彈出窗口,您需要編寫一個 Descriptive-name-of-your-changes,然后單擊OK
按鈕:
...一旦 Sourcetree 完成存儲您更改的文件,重復步驟 2中的操作(上述上一步),然后您的本地文件將具有最新更改。 現在可以通過打開STASHES
底部的 STASHES 來重新應用您的更改,使用箭頭展開您的 stash,然后右鍵單擊選擇Apply Stash 'Descriptive-name-of-your-changes'
,然后選擇OK
彈出對話框中的按鈕:
如果您現在有任何合並沖突,請轉到您首選的文本編輯器,如 Visual Studio Code,並在受影響的文件中選擇Accept Incoming Change
鏈接,然后保存:
然后回到 Sourcetree,點擊頂部的Commit
按鈕:
然后右鍵單擊沖突的文件,並在Resolve Conflicts
下選擇Mark Resolved
選項:
最后!!! 我們現在可以提交我們的文件,還可以在單擊Commit
按鈕之前選中Push changes immediately to origin
選項:
PS 在寫這篇文章的時候,另一個開發者在我提交之前提交了一個提交,所以不得不重復很多步驟。
當您使用https://github.com/在您的 GitHub 存儲庫中提交了一些更改,這些更改未拉入您的本地存儲庫時,就會出現此問題
為了解決這個問題——
git pull <git repo HTTPS LINK>
git commit -m "updates and bugs fixed"
git push
我遇到了這個問題,以上建議都沒有幫助我。 我能夠正確獲取所有內容。 但是推送總是失敗。 它是一個位於 windows 目錄的本地存儲庫,有多個客戶端通過 VMWare 共享文件夾驅動程序使用它。 似乎其中一個系統鎖定了 Git 存儲庫以進行編寫。 停止相關的VMWare系統后,導致鎖的一切立即修復。 幾乎不可能弄清楚是哪個系統導致了錯誤,所以我不得不一一阻止它們,直到成功。
好吧,我在這里使用了建議,當它直接將我的本地代碼合並到 master 時,它把我搞砸了。 .... 所以把這一切都帶上一粒鹽。 我的同事說以下有助於解決問題,需要重新指向我的分支。
git branch --set-upstream-to=origin/feature/my-current-branch feature/my-current-branch
在 Eclipse 中執行以下操作:
GIT Repositories > Remotes > Origin > 右鍵單擊並說 fetch
GIT Repositories > Remote Tracking > 選擇你的分支並說合並
轉到項目,右鍵單擊您的文件並說從上游獲取。
這是解決此問題的另一種解決方案
>git pull
>git commit -m "any meaning full message"
>git push
得到非快進錯誤后,只需執行以下操作:
1> git pull --rebase origin <name-of-the-remote-branch>
這會將遠程更改獲取到您的本地分支。 最重要的是,它將應用您的本地提交。
2> git push origin <name-of-the-remote-branch>
這會將遠程分支的本地副本中的本地更改應用到 git 中的實際遠程分支 repo
git結賬大師
git fetch [推送到master之前公司的時間分支]
git pull --rebase [推送到master之前公司的臨時分支] master
git checkout -b [新分支]
然后添加您的文件並執行以下操作:
混帳添加。
git commit -m"添加的文章"
git push -u origin [新分支]
這對我有用
有時,Git 無法在不丟失提交的情況下對遠程存儲庫進行更改。 發生這種情況時,您的推送將被拒絕。 如果其他人與您推送到同一分支,Git 將無法推送您的更改
您可以通過以下方式解決此問題:
git pull origin YOUR_BRANCH_NAME
只有這對我有用:
您可以通過獲取並合並在遠程分支上所做的更改與您在本地所做的更改來解決此問題:
$ git fetch origin
# Fetches updates made to an online repository
$ git merge origin YOUR_BRANCH_NAME
# Merges updates made online with your local work
關注此以獲得更多見解
git reset --soft HEAD~1
git pull
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.