mancuoj / TIL

今天学了什么,记录在 issue 中
1 stars 0 forks source link

Git 不小心提交到了错误的分支该怎么办? #2

Open mancuoj opened 1 month ago

mancuoj commented 1 month ago

首先在错误分支取消上次的提交,然后 stash 保存你的修改:

git reset HEAD~ --soft
git stash

切换到正确分支,pop 出你临时保存的修改重新提交:

git checkout correct-branch
git stash pop
git add .
git commit -m "your message"
mancuoj commented 1 month ago

另一种方法

git cherry-pick 命令的作用是将其他分支中的某个具体提交应用到当前分支上。

所以我们可以在正确分支上应用错误分支的最新一次提交:

git checkout correct-branch
git cherry-pick wrong-branch

最后删除错误分支上的这个提交:

git checkout wrong-branch
git reset HEAD~ --hard