Git 分支刪除后恢復

如果本地功能分支刪除了,發現遠程倉庫也沒有提交怎么辦?

如,當前 FileServer 項目本地分支如下

? FileServer git:(master) git branch 
 download
 js-post-upload
 kekule
* master

刪除 download 分支,查看本地分支,沒有 download 分支了

?  FileServer git:(master) git branch -D download 
Deleted branch download (was 260b35d).
?  FileServer git:(master) git branch
  js-post-upload
  kekule
* master

找回刪除分支

使用 git log -g 查看所有 commit,根據 commit 記錄查找功能分支(download)的 commit 提交,刪除的分支 commit 沒有分支記錄,如: commit 260b35df028dde0e467786c0489558be0e943ee4

?  FileServer git:(master) git log -g

Author: gyw123 <username@xx.com>
Date:   Sun Jun 10 22:52:00 2018 +0800

    add 3d

commit 260b35df028dde0e467786c0489558be0e943ee4
Reflog: HEAD@{1} (gywgithub <qingyi_w@outlook.com>)
Reflog message: commit: add a.txt
Author: gywgithub <qingyi_w@outlook.com>
Date:   Wed Feb 26 18:34:03 2020 +0800

    add a.txt

commit d80a60f37d6c1d1ed633fb9bfab6e09ac06df6e8 (origin/js-post-upload, origin/js-post-upload, js-post-upload)
Reflog: HEAD@{2} (gywgithub <qingyi_w@outlook.com>)
Reflog message: checkout: moving from js-post-upload to download
Author: username <username@xx.com>
Date:   Thu Nov 14 17:18:29 2019 +0800

    add post_403

commit d80a60f37d6c1d1ed633fb9bfab6e09ac06df6e8 (origin/js-post-upload, origin/js-post-upload, js-post-upload)
Reflog: HEAD@{3} (gyw123 <username@xx.com>)
Reflog message: checkout: moving from d80a60f37d6c1d1ed633fb9bfab6e09ac06df6e8 to js-post-upload
Author: username <username@xx.com>
Date:   Thu Nov 14 17:18:29 2019 +0800

    add post_403

commit d80a60f37d6c1d1ed633fb9bfab6e09ac06df6e8 (origin/js-post-upload, origin/js-post-upload, js-post-upload)
Reflog: HEAD@{4} (gyw123 <username@xx.com>)
Reflog message: checkout: moving from master to origin/js-post-upload
Author: username <username@xx.com>
Date:   Thu Nov 14 17:18:29 2019 +0800

    add post_403

由于記得在功能分支(download)中最后添加了 a.txt 文件,現在可以確定 commit 260b35df028dde0e467786c0489558be0e943ee4 是功能分支(download)的提交

使用 git branch new_branch commitId 命令創建功能分支(download),這樣創建出來的分支就保留了 commitId 的對應的代碼修改

?  FileServer git:(master) git branch download 260b35df028dde0e467786c0489558be0e943ee4
?  FileServer git:(master) git branch 
?  FileServer git:(master) git checkout download 
Switched to branch 'download'
?  FileServer git:(download) git log

commit 260b35df028dde0e467786c0489558be0e943ee4 (HEAD -> download)
Author: gywgithub <qingyi_w@outlook.com>
Date:   Wed Feb 26 18:34:03 2020 +0800

    add a.txt
...
提示

如果 commit 記錄很多,可以使用 git log -g --grep 'xxx' 命令根據 commit 提交的信息模糊搜索;或者使用 git log -g --author='username@xx.com' 命令根據用戶名模糊搜索。

git 命令參數文檔查找可以使用 git log --help , git --help

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。