如何撤销提交并将更改提交到Git中的其他分支? [英] How to undo a commit and commit the changes into the other branch in Git?
问题描述
我在git中犯的常见错误是
- 不检查我是哪个分支
- 将更改提交给错误的分支(在分支B上,认为我在A上,对功能A进行了更改)
我会回去,并将编辑提交给正确的分支吗?
/ code>可以在这里帮助,特别是如果你有一系列的提交回退。不要忘记 git stash
来保存任何与 wrongBranch
(提交被正确应用的那个),以便在进程结束时弹出它们。
基本上,您需要应用提交或提交范围 rightBranch ):
#检出提交应用的右分支
git checkout rightBranch
#在当前位置签出一个新的临时分支
git checkout -b tmp
#将rightBranch分支移动到本应位于右侧的patchset头部分支
git分支-f rightBranch last_SHA-1_of_commits_for_right分支
#将修补程序集重新定位到rightBranch的旧位置tmp
git rebase - 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
#如果需要,重新应用您正在处理的内容vefore this all proc cess
git stash pop
警告:
- 樱桃挑选或重新命名 - 有后果(参见 wrongBranch
- code>已经发布(推向公共回购),这对于那些回购该回购的人来说可能会很尴尬(必须在新分支之上重新设置他们的所有更改)。
The common mistake I make in git is
- not check on which branch I am
- commit changes to a wrong branch (on branch B, thinking I'm on A, commiting a change for feature A)
How do I get back, and commit the edits to the proper branch?
rebase --onto
can help here, especially if you have a range of commits to move back.
Do not forget a git stash
to save any current uncommitted changes which are relevant to "wrongBranch
" (the one where commits were appropriately applied), in order to pop them back at the end of the process.
Basically, you need to apply a commit or range of commits to another branch (called here "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
is the parent commit of first_SHA-1_of_commits_for_rightBranch
, that is the commit on top of which all the commits for rightBranch have been incorrectly applied)
rightBranch
will be again at the top of:
- all the old commits of right branch (pointed by the
tmp
branch) - plus all the relevant commits range from
wrongBranch
, which have just been replayed on top oftmp
(tmp
being the previousrightBranch HEAD
).
Then you can reset wrongBranch
to some previous commits.
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
Caveat:
- cherry-picking or rebase --onto have consequences (see this question)
- if
wrongBranch
has already been published (pushed to a public repo), that can be awkward for the people pulling for that repo (having to rebase all their changes on top of that "new" branch)
这篇关于如何撤销提交并将更改提交到Git中的其他分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!