Git遠端庫版本回滾

張善友發表於2017-11-28

在git的一般使用中,如果發現錯誤的將不想staging的檔案add進入index之後,想回退取消,這就叫做git程式碼庫回滾: 指的是將程式碼庫某分支退回到以前的某個commit id。可以使用命令:git reset HEAD <file>...,同時git add完畢之後,git也會做相應的提示,Git reset 是Git最常用的命令之一,也是最危險最容易誤用的命令。 用法參考 Git學習筆記03--git reset

【原生程式碼庫回滾】:

git reset --hard commit-id :回滾到commit-id,講commit-id之後提交的commit都去除

git reset --hard HEAD~3:將最近3次的提交回滾

【遠端程式碼庫回滾】:

這個是重點要說的內容,過程比本地回滾要複雜

應用場景:自動部署系統釋出後發現問題,需要回滾到某一個commit,再重新發布

原理:先將本地分支退回到某個commit,刪除遠端分支,再重新push本地分支

操作步驟:

1、git checkout the_branch

2、git pull

D:\FitProject\NPS>git pull
remote: Counting objects: 307, done
remote: Finding sources: 100% (245/245)
remote: Getting sizes: 100% (134/134)
remote: Compressing objects: 100% (31123/31123)
Rremote: Total 245 (delta 156), reused 242 (delta 154)
Receiving objects: 100% (245/245), 22.67 KiB | 0 bytes/s, done.
Resolving deltas: 100% (156/156), completed with 40 local objects.
From xxxxxxxxxxxx
    fe0d037..d159d4d  stable_chh_1127 -> origin/stable_chh_1127
Already up-to-date.

3、git branch the_branch_backup //備份一下這個分支當前的情況

D:\FitProject\NPS>git branch Geffdev_0926_backup

4、git reset --hard the_commit_id //把the_branch本地回滾到the_commit_id

D:\FitProject\NPS>git reset --hard 56f7c0d56befd4cad99a6017062824e073b56c01
HEAD is now at 56f7c0d 封裝付款體現Relay介面

5、git push origin :the_branch //刪除遠端 the_branch

6、git push origin the_branch //用回滾後的本地分支重新建立遠端分支

D:\FitProject\NPS>git push origin : Geffdev_0926
To xxxxxx
  ! [rejected]        Geffdev_0926 -> Geffdev_0926 (non-fast-forward)
  ! [rejected]        develop -> develop (non-fast-forward)
  ! [rejected]        master -> master (non-fast-forward)
  ! [rejected]        stable_chh_1127 -> stable_chh_1127 (non-fast-forward)
error: failed to push some refs to xxxxxx
hint: Updates were rejected because the tip of your current branch is behind
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.

D:\FitProject\NPS>git push origin  Geffdev_0926
Total 0 (delta 0), reused 0 (delta 0)
remote: Updating references: 100% (1/1)
To xxxxxx
  * [new branch]      Geffdev_0926 -> Geffdev_0926

7、git push origin :the_branch_backup //如果前面都成功了,刪除這個備份分支

相關文章