常見的錯誤,我使Git是如何撤消提交併將更改提交到Git中的其他分支?
- 沒有檢查我
- 犯一個錯誤的分支修改(分支B,以爲我在一個哪個分支,commiting爲特徵的改變)
如何取回,並將編輯提交到正確的分支?
常見的錯誤,我使Git是如何撤消提交併將更改提交到Git中的其他分支?
如何取回,並將編輯提交到正確的分支?
rebase --onto
可以在這裏幫助,特別是如果你有一系列的提交移回。
不要忘記git stash
保存與「wrongBranch
」(提交適當應用的那個)相關的任何當前未提交的更改,以便在流程結束時將其彈出。
基本上,您需要申請提交或範圍的承諾to another branch(這裏所說的「rightBranch
」):
# Checkout the right branch where the commit should have been applied
git checkout rightBranch
# Checkout a new temporary branch at the current location
git checkout -b tmp
# Move the rightBranch branch to the head of patchset which should have been on rightBranch
git branch -f rightBranch last_SHA-1_of_commits_for_rightBranch
# Rebase the patchset onto tmp, the old location of rightBranch
git rebase --onto tmp first_SHA-1_of_commits_for_rightBranch~1 rightBranch
(first_SHA-1_of_commits_for_rightBranch~1
在父提交的first_SHA-1_of_commits_for_rightBranch
,那就是在上面提交其所有對於rightBranch的提交過應用不當)
rightBranch
將在頂部再次爲:
tmp
分支指出)wrongBranch
,它剛剛被重播上的tmp
頂部(tmp
爲前rightBranch HEAD
)。然後,您可以將wrongBranch
重置爲某些以前的提交。
git checkout wrongBranch
git reset --hard some_older_commit
# if needed, re-apply what you were working on vefore this all proccess
git stash pop
警告:
wrongBranch
已經發布(推送到公共回購),即對於那些拉回該回購的人來說可能會很尷尬(必須在「新」分支之上重新設置所有變化)@VonC,+1用於清晰的播放。在git rebase --onto ...命令中,你的意思是first_SHA-1-commit-for-rightBranch〜1嗎? – 2010-01-20 11:58:33
@Wayne:哈哈!正確的,在指定的提交之後,爲'rebase --onto'重播的提交範圍開始*。還有一個錯誤的錯誤。 – VonC 2010-01-20 12:53:19
結帳錯誤的分支,其中承諾是
git checkout wrong_branch
在錯誤的分支,恢復到以前的承諾:
git reset --hard HEAD^
注:^運算符表示以前提交,刪除多個提交使用〜N,其中N是提交
結帳的分支,其中所述提交數目應該
git checkout right_branch
重新應用提交使用櫻桃挑
git cherry-pick [email protected]{1}
注:wrong_branch @ {1}是之前的最後提交wrong_branch 的GIT中被執行重置命令,例如,HEAD @ {2 }可太習慣在這種情況下
要移動多次提交你可以使用多個調用與git摘櫻桃或混帳底墊只有一個執行:
git rebase --onto right_branch [email protected]{1}~N [email protected]{1}
(以及相應的復位參數在這種情況下將爲HEAD〜N)
簡單的答案?
git checkout branch_with_wrong_tip
git reset HEAD~1 (or whatever number of commits you want to go back)
git checkout correct_branch
git commit ...etc
注意的重要組成部分,這就是軟復位(保留更改),而不是任何硬復位,這可能讓你失去改變的地方。
這也會將您重置的所有提交返回到您必須單獨或作爲組重新提交的更改。例如。如果你git重置HEAD〜10,你將不得不對10個提交中提交的所有文件進行提交,否則將它們變成一個全新的提交。當然,如果你使用這種方法。
如果你經常忘記你正在使用bash shell的分支,可以查看:http://railstips.org/blog/archives/2009/02/02/bedazzle-your-bash-prompt -with-git-info/ – whaley 2010-01-20 13:35:17