環境
git : 2+
前言
最近兩天,公司的git
合并代碼時,出現了嚴重的問題,浪費很多時間;
現在記錄下;
情況是這樣的,一個同事自己的本地分支(遠程沒有),不知怎么的,有了別人開發分支的代碼,而他自己又不知道;
其在切換到主分支,并merge
自己的分支,此時其已經把別人正在開發的代碼都合并到了主分支。
到了晚上準備升級時,才發現,主分支的代碼出了問題;此時版本庫是這樣的:
如圖 100047dcc
這一步就有不該有的代碼;
而此時版本庫已經提交過了很多次,現在的問題就是,如何撤銷掉100047dcc
提交的代碼,并且保留其他人提交的代碼。
這個問題,折騰到了晚上9點半左右,嘗試了網上給出的:
1
2
|
git rebase -i commit_id // 再通過將pick改為drop |
但是,實際的效果是,100047dcc
代碼沒了,其他人提交的代碼也沒有了!
也就是給人感覺和git reset --hard be8c6f6dd
沒有什么區別!
最后因為太晚,從提交記錄上看,100047dcc
之后就一個人提交了代碼,所以就執行了:
1
2
3
4
|
// 先切一個備份分支 git branch -b master_tmp // 再執行 git reset --hard be8c6f6dd |
之后,那個人(也就是我)從備份分支上把文件拷貝回來—(因為我是直接在主分支上改的,自己的分支并沒有代碼)。
第二天,我打算去拷貝文件,我執行如下操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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) 384decc..1784b12 master -> master |
簡單的說,我的操作就是兩步:
1git pull
2、git push
結果又把代碼還原回去了!
為啥呢?
雖然昨天晚上,把遠程庫的版本回退到了正確的版本,但是我的本地主分支還是最新的commit,也就是說,相比遠程庫,我本地庫是超前了多次提交,畢竟因為遠程庫回退了嘛!
這個時候,我必須也得對本地庫進行回退,回退到線上相同的commit
節點才行。
這個時候,我做了以下幾個操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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 |
上面敲了很多命令,其實真正只需要git reset --hard 384deccaa6
即可。
1
|
git reset --hard 384deccaa6 |
接下來,我開始復制粘貼,從備份分支上,把代碼拷貝下。
真的操蛋,這等于是增加工作量啊!
revert 撤銷某次提交
到了下午,又有個同事干了類似我上午的操作。把不該有的代碼提交上去了!
這就麻煩了,雖然遠程庫回退了!結果是要求凡是pull
最新代碼的人,都得進行本地回退的操作。
否則,就沒完沒了!
到了晚上,對著備份分支進行測試,終于找到了優雅的解決辦法!
這就是revert
命令
該命令就是為撤銷某次提交而存在的;
首先,我是明白100047dcc
這次提交是有問題的,這是問題的源頭;
也就是說,只要我們把這次提交的給撤銷了,就不會有問題了!
步驟 一
1
2
3
|
$ git revert 100047dcc error: Commit 100047dccb58f3ee5e27b0dfaf5c02ac91dc2c73 is a merge but no -m option was given. fatal: revert failed |
結果報錯了,報了一個Commit is a merge but no -m option was given.
為什么呢?
如果100047dcc
這只是一個普通的提交,其實是不會報錯的!
但是,這是一個merge
的提交。
那么在撤銷時,git
并不知道我要撤銷具體哪次!如下圖:
這個時候,怎么辦呢?
我的做法
步驟二
1
2
3
4
5
6
|
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
|
git revert 100047dcc -m 1 |
參數 -m 就是指定要撤銷的那個提價,從左往右,從1開始數;也就是我撤銷的是ca4a7ff999
。
接著其把代碼沖突,然后我就解決沖突,保留主分支的代碼,去掉那個人的代碼。
解決完沖突后,我執行如下操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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
|
$ git add -A $ git commit -m "ceshi" |
步驟三
1
2
3
4
5
6
|
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
|
$ git revert 100047dcc -m 2 |
即 撤銷be8c6f6dde
的提交,這個時候也會提示代碼沖突了,
接著和上面一樣,解決沖突,在提交:
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
|
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
|
$ git add -A $ git commit -m "使用revert 版本號 -m 1或者2同時進行撤銷某次提交測試" |
可以看出刪除掉了那個人提交的文件。
最后一步
1
2
|
yutao@yutao MINGW64 /d/sts/workspace/ggservice (master_tmp) $ git push |
這樣就把那個人提交錯誤的代碼給刪除了,其他人的本地分支也不需要版本回退了!
一次改好,到處OK!
總結
當想撤銷中間某次提交時,強烈建議使用revert
命令,而不是reset
。
git reset –hard commit_id 雖然可以回退遠程庫,但是其要求pull
最新代碼的每個人的本地分支都要進行版本回退。這樣就增加工作量了!
正確的步驟:
1
2
3
4
5
6
7
8
9
10
11
|
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 |
其中git revert commit_id -m 數字
是針對,merge
提交點的操作。
如果是普通的提交點,不需要這么麻煩。
參考地址:
[Git高級教程(二)] 遠程倉庫版本回退方法
https://www.cnblogs.com/ShaYeBlog/p/5368064.html
https://blog.csdn.net/hongchangfirst/article/details/49472913
到此這篇關于git 優雅的撤銷中間某次提交方法的文章就介紹到這了,更多相關git 撤銷某次提交內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/u013066244/article/details/79920012