git版本回退的一個問題
來源:程序員人生 發布時間:2015-05-15 08:24:22 閱讀次數:4153次
有時候我們想在本地修改遠程已提交的某個歷史,如果我們在本地修改以后,然后push會有問題,比如說我們先看1下現在的狀態:
$ 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)
但是我們將改動的版本推送到
服務器上的時候,會報錯以下:
$ 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到遠程的提交是不能回退提交的,也就是我們不能單獨抽出某個提交進行修改,但是此時我們可使用git push -f 來履行。
我們可以操作以下:
(1)先將遠程服務器上最新的代碼clone到本地,并建立dev分支。
(2)把返回歷史的提交和以后所有提交的commit ID的順序都記錄下來。
(3)然后我們回退到某個點,也就是git reset SHA --hard
(4)進行文件修改并且對返回的提交進行修改:modify,git add,git commit
(5)用之前記錄下來的commit ID反向cherry pick,目標提交后有多少提交都需要使用到。
(6)對當前分支進行push,替換到服務器上的遠程分支,需要加-f參數。
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈