明天就要放假了,今天上班有點心不在焉···然后就翻車了,已經commit了好幾次才發現自己不在正確的那個分支上。
解決方法:
- git log顯示出當前分支上的所有commit
- 復制最近一次正確commit的ID
- git reset commitID (默認是--soft模式,加不加都可以。即不會更改index或working directory 中的內容)
- git checkout本應提交的那個分支名稱
- git commit 在當前這個正確的分支上提交剛剛那些走錯門的commit
注意:
- 千萬要注意第3步使用reset 時要慎用 --hard 選項,他會把index和 working directory也回退到你指定的那次commit。
- 如果你只是做錯了一次提交,可以將2 和3 省略為 git reset --soft HEAD^
- 我尚未push到遠端。
關於 git reset 命令,參考:https://git-scm.com/docs/git-reset
關於 reset 和 checkout 的詳解: https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E9%87%8D%E7%BD%AE%E6%8F%AD%E5%AF%86
關於類似問題的解決方法,參考:https://www.clearvision-cm.com/blog/what-to-do-when-you-commit-to-the-wrong-git-branch/,https://stackoverflow.com/questions/2941517/how-to-fix-committing-to-the-wrong-git-branch