最近在开发中,需要恢复git中已经删除的分支。
情况是这样,有两个开发分支,dev_1
和dev_2
,之所以维护两个,是因为需求存在分叉。
最近在dev_1
上出现了bug,所以,我开了一个fix
分支出来进行修复,修复完毕后,merge
回dev_1
上了,接着就把这个fix
分支删除了。但是,这些改动仍然需要fix到dev_2
上,此时,办法不唯一。
-
当然可以直接找到
fix
的commit
,然后cherry-pick
过去。 -
也可以尝试恢复
fix
分支,然后合并到dev_2
上去。
此时dev_1
的tree
大概是这样:
* 229e0eb (HEAD -> dev_1) Merge branch 'fix' into dev_1
|\
| * 321c03e fix c2
| * 738d5cf fix c1
|/
* bd44f10 dev 1 feat
* e274b11 (master) first commit
dev_2
的tree
:
* c9ff336 (HEAD -> dev_2) dev 2 feat
* e274b11 (master) first commit
git创建分支时,是可以指定一个起点的,默认是当前分支的最后一个commit
。
使用 git reflog
可以查看HEAD的变化历史。当然,用git log -g --oneline
也是同样的效果。如下:
229e0eb (HEAD -> dev_1) HEAD@{0}: checkout: moving from dev_2 to dev_1
c9ff336 (dev_2) HEAD@{1}: checkout: moving from dev_1 to dev_2
229e0eb (HEAD -> dev_1) HEAD@{2}: merge fix: Merge made by the 'ort' strategy.
bd44f10 HEAD@{3}: checkout: moving from fix to dev_1
321c03e HEAD@{4}: commit: fix c2
738d5cf HEAD@{5}: commit: fix c1
bd44f10 HEAD@{6}: checkout: moving from dev_1 to fix
bd44f10 HEAD@{7}: checkout: moving from master to dev_1
e274b11 (master) HEAD@{8}: checkout: moving from dev_2 to master
c9ff336 (dev_2) HEAD@{9}: checkout: moving from dev_1 to dev_2
bd44f10 HEAD@{10}: checkout: moving from dev_2 to dev_1
c9ff336 (dev_2) HEAD@{11}: commit: dev 2 feat
e274b11 (master) HEAD@{12}: checkout: moving from dev_1 to dev_2
bd44f10 HEAD@{13}: commit: dev 1 feat
e274b11 (master) HEAD@{14}: checkout: moving from DEV_1 to dev_1
e274b11 (master) HEAD@{15}: checkout: moving from dev_2 to DEV_1
e274b11 (master) HEAD@{16}: checkout: moving from dev_2 to dev_2
e274b11 (master) HEAD@{17}: checkout: moving from dev_1 to dev_2
e274b11 (master) HEAD@{18}: checkout: moving from master to dev_1
e274b11 (master) HEAD@{19}: commit (initial): first commit
只需要找到fix
分支最后一次提交的commit
,从它开始建立一个分支,就可以了。
git branch fix_rec 321c03e