如何将(未推送的)提交从一个分支移动到另一个分支

Ant*_*ony 2 git

我在branch-x并做了一些更改并提交了它们(但没有推动)。尽管提交是正确的,但它们不应该branch-xbranch-y. 我怎样才能从提交中获取branch-x并将它们应用到branch-y. 我想避免从branch-x.

下面是我在命令中所做的。

(branch-x)$: git status
(branch-x)$: git add .
(branch-x)$: git commit -m "some commit"
<oops, I should have made these changes in branch-y>
(branch-x)$: git checkout -b branch-y
(branch-y)$: <how can I take the commit from branch-x and apply it here?>
Run Code Online (Sandbox Code Playgroud)

Tim*_*sen 5

由于您已经通过创建了您想要的分支git checkout -b branch-y,并且这个新分支有您想要的提交,那么唯一剩下的问题就是branch-x. 由于您尚未发布branch-x,因此您应该安全地重写该历史记录。做这个:

git checkout branch-x    # switch back to branch-x
git reset --hard HEAD~1  # nuke the rogue commit on the tip of this branch
Run Code Online (Sandbox Code Playgroud)