簡體   English   中英

Git push master fatal: 你目前不在分支

[英]Git push master fatal: You are not currently on a branch

Master 是在 commit #10 處。 然而,我最終意識到我在測試過程中破壞了一些沒有被測試發現的東西。

我最終提交了#5,然后慢慢地重新做了每個提交的開發並不斷調整它以確保它不會重新引起錯誤。 現在我基本上回到了提交 #10,但是有一些更改可以防止錯誤發生。

我現在想使用我的更改創建提交 #11。 但是當我試圖推動掌握時,我得到了

fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
state now, use

    git push master HEAD:<name-of-remote-branch>

這是可以預料的。 但是我如何才能真正將其推送到我的遠程分支?

我嘗試git push origin HEAD:master但后來得到了這個:

! [rejected]        HEAD -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/tomhammond/sample.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and 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.

當我執行git status時,我看到HEAD detached from 4a74ac3

但是當我試圖推動掌握時,我得到了

fatal: You are not currently on a branch. To push the history leading to the current (detached HEAD)

這是可以預料的

在超脫狀態下工作是不可預料的,除非你故意想要這樣做,我懷疑你的情況。 您應該將master分支恢復到該提交,而不是檢查提交 #5,或者在交互模式下執行git rebase ,您可以根據需要重新哈希提交。

話雖如此,如果您確定處於分離狀態的master版本是您真正想要保留的版本,那么您可以通過強制將分支推送到遠程來解決non-fast-forward錯誤:

git push origin HEAD:master --force

但是,如果您強制推送,則可能會導致所有其他已簽出該分支的用戶出現問題。 一個風險較小的解決方案是從分離的 HEAD 創建一個臨時分支,然后將該分支合並到master

git branch temp-branch
git checkout master
git merge temp-branch
git push origin master

git push只會讓你快進遙控器。 這意味着您嘗試推送的提交需要是遠程分支的后代。 由於您在 5 之后編輯了之前的提交,因此您沒有后代,而是更多的堂兄弟。 如果你想覆蓋分支,你可以給git push --force ,但是如果其他人在當前 master 之上進行了自己的更改,他們將無法再拉分支。 此外,如果其他人在您之前推動掌握,他們的更改將丟失。 一般來說,如果你不是唯一一個使用分支的人,你不想強制推送。

你確定你真的在一個分行嗎? 使用git branch並檢查您是否在分支中。 如果沒有,只需git checkout branch-name-you-want然后git push就可以了!

您可以創建一個新分支,並且可以將這些更改合並到之前的分支中

git checkout -b newBranch 
git checkout previousBranch
git merge newBranch
git push origin previousBranch
git branch -d previousBranch

如果您要推送到新的存儲庫,您可以使用

git push origin HEAD:refs/heads/main --force

這里的main是一個新分支,如果您的目標存儲庫為空,將創建該分支。

如果你想在沒有變基的情況下這樣做,這個應該可以工作

git config pull.rebase false 
git pull origin master
git add .
git commit -m "Your Commit message"
git push

現在您對 go 有好處;-)

您可以創建一個新分支來保留您創建的提交,然后將它們推送到遠程。

使用此命令:

git switch -c <新分支名稱>

暫無
暫無

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

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