一次git错误提交的操作记录:
$ git log
(错误的commit记录,比如:push之前没有pull,造成代码错乱)
commit c15809c85412701b8045c29e42b86ea79b757370 (HEAD -> zhejiang_1.13_part2_videoMsg_20190104)
Author: liupengzhen <liupengzhen@cmhi.com>
Date: Tue Feb 19 17:57:42 2019 +0800
OCT REM fix videoMst Template list
1、先使用reset尝试恢复:
$ git reset (回退当前commit的版本)
执行之后 $ git status 查看还是有很多冲突文件。。
2、reset操作无效,使用cherry-pick版本管理操作:
2.1、回退本地提交记录:
$ git add .
$ git reset --hard
HEAD is now at c15809c854 OCT REM fix videoMst Template list
2.2、创建本地分支,保留提交错误的版本号(代码):
$ git branch backup (创建当前分支到backup分支,做记录)
$ git branch (查看所有分支)
$ git checkout backup (切换backup分支)
Switched to branch 'backup'
$ git log (查看并记录最近一次错误提交的版本号)
$ git branch
* backup
master
zhejiang_1.11.2_ABX_bugfix
zhejiang_1.11.2_ABX_dev
2.3、切换到开发分支,更新下来最新的远程代码:
$ git checkout zhejiang_1.13_part2_videoMsg_20190104 (切换到最新代码的分支)
Switched to branch 'zhejiang_1.13_part2_videoMsg_20190104'
Your branch and 'origin/zhejiang_1.13_part2_videoMsg_20190104' have diverged,
and have 1 and 3 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
$ git reset --hard origin/zhejiang_1.13_part2_videoMsg_20190104
(回退一个版本,清空暂存区,将已提交的内容的版本恢复到本地,本地的文件也将被恢复的版本替换)
HEAD is now at 02f2f76d95 oct rem:提交最新代码
$ git pull (拉取最新代码)
2.4、在开发分支上cherry-pick备份保存的代码版本:
$ git cherry-pick c15809c85412701b8045c29e42b86ea79b757370 (检出之前备份的版本记录)
[zhejiang_1.13_part2_videoMsg_20190104 3de0bce5a0] OCT REM fix videoMst Template list
Date: Tue Feb 19 17:57:42 2019 +0800
2 files changed, 9 insertions(+), 10 deletions(-)
$ git log (此时看到日志记录中,检出的版本已经出现了)
commit 3de0bce5a0a4a187c4f90eb1aa6778c6924679a2 (HEAD -> zhejiang_1.13_part2_videoMsg_20190104)
Author: liupengzhen <liupengzhen@cmhi.chinamobile.com>
Date: Tue Feb 19 17:57:42 2019 +0800
OCT REM fix videoMst Template list
commit 1f3f17ac2688537afe8639827c187b1c499152a0 (origin/zhejiang_1.13_part2_videoMsg_20190104)
Merge: 02f2f76d95 19231543f9
Author: wujiamin <wujiamin@cmhi.chinamobile.com>
Date: Tue Feb 19 18:32:51 2019 +0800
Merge remote-tracking branch 'origin/zhejiang_1.13_part2_february_20190218' into zhejiang_1.13_part2_videoMsg_20190104
2.5、提交版本代码:
$ git push (提交这次版本号的代码即可)
---
3、删除本地本分分支:
查看现有分支情况(remote/origin/master表示的是远程分支):
删除本地备份的分支backup:
(git push origin --delete xxx 可以删除远程分支xxx)
注:上面当我删除本地分支backup的时候,提示我:The branch 'backup' is not fully merged.
所以,我应该也可以通过git merge backup命令,将该分支上的代码合到最新分支上。待验证...