git 恢复删除的本地分支
参考博客:https://blog.csdn.net/PersonalM/article/details/108385526
过程比较简单,下面以实验的方式讲解一下:
1 首先创建一个文件夹作为实验文件夹
mkdir test_recover
2 初始化目录
cd test_recover
git init
回显:
[user_name@id ~/c]$ cd test_recover
[user_name@id test_recover]$ git init
Initialized empty Git repository in /pd01/user_name/c/test_recover/.git/
3 创建一个文件并 commit
echo "this is master branch" > 1.txt
git add 1.txt
git commit -m "master add 1.txt"
回显:
[user_name@id test_recover]$ echo "this is master branch" > 1.txt
[user_name@id test_recover]$ git add .
[user_name@id test_recover]$ git commit -m "master add 1.txt"
[master (root-commit) 40bf112] master add 1.txt
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 1.txt
4 创建一个分支并添加一个文件并 commit
git checkout -b branch1
echo "branch1 add 2.txt" > 2.txt
git add 2.txt
git commit -m "branch2 add 2.txt"
回显:
[user_name@id test_recover]$ git checkout -b branch1
Switched to a new branch 'branch1'
[user_name@id test_recover]$ echo "branch1 add 2.txt" > 2.txt
[user_name@id test_recover]$ git add 2.txt
[user_name@id test_recover]$ git commit -m "branch2 add 2.txt"
[branch1 a1e927e] branch2 add 2.txt
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 2.txt
5 切换到 master 分支并删除 branch1 分支
git checkout master
git branch -l
git branch -D branch1
git branch -l
显示结果:
[user_name@id test_recover]$ git checkout master
Switched to branch 'master'
[user_name@id test_recover]$ git branch -l
branch1
* master
[user_name@id test_recover]$ git branch -D branch1
Deleted branch branch1 (was a1e927e).
[user_name@id test_recover]$ git branch -l
* master
6 恢复 branch1 分支
git reflog
git checkout -b branch1_recover HEAD@\{1}
git branch -l
显示结果:
[user_name@id test_recover]$ git reflog
65f5e75 HEAD@{0}: checkout: moving from branch1 to master
a1e927e HEAD@{1}: commit: branch2 add 2.txt
65f5e75 HEAD@{2}: checkout: moving from master to branch1
[user_name@id test_recover]$ git checkout -b branch1_recover HEAD@\{1}
Switched to a new branch 'branch1_recover'
[user_name@id test_recover]$ git branch -l
* branch1_recover
master