侧边栏壁纸
  • 累计撰写 100 篇文章
  • 累计创建 55 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录
Git

恢复被删除的分支

Malson
2022-06-25 / 0 评论 / 0 点赞 / 76 阅读 / 345 字

首先用以下步骤创建一个新分支,修改一些文件后删除,以便进行恢复。

创建分支

  • 创建分支 abc
$ git branch abc
  • 查看分支列表
$ git branch -a
  abc
* develop
  remotes/origin-dev/develop

切换到abc分支

  • 切换分支
$ git checkout abc
Switched to branch 'abc'

随便修改一下东西后 commit

  • 创建一个文件
$ echo 'abc' > test.txt
  • 提交这个文件
$ git add .
$ git commit -m 'add test.txt'
[abc 3eac14d] add test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

删除分支(假装误删)

  • 删除分支abc
$ git branch -D abc
Deleted branch abc (was 3eac14d).
  • 查看分支列表,abc分支 已不存在
$ git branch -a
* develop
  remotes/origin-dev/develop

恢复步骤如下

  • 使用 git log -g 找回之前提交的 commit
$ git log -g
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
  • 使用 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已创建

  • 切换到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

这样就可以恢复被误删的分支了

0
博主关闭了所有页面的评论