1. 程式人生 > >git 本地提交未推送 不小心遷出、刪除後 的找回方法!

git 本地提交未推送 不小心遷出、刪除後 的找回方法!

使用Git進行本地提交後,未上傳提交,卻不小心刪除了本地提交或提交所在分支,怎麼辦?????

不要緊!!!!

可以使用git reflog命令來幫助恢復刪除的本地提交!

執行以下命令你就知道怎麼用了!

  1. git stash 清空工作區和暫存區

  2. git pull 同步伺服器程式碼

  3. git commit –allow-empty -m “this is a test” 進行一次空提交

  4. git reset HEAD^ –hard 刪除剛才的空提交

  5. git reflog 檢視各分支的提交記錄

70cf5fe HEAD@{0}: reset: moving to HEAD^
28d447f HEAD@{1}
: commit: this is a test 70cf5fe HEAD@{2}: pull: Fast-forward
  1. git reset 28d447f –hard 恢復到commit號為28d447f 的提交,亦可使用HEAD@{1}

  2. git log

看this is a test 提交是不是回來了!!