我可以使用 Git 中的 'Amend last commit' 将更改转移到另一个分支吗?

Can I use the 'Amend last commit' in Git to transfer the changes to another branch?

我不小心在错误的分支中进行了提交,但由于我可以进行修改,所以我在考虑是否可以使用它来将更改转移到正确的分支。我可以改为更改分支并在那里进行修改,还是将其应用于提交它的同一分支?

假设你还没有推送错误提交的第一个分支,你可以尝试将它挑选到正确的分支,然后回滚第一个分支:

# switch to correct branch, and cherry-pick desired commit
git checkout branch2
git cherry-pick <SHA-1 of commit>

# then switch to first branch, and remove incorrect commit
git checkout branch1
git reset --hard HEAD~1

可以使用git log branch1找到您想要的提交的<SHA-1>。请注意,如果您已经推送 branch1,那么更安全的选择是 git revert 该提交。但是,您仍然可以使用 cherry picking 将提交移动到第二个分支。