在使用git的過程中,因為人為因素造成分支(commit)被刪除,可以使用以下步驟進行恢復。
首先用以下步驟創建一個新分支,修改一些文件后刪除,以便進行恢復。
1.創建分支 abc
git branch abc
2.查看分支列表
git branch -a abc * develop remotes/origin-dev/develop
3.切換到abc分支,隨便修改一下東西后 commit
切換分支
git checkout abc
Switched to branch 'abc' 創建一個文件 echo 'abc' > test.txt commit git add . git commit -m 'add test.txt' [abc 3eac14d] add test.txt 1 file changed, 1 insertion(+) create mode 100644 test.txt
4.刪除分支abc
git branch -D abc Deleted branch abc (was 3eac14d).
- 1
- 2
5.查看分支列表,abc分支已不存在
git branch -a * develop remotes/origin-dev/develop
恢復步驟如下:
1.使用git log -g 找回之前提交的commit
commit 3eac14d05bc1264cda54a7c21f04c3892f32406a Reflog: HEAD@{1} (fdipzone <fdipzone@sina.com>) Reflog message: commit: add test.txt Author: fdipzone <fdipzone@sina.com> Date: Sun Jan 31 22:26:33 2016 +0800 add test.txt
2.使用git branch recover_branch[新分支] commit_id命令用這個commit創建一個分支
git branch recover_branch_abc 3eac14d05bc1264cda54a7c21f04c3892f32406a git branch -a * develop recover_branch_abc remotes/origin-dev/develop
可以見到recover_branch_abc已創建
3.切換到recover_branch_abc分支,檢查文件是否存在
git checkout recover_branch_abc
Switched to branch 'recover_branch_abc' ls -lt total 8 -rw-r--r-- 1 fdipzone staff 4 1 31 22:38 test.txt
這樣就可以恢復被誤刪的分支了
轉自 https://blog.csdn.net/fdipzone/article/details/50616386