git版本回退的一個(gè)問(wèn)題
來(lái)源:程序員人生 發(fā)布時(shí)間:2015-05-15 08:24:22 閱讀次數(shù):4157次
有時(shí)候我們想在本地修改遠(yuǎn)程已提交的某個(gè)歷史,如果我們?cè)诒镜匦薷囊院螅缓髉ush會(huì)有問(wèn)題,比如說(shuō)我們先看1下現(xiàn)在的狀態(tài):
$ git status
On branch dev
Your branch is behind 'origin/dev' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
但是我們將改動(dòng)的版本推送到
服務(wù)器上的時(shí)候,會(huì)報(bào)錯(cuò)以下:
$ git push origin dev
root@192.168.1.180's password:
To ssh://root@192.168.1.180:/home/test_web.git
! [rejected] dev -> dev (non-fast-forward)
error: failed to push some refs to 'ssh://root@192.168.1.180:/home/test_web.git'
hint: Updates were rejected because the tip of your current branch is behin
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
這里的主要緣由就是由于已push到遠(yuǎn)程的提交是不能回退提交的,也就是我們不能單獨(dú)抽出某個(gè)提交進(jìn)行修改,但是此時(shí)我們可使用git push -f 來(lái)履行。
我們可以操作以下:
(1)先將遠(yuǎn)程服務(wù)器上最新的代碼clone到本地,并建立dev分支。
(2)把返回歷史的提交和以后所有提交的commit ID的順序都記錄下來(lái)。
(3)然后我們回退到某個(gè)點(diǎn),也就是git reset SHA --hard
(4)進(jìn)行文件修改并且對(duì)返回的提交進(jìn)行修改:modify,git add,git commit
(5)用之前記錄下來(lái)的commit ID反向cherry pick,目標(biāo)提交后有多少提交都需要使用到。
(6)對(duì)當(dāng)前分支進(jìn)行push,替換到服務(wù)器上的遠(yuǎn)程分支,需要加-f參數(shù)。
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)