常见的错误,我使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