環境
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
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!