1,代碼未提交時:
使用以下命令即可解決。
git add . (把所有改動暫存)
git stash (把暫存的文件提交到git的暫存棧)
git checkout 本該提交代碼的分支
git stash pop (將暫存棧中的代碼放出來)
2,代碼提交了
git checkout 不該提交代碼提交了代碼的分支
git reset HEAD~1 (最近一次提交放回暫存區, 並取消此次提交)
git stash (把暫存的文件提交到git的暫存棧)
git checkout 應該提交代碼的分支
git stash pop
git checkout 不該提交代碼提交了代碼的分支 (等你把代碼提交到了正確的分支后,再次切到剛剛錯的分支)
git push origin 錯誤的分支 -f (把不該上去的文件回退掉)
轉載自:https://www.jianshu.com/p/28ff3c15be37