git reflog 恢复已删除分支
语法: git reflog --date=[iso | local | relative] | grep [-w 全词匹配]
iso
:格式化后的时间时间local
:实际时间relative
:相对时间, 多少天之前
本案例以恢复remove_branch为例
一
对remove_branch
分支操作。 合并develop
分支到remove_branch
, 在remove_branch
分支做了两次commit
, 离开remove_branch
分支最终删除该分支。 其活动历时 如图:
二
git reflog
会记录所有的HEAD变动记录, 如commit, 分支切换信息。
通过查询最后一次进入分支remove_branch
,最后一次离开remove_branch
分支这段时间内head的变化来恢复分支。
由图可知remove_branch
分支最后一次进入,离开的时间段为2018-07-20 10:06:39 到2018-07-20 10:09:14
搜索这个时间段内的所有记录, 找到最近一次commit的记录 复制第一列的commit id
三
还原删除分支
从最近一次commit 中检出分支,可重命名,本例重命名为:reback_remove_branch
git checkout -b reback_remove_branch ddd94a4