環(huán)境
git : 2+
前言
最近兩天,公司的git
合并代碼時,出現(xiàn)了嚴重的問題,浪費很多時間;
現(xiàn)在記錄下;
情況是這樣的,一個同事自己的本地分支(遠程沒有),不知怎么的,有了別人開發(fā)分支的代碼,而他自己又不知道;
其在切換到主分支,并merge
自己的分支,此時其已經(jīng)把別人正在開發(fā)的代碼都合并到了主分支。
到了晚上準備升級時,才發(fā)現(xiàn),主分支的代碼出了問題;此時版本庫是這樣的:
如圖 100047dcc
這一步就有不該有的代碼;
而此時版本庫已經(jīng)提交過了很多次,現(xiàn)在的問題就是,如何撤銷掉100047dcc
提交的代碼,并且保留其他人提交的代碼。
這個問題,折騰到了晚上9點半左右,嘗試了網(wǎng)上給出的:
git rebase -i commit_id
//再通過將pick改為drop
但是,實際的效果是,100047dcc
代碼沒了,其他人提交的代碼也沒有了!
也就是給人感覺和git reset --hard be8c6f6dd
沒有什么區(qū)別!
最后因為太晚,從提交記錄上看,100047dcc
之后就一個人提交了代碼,所以就執(zhí)行了:
//先切一個備份分支
git branch -b master_tmp
//再執(zhí)行
git reset --hard be8c6f6dd
之后,那個人(也就是我)從備份分支上把文件拷貝回來—(因為我是直接在主分支上改的,自己的分支并沒有代碼)。
第二天,我打算去拷貝文件,我執(zhí)行如下操作:
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
簡單的說,我的操作就是兩步:
1git pull
2、git push
結(jié)果又把代碼還原回去了!
為啥呢?
雖然昨天晚上,把遠程庫的版本回退到了正確的版本,但是我的本地主分支還是最新的commit,也就是說,相比遠程庫,我本地庫是超前了多次提交,畢竟因為遠程庫回退了嘛!
這個時候,我必須也得對本地庫進行回退,回退到線上相同的commit
節(jié)點才行。
這個時候,我做了以下幾個操作:
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
即可。
git reset --hard 384deccaa6
接下來,我開始復制粘貼,從備份分支上,把代碼拷貝下。
真的操蛋,這等于是增加工作量啊!
revert 撤銷某次提交
到了下午,又有個同事干了類似我上午的操作。把不該有的代碼提交上去了!
這就麻煩了,雖然遠程庫回退了!結(jié)果是要求凡是pull
最新代碼的人,都得進行本地回退的操作。
否則,就沒完沒了!
到了晚上,對著備份分支進行測試,終于找到了優(yōu)雅的解決辦法!
這就是revert
命令
該命令就是為撤銷某次提交而存在的;
首先,我是明白100047dcc
這次提交是有問題的,這是問題的源頭;
也就是說,只要我們把這次提交的給撤銷了,就不會有問題了!
步驟 一
$ git revert 100047dcc
error: Commit 100047dccb58f3ee5e27b0dfaf5c02ac91dc2c73 is a merge but no -m option was given.
fatal: revert failed
結(jié)果報錯了,報了一個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'
我執(zhí)行了這樣的一個操作:
git revert 100047dcc -m 1
參數(shù) -m 就是指定要撤銷的那個提價,從左往右,從1開始數(shù);也就是我撤銷的是ca4a7ff999
。
接著其把代碼沖突,然后我就解決沖突,保留主分支的代碼,去掉那個人的代碼。
解決完沖突后,我執(zhí)行如下操作:
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í)行的語句其實就是:
$ git add -A
$ git commit -m "ceshi"
步驟三
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í)行:
$ git revert 100047dcc -m 2
即 撤銷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
即:
$ git add -A
$ git commit -m "使用revert 版本號 -m 1或者2同時進行撤銷某次提交測試"
可以看出刪除掉了那個人提交的文件。
最后一步
yutao@yutao MINGW64 /d/sts/workspace/ggservice (master_tmp)
$ git push
這樣就把那個人提交錯誤的代碼給刪除了,其他人的本地分支也不需要版本回退了!
一次改好,到處OK!
總結(jié)
當想撤銷中間某次提交時,強烈建議使用revert
命令,而不是reset
。
git reset –hard commit_id 雖然可以回退遠程庫,但是其要求pull
最新代碼的每個人的本地分支都要進行版本回退。這樣就增加工作量了!
正確的步驟:
git revert commit_id
//如果commit_id是merge節(jié)點的話,-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 數(shù)字
是針對,merge
提交點的操作。
如果是普通的提交點,不需要這么麻煩。
參考地址:
[Git高級教程(二)] 遠程倉庫版本回退方法
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- git rebase 成功之后撤銷的操作方法
- Git撤銷&回滾操作(git reset 和 get revert)
- git如何撤銷commit的方法(未push)
- Git使用小技巧之回滾與撤銷詳解
- 基于Git的常用撤銷技巧與解決沖突方法(推薦)
- Git 撤銷操作、刪除文件和恢復文件
- Git里多種撤銷操作的最佳方法