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