环境
git : 2+

前言
最近两天,公司的git合并代码时,出现了严重的问题,浪费很多时间;
现在记录下;
情况是这样的,一个同事自己的本地分支(远程没有),不知怎么的,有了别人开发分支的代码,而他自己又不知道;
其在切换到主分支,并merge自己的分支,此时其已经把别人正在开发的代码都合并到了主分支。

到了晚上准备升级时,才发现,主分支的代码出了问题;此时版本库是这样的:

如图 100047dcc这一步就有不该有的代码;

而此时版本库已经提交过了很多次,现在的问题就是,如何撤销掉100047dcc提交的代码,并且保留其他人提交的代码。

这个问题,折腾到了晚上9点半左右,尝试了网上给出的:

git rebase -i commit_id
//再通过将pick改为drop
1
2
但是,实际的效果是,100047dcc代码没了,其他人提交的代码也没有了!
也就是给人感觉和git reset --hard be8c6f6dd没有什么区别!

最后因为太晚,从提交记录上看,100047dcc之后就一个人提交了代码,所以就执行了:

//先切一个备份分支
git branch -b master_tmp
//再执行
git reset --hard be8c6f6dd
1
2
3
4
之后,那个人(也就是我)从备份分支上把文件拷贝回来—(因为我是直接在主分支上改的,自己的分支并没有代码)。

第二天,我打算去拷贝文件,我执行如下操作:

yutao@yutao MINGW64 /d/sts/workspace/ggservice (master)
$ git pull
remote: Counting objects: 44, done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 26 (delta 19), reused 0 (delta 0)
Unpacking objects: 100% (26/26), done.
From gitlab.gofund.cn:gg-service/ggservice
+ 1784b12...384decc master -> origin/master (forced update)
f8f2b19..eb33489 devyaomy -> origin/devyaomy
* [new branch] master_tmp -> origin/master_tmp
Already up-to-date.

yutao@yutao MINGW64 /d/sts/workspace/ggservice (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 796 commits.
(use "git push" to publish your local commits)
nothing to commit, working directory clean

yutao@yutao MINGW64 /d/sts/workspace/ggservice (master)
$ git push
Total 0 (delta 0), reused 0 (delta 0)
To git@gitlab.gofund.cn:gg-service/ggservice.git
384decc..1784b12 master -> master
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
简单的说,我的操作就是两步:
1git pull
2、git push
结果又把代码还原回去了!

为啥呢?

虽然昨天晚上,把远程库的版本回退到了正确的版本,但是我的本地主分支还是最新的commit,也就是说,相比远程库,我本地库是超前了多次提交,毕竟因为远程库回退了嘛!

这个时候,我必须也得对本地库进行回退,回退到线上相同的commit节点才行。

这个时候,我做了以下几个操作:

yutao@yutao MINGW64 /d/sts/workspace/ggservice (master)
$ git reset --soft 384deccaa6
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: .gitignore
modified: conf/application.conf
new file: conf/hanlp.properties
new file: dataservice/app/ggservice/common/UserCodeEnum.java
new file: dataservice/app/ggservice/v1/email/action/BindEmailGG3Action.java
new file: dataservice/app/ggservice/v1/email/action/SendEmailCaptchaGG3Action.java
// 文件太多不一一显示
yutao@yutao MINGW64 /d/sts/workspace/ggservice (master)
$ git reset --hard 384deccaa6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
上面敲了很多命令,其实真正只需要git reset --hard 384deccaa6即可。

git reset --hard 384deccaa6
1
接下来,我开始复制粘贴,从备份分支上,把代码拷贝下。
真的操蛋,这等于是增加工作量啊!

revert 撤销某次提交
到了下午,又有个同事干了类似我上午的操作。把不该有的代码提交上去了!
这就麻烦了,虽然远程库回退了!结果是要求凡是pull最新代码的人,都得进行本地回退的操作。
否则,就没完没了!

到了晚上,对着备份分支进行测试,终于找到了优雅的解决办法!

这就是revert命令

该命令就是为撤销某次提交而存在的;

首先,我是明白100047dcc这次提交是有问题的,这是问题的源头;
也就是说,只要我们把这次提交的给撤销了,就不会有问题了!

步骤 一
$ git revert 100047dcc
error: Commit 100047dccb58f3ee5e27b0dfaf5c02ac91dc2c73 is a merge but no -m option was given.
fatal: revert failed
1
2
3
结果报错了,报了一个Commit is a merge but no -m option was given.

为什么呢?

如果100047dcc这只是一个普通的提交,其实是不会报错的!
但是,这是一个merge的提交。

那么在撤销时,git并不知道我要撤销具体哪次!如下图:

这个时候,怎么办呢?

我的做法

步骤二
yutao@yutao MINGW64 /d/sts/workspace/ggservice (master_tmp)
$ git revert 100047dcc -m 1
error: could not revert 100047d... Merge branch 'master' of gitlab.gofund.cn:gg-service/ggservice into wjs
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
1
2
3
4
5
6
我执行了这样的一个操作:

git revert 100047dcc -m 1
1
参数 -m 就是指定要撤销的那个提价,从左往右,从1开始数;也就是我撤销的是ca4a7ff999。

接着其把代码冲突,然后我就解决冲突,保留主分支的代码,去掉那个人的代码。

解决完冲突后,我执行如下操作:

yutao@yutao MINGW64 /d/sts/workspace/ggservice (master_tmp|REVERTING)
$ git add -A

yutao@yutao MINGW64 /d/sts/workspace/ggservice (master_tmp|REVERTING)
$ git status
On branch master_tmp
Your branch is up-to-date with 'origin/master_tmp'.
You are currently reverting commit 100047d.
(all conflicts fixed: run "git revert --continue")
(use "git revert --abort" to cancel the revert operation)

Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: dataservice/app/ggservice/v1/datacentre/action/GetIncomeDistributeAction.java
//文件太多省略。。。

yutao@yutao MINGW64 /d/sts/workspace/ggservice (master_tmp|REVERTING)
$ git commit -m "ceshi"
[master_tmp d2ae829] ceshi
18 files changed, 95 insertions(+), 396 deletions(-)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
我上面执行的语句其实就是:

$ git add -A
$ git commit -m "ceshi"
1
2
步骤三
yutao@yutao MINGW64 /d/sts/workspace/ggservice (master_tmp)
$ git revert 100047dcc -m 2
error: could not revert 100047d... Merge branch 'master' of gitlab.gofund.cn:gg-service/ggservice into wjs
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
1
2
3
4
5
6
也就是执行:

$ git revert 100047dcc -m 2
1
即 撤销be8c6f6dde的提交,这个时候也会提示代码冲突了,

接着和上面一样,解决冲突,在提交:

yutao@yutao MINGW64 /d/sts/workspace/ggservice (master_tmp|REVERTING)
$ git status
On branch master_tmp
Your branch is ahead of 'origin/master_tmp' by 1 commit.
(use "git push" to publish your local commits)
You are currently reverting commit 100047d.
(fix conflicts and run "git revert --continue")
(use "git revert --abort" to cancel the revert operation)

Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: .gitignore
deleted: conf/hanlp.properties
deleted: dataservice/app/ggservice/common/UserCodeEnum.java
deleted: dataservice/app/ggservice/v1/email/action/BindEmailGG3Action.java
deleted: dataservice/app/ggservice/v1/email/action/SendEmailCaptchaGG3Action.java
deleted: dataservice/app/ggservice/v1/email/service/EmailCaptchaService.java
deleted: dataservice/app/ggservice/v1/expert/action/GetExpertOfStockAssessAction.java
modified: dataservice/app/ggservice/v1/expert/service/ExpertGG3Service.java
modified: dataservice/app/ggservice/v1/ggmtoolbox/action/GetMyStockLabelInfoAction.java
modified: dataservice/app/ggservice/v1/ggmtoolbox/action/UpdateUserTokenInfoAction.java
modified: dataservice/app/ggservice/v1/ggmtoolbox/service/AppDOSInfoService.java
modified: dataservice/app/ggservice/v1/ggmtoolbox/service/UserInfoService.java
modified: dataservice/app/ggservice/v1/graph/action/GetStockPlateComponentAction.java
modified: dataservice/app/ggservice/v1/graph/service/StockPlateService.java
// 文件太多省略。。。

yutao@yutao MINGW64 /d/sts/workspace/ggservice (master_tmp|REVERTING)
$ git add -A

yutao@yutao MINGW64 /d/sts/workspace/ggservice (master_tmp|REVERTING)
$ git status
On branch master_tmp
Your branch is ahead of 'origin/master_tmp' by 1 commit.
(use "git push" to publish your local commits)
You are currently reverting commit 100047d.
(all conflicts fixed: run "git revert --continue")
(use "git revert --abort" to cancel the revert operation)

Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: .gitignore
modified: conf/application.conf
deleted: conf/hanlp.properties
deleted: dataservice/app/ggservice/common/UserCodeEnum.java
deleted: dataservice/app/ggservice/v1/email/action/BindEmailGG3Action.java
deleted: dataservice/app/ggservice/v1/email/action/SendEmailCaptchaGG3Action.java
deleted: dataservice/app/ggservice/v1/email/service/EmailCaptchaService.java
deleted: dataservice/app/ggservice/v1/expert/action/GetExpertOfStockAssessAction.java
modified: dataservice/app/ggservice/v1/expert/service/ExpertGG3Service.java
modified: dataservice/app/ggservice/v1/ggmtoolbox/action/GetMyStockLabelInfoAction.java
modified: dataservice/app/ggservice/v1/ggmtoolbox/action/UpdateUserTokenInfoAction.java
modified: dataservice/app/ggservice/v1/ggmtoolbox/service/AppDOSInfoService.java
modified: dataservice/app/ggservice/v1/ggmtoolbox/service/UserInfoService.java
modified: dataservice/app/ggservice/v1/graph/action/GetStockPlateComponentAction.java
modified: dataservice/app/ggservice/v1/graph/service/StockPlateService.java
modified: dataservice/app/ggservice/v1/hq/action/GetStockHistoryDynamicAction.java
modified: dataservice/app/ggservice/v1/keybordspirit/action/GetMyGroupStockIndexAction.java

yutao@yutao MINGW64 /d/sts/workspace/ggservice (master_tmp|REVERTING)
$ git commit -m "使用revert 版本号 -m 1或者2同时进行撤销某次提交测试"
[master_tmp 236da00] 使用revert 版本号 -m 1或者2同时进行撤销某次提交测试
95 files changed, 2093 insertions(+), 10011 deletions(-)
delete mode 100644 conf/hanlp.properties
delete mode 100644 dataservice/app/ggservice/common/UserCodeEnum.java
delete mode 100644 dataservice/app/ggservice/v1/email/action/BindEmailGG3Action.java
delete mode 100644 dataservice/app/ggservice/v1/email/action/SendEmailCaptchaGG3Action.java
delete mode 100644 dataservice/app/ggservice/v1/email/service/EmailCaptchaService.java
delete mode 100644 dataservice/app/ggservice/v1/expert/action/GetExpertOfStockAssessAction.java
delete mode 100644 dataservice/app/ggservice/v1/mobile/action/BindMobileGG3Action.java
delete mode 100644 dataservice/app/ggservice/v1/mobile/action/SendMobileCaptchaGG3Action.java
rewrite dataservice/app/ggservice/v1/mystocktags/service/MyStockTagService.java (82%)
delete mode 100644 dataservice/app/ggservice/v1/report/action/GetAuthorRankListAction.java
delete mode 100644 dataservice/app/ggservice/v1/report/action/GetAuthorRecommendReportListAction.java
delete mode 100644 dataservice/app/ggservice/v1/report/action/GetHonoraryAuthorListAction.java
delete mode 100644 dataservice/app/ggservice/v1/report/action/GetHotIndustryListAction.java
delete mode 100644 dataservice/app/ggservice/v1/report/action/GetHotStockListAction.java
delete mode 100644 dataservice/app/ggservice/v1/report/action/GetHotThemeListAction.java
delete mode 100644 dataservice/app/ggservice/v1/report/action/GetOrganRankListAction.java
delete mode 100644 dataservice/app/ggservice/v1/report/condition/AuthorOrganRankCondition.java
delete mode 100644 dataservice/app/ggservice/v1/report/condition/HotReportCondition.java
delete mode 100644 dataservice/app/ggservice/v1/report/service/HotReportService.java
delete mode 100644 dataservice/app/ggservice/v1/usergg/action/AutoLoginAction.java
delete mode 100644 dataservice/app/ggservice/v1/usergg/action/BindOuterChannelAction.java
delete mode 100644 dataservice/app/ggservice/v1/usergg/action/EmailRegisterAction.java
delete mode 100644 dataservice/app/ggservice/v1/usergg/action/GetUserAction.java
delete mode 100644 dataservice/app/ggservice/v1/usergg/action/IsAccountExistAction.java
delete mode 100644 dataservice/app/ggservice/v1/usergg/action/LoginAction.java
delete mode 100644 dataservice/app/ggservice/v1/usergg/action/LogoutAction.java
delete mode 100644 dataservice/app/ggservice/v1/usergg/action/OuterChannelLoginAction.java
delete mode 100644 dataservice/app/ggservice/v1/usergg/action/RegisterAction.java
delete mode 100644 dataservice/app/ggservice/v1/usergg/service/LoginService.java
delete mode 100644 dataservice/app/ggservice/v1/usergg/service/RegisterService.java
delete mode 100644 dataservice/app/ggservice/v1/usergg/service/UserCommonUtils.java
delete mode 100644 dataservice/app/ggservice/v1/usergg/service/UserService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
即:

$ git add -A
$ git commit -m "使用revert 版本号 -m 1或者2同时进行撤销某次提交测试"
1
2
可以看出删除掉了那个人提交的文件。

最后一步
yutao@yutao MINGW64 /d/sts/workspace/ggservice (master_tmp)
$ git push
1
2
这样就把那个人提交错误的代码给删除了,其他人的本地分支也不需要版本回退了!
一次改好,到处OK!

总结
当想撤销中间某次提交时,强烈建议使用revert命令,而不是reset。
git reset –hard commit_id 虽然可以回退远程库,但是其要求pull最新代码的每个人的本地分支都要进行版本回退。这样就增加工作量了!

正确的步骤:

git revert commit_id
//如果commit_id是merge节点的话,-m是指定具体哪个提交点
git revert commit_id -m 1
//接着就是解决冲突
git add -A
git commit -m ".."
git revert commit_id -m 2
//接着就是解决冲突
git add -A
git commit -m ".."
git push
1
2
3
4
5
6
7
8
9
10
11
其中git revert commit_id -m 数字是针对,merge提交点的操作。
如果是普通的提交点,不需要这么麻烦。

参考地址:
[Git高级教程(二)] 远程仓库版本回退方法
https://www.cnblogs.com/ShaYeBlog/p/5368064.html
https://blog.csdn.net/hongchangfirst/article/details/49472913
---------------------
作者:山鬼谣me
来源:CSDN
原文:https://blog.csdn.net/u013066244/article/details/79920012
版权声明:本文为博主原创文章,转载请附上博文链接!

git 优雅的撤销中间某次提交的更多相关文章

  1. git常用命令速查:创建,修改提交,撤销,切换分支,合并分支,变基解决冲突

    创建 $ git init  #在当前目录下创建一个空的本地仓库 $ rm -rf  .git  #删除本地仓库 $ git add .  #把当前目录下的所有文件添加到暂存区 $ git commi ...

  2. git命令--git checkout 之 撤销提交到暂存区的更改

    SYJ@WIN-95I6OG3AT1N /D/gitlab/ihr-kafka-produce (master) $ git status [由于工作区文件被修改了,所以显示为红色] On branc ...

  3. git reset revert 回退回滚取消提交返回上一版本

    git reset revert 回退回滚取消提交返回上一版本 总有一天你会遇到下面的问题. (1)改完代码匆忙提交,上线发现有问题,怎么办? 赶紧回滚. (2)改完代码测试也没有问题,但是上线发现你 ...

  4. Git版本控制:Git查阅、撤销文件修改和撤销文件追踪

    http://blog.csdn.net/pipisorry/article/details/47867097 查看文件的修改历史 git log --pretty=oneline 文件名 # 显示修 ...

  5. git 学习之撤销和删除

    在实际的工作和学习中我们经常的会对文件进行修改,但是或多或少的就会发现由于某些原因修改是错误的这时候就需要对所做的修改进行撤销,更或者某些时候需要对文件进行删除.本节就会告诉大家如何操作. 撤销操作 ...

  6. git 常用的撤销操作

    git 开发时的使用流程,从同事那里学习的, 第一步,先建立一个新的分支,可以在GITLAB或通过命令git checkout -b newbranch 或通过IDE插件 第二步,让项目切换到该分支, ...

  7. 【Git】Git 本地的撤销修改和删除操作

    一:撤销操作 比如我现在在readme.txt文件里面增加一行 内容为555555555555,我们先通过命令查看如下: 在我未提交之前,我发现添加5555555555555内容有误,所以我得马上恢复 ...

  8. git使用cherry-pick和revert抢救错误代码提交

    大多数的新手在新接触git时都会出现这样的问题.代码写完了,提交到dev分支进行测试.一高兴忘记切回来,继续在dev分支开发,写完之后提交时猛的发现,我靠,我怎么在dev上面写代码,此时内心必然是一阵 ...

  9. 在Git中如何撤销上一次的commit

    有的时候我们一不小心就git commit -m ‘commit message info’解决办法,很简单,只需执行git reset HEAD~这条命令即可,即能保证你原本的修改还在,也能撤销本次 ...

随机推荐

  1. Dephi 10.3.3试用报告

    官方没有正式发布,但出了下载及keygen,具体内容我在这篇内容写了:Delphi 10.3.3最新消息 . 也可以去Delphi多层开发交流QQ群:209321818找相关的keygen. 今早来就 ...

  2. Mysql中的锁机制-转载

    原文:http://blog.csdn.net/soonfly/article/details/70238902 锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的 计算资源(如 ...

  3. pip安装超时:Read timed out.

    环境:win10 和 pip 在pip install h5py(或者其他第三方依赖包时) 会出现Read timed out.的问题,即安装超时.如下图所示: 解决方法: 1. 在用户目录下,新建p ...

  4. Spring的使用及Spring3.2控制器增强@ControllerAdvice

    在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component. @Controller.@Service等这些注解的类,则把这 ...

  5. mysql总复习

    目录 数据库操作 库操作 表操作 数据行操作 表关系操作 单表操作 外键创建 多表联查 pymysql模块 索引 主键索引 唯一索引 普通索引 数据库操作 库操作 create database 库名 ...

  6. ValueError: Expecting property name: line 1 column 2 (char 1)

    代码: import json str2 = '{"domain":"456"}' str1 = "{'domain':'123'}" pr ...

  7. MySQL之InnoDB索引面试学习笔记

    写在前面 想要做好后台开发,终究是绕不过索引这一关的.先问自己一个问题,InnoDB为什么选择B+树作为默认索引结构.本文主要参考MySQL索引背后的数据结构及算法原理和剖析Mysql的InnoDB索 ...

  8. Oracle 中文转字母 函数

    CREATE OR REPLACE FUNCTION F_TRANS_PINYIN_CAPITAL(P_NAME IN VARCHAR2) RETURN VARCHAR2 AS V_COMPARE V ...

  9. 获取bin目录

    System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取bin\Debug\目录System.AppDomain.Cur ...

  10. SIGAI深度学习第七集 卷积神经网络1

    讲授卷积神经网络核心思想.卷积层.池化层.全连接层.网络的训练.反向传播算法.随机梯度下降法.AdaGrad算法.RMSProp算法.AdaDelta算法.Adam算法.迁移学习和fine tune等 ...