Git 分支删除后恢复

简介: 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

目录
相关文章
|
10天前
|
缓存 开发工具 git
Git创建分支以及合并分支
在Git中,创建分支使用`git branch [branch_name]`,切换分支使用`git checkout [branch_name]`。修改文件后,通过`git add [file]`添加到暂存区,然后`git commit`提交到本地仓库。如果是新建分支的第一次推送,使用`git push origin [branch_name]`推送到远程仓库,之后可以简化为`git push`。合并分支时,使用`git merge [branch_name]`将指定分支的更改合并到当前分支。
13 2
Git创建分支以及合并分支
|
4月前
|
项目管理 开发工具 git
Git项目管理——分支(三)
Git项目管理——分支(三)
56 2
|
7天前
|
存储 Linux 开发工具
Git基础命令,分支,标签的使用【快速入门Git】
本文详细介绍了Git版本控制系统的基础概念和常用命令,包括工作区、暂存区和版本库的区别,文件状态的变化,以及如何进行文件的添加、提交、查看状态、重命名、删除、查看提交历史、远程仓库操作和分支管理,还涉及了Git标签的创建和删除,旨在帮助读者快速入门Git。
Git基础命令,分支,标签的使用【快速入门Git】
|
21天前
|
测试技术 开发工具 git
掌握 Git 分支策略:提升你的版本控制技能
在现代软件开发中,版本控制至关重要,Git 作为最流行的分布式版本控制系统,其分支管理策略对于高效协作和代码维护尤为重要。本文介绍了几种常用的 Git 分支策略,包括主线开发模型、功能分支模型、Gitflow 工作流和 Forking 工作流,并探讨了如何根据项目需求选择合适的分支模型。通过保持 `master` 分支稳定、及时合并清理分支、使用命名规范、利用 Pull Request 进行代码审查及自动化测试等最佳实践,可以显著提升团队协作效率和软件质量。掌握这些策略将帮助开发者更好地管理代码库,加快开发流程。
|
5月前
|
JSON 开发工具 git
git rebase 合并当前分支的多个commit记录
git rebase 合并当前分支的多个commit记录
|
2月前
|
开发工具 git 开发者
|
2月前
|
项目管理 开发工具 git
|
2月前
|
存储 小程序 安全
【技巧】git stash用的好,切换分支随便搞
本文详细介绍了 Git 中的 `git stash` 命令,帮助你在切换分支时临时保存未提交的更改。通过实际操作示例,展示了如何使用 `git stash` 的各种命令,如 `save`、`list`、`apply` 等。无论你是初学者还是有一定经验的开发者,都能从中受益。
33 0
【技巧】git stash用的好,切换分支随便搞
|
2月前
|
Shell 开发工具 git
|
2月前
|
JavaScript 测试技术 开发工具
Git 分支设计规范
Git 分支设计规范
88 11