First create a new branch with the following steps, modify some files and delete them for recovery.
1. Create branch abc
git branch abc
- 1
- 1
2. View the branch list
git branch -a
abc
* develop
remotes/origin-dev/develop
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
3. Switch to the abc branch, modify something and 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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
4. Delete branch abc
git branch -D abc
Deleted branch abc (was 3eac14d).
- 1
- 2
- 1
- 2
5. Check the branch list, the abc branch no longer exists
git branch -a
* develop
remotes/origin-dev/develop
- 1
- 2
- 3
- 1
- 2
- 3
The recovery steps are as follows:
1. Use git log -g to retrieve the previously submitted commit
commit 3eac14d05bc1264cda54a7c21f04c3892f32406a
Reflog: HEAD@{1} (fdipzone <[email protected]>)
Reflog message: commit: add test.txt
Author: fdipzone <[email protected]>
Date: Sun Jan 31 22:26:33 2016 +0800
add test.txt
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
2. Use the git branch recover_branch [new branch] commit_id command to create a branch with this commit
git branch recover_branch_abc 3eac14d05bc1264cda54a7c21f04c3892f32406a
git branch -a
* develop
recover_branch_abc
remotes/origin-dev/develop
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
You can see that recover_branch_abc has been created
3. Switch to the recover_branch_abc branch and check if the file exists
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
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
This will restore the branch that was accidentally deleted.
Reprinted from: http://blog.csdn.net/fdipzone/article/details/50616386