Git 从release merge develop 上的代码,又回滚到merge之前的代码

一场无用功的merge

一:背景(开发工具Intellij IDE)

项目中有develop 和 release 两个分支, 在develop 开发新需求,release 分支是在提测的时候来合并develop代码供测试使用,没等到提测时,提前给merge 了。 

 

二:使用git reflog 查看所有HEAD历史  (操作包含commit(merge)、reset、chekcout、pull、renamed等),部分内容如下:

3444841f HEAD@{25}: reset: moving to 3444841f47251161d65d815cde72d8ffaccd87c7
66afa884 HEAD@{26}: checkout: moving from develop to release
3444841f HEAD@{27}: reset: moving to 3444841f47251161d65d815cde72d8ffaccd87c7
6b7503b8 (HEAD -> release-333, recover-branch) HEAD@{28}: checkout: moving from release to develop
66afa884 HEAD@{29}: merge develop: Merge made by the 'recursive' strategy.
541b298f HEAD@{30}: checkout: moving from develop to release
6b7503b8 (HEAD -> release-333, recover-branch) HEAD@{31}: commit: <8D><88><8F><90><9C>
<88><81>
dbe3699e (origin/release) HEAD@{32}: checkout: moving from release to develop
541b298f HEAD@{33}: commit: <8D><88><8F><90><9C><88><81>
ce7397d8 HEAD@{34}: checkout: moving from develop to release
dbe3699e (origin/release) HEAD@{35}: checkout: moving from release to develop
ce7397d8 HEAD@{36}: commit (merge): Merge branch 'develop' into release
0d055a70 HEAD@{37}: pull --progress --no-stat -v --progress origin release: Fast-forward
e145813b HEAD@{38}: checkout: moving from develop to release
dbe3699e (origin/release) HEAD@{39}: pull --progress --no-stat -v --progress origin develop: Merge made by the 'recursive' strategy.
3abb05ef HEAD@{40}: commit: <8D><88><8F><90><9C><88><81>
8b5d7138 HEAD@{41}: pull --progress --no-stat -v --progress origin develop: Merge made by the 'recursive' strategy.
304d49e1 (release) HEAD@{42}: commit: <97><9D><84><9C><8B><8E><8F><80><8F><91>
c001f23c HEAD@{43}: pull --progress --no-stat -v --progress origin develop: Fast-forward
c7bbbed6 HEAD@{44}: checkout: moving from release to develop
 

二:观察日志列表内容,找到这次merge 之前 git log (上面内容中绿色行)。再做如下操作完成代码回滚

(1)基础绿色行版本创建新的分支

         git branch recover-branch 0d055a70

(2)切换到新的分支上

          git checkout recover-branch 

(3)在Intellij 查看这个项目的git show history内容

      发现最近历史记录是这次merge 前的内容。

(4)把原来的release本地分支更改名称,而recover-branch 分支更改名称为release

  (5)  在新改名release 分支下,做如下操作把本地新代码同步到相应的远程分支上

        git push origin HEAD --force

你可能感兴趣的:(java)