git如何正確回滾代碼
git如何正確回滾代碼
方法一,刪除遠程分支再提交
①首先兩步保證當前工作區是干凈的,并且和遠程分支代碼一致
$ git co currentBranch $ git pull origin currentBranch $ git co ./
②備份當前分支(如有必要)
$ git branch currentBranchBackUp
③恢復到指定的commit hash
$ git reset --hard resetVersionHash //將當前branch的HEAD指針指向commit hash
④刪除當前分支的遠程分支
$ git push origin :currentBranch $ //或者這么寫git push origin --delete currentBranch
⑤把當前分支提交到遠程
$ git push origin currentBranch
方法二,強制push遠程分支
①首先兩步保證當前工作區是干凈的,并且和遠程分支代碼一致
②備份當前分支(如有必要)
③恢復到指定的commit hash
$ git reset --hard resetVersionHash
④把當前分支強制提交到遠程
$ git push -f origin currentBranch
方法三,從回滾位置生成新的commit hash
①首先兩步保證當前工作區是干凈的,并且和遠程分支代碼一致
②備份當前分支(如有必要)
③使用git revert恢復到指定的commit hash,當前分支恢復到a>3版本(見下圖)
a)此方法會產生一條多余的commit hash&log,其實1c0ce98和01592eb內容上是一致的
b)git revert是以要回滾的commit hash(1c0ce98)為基礎,新生成一個commit hash(01592eb)
$ git revert resetVersionHash
④提交遠程分支
$ git push origin currentBranch
方法四,從回滾位置生成新的分支merge
①首先兩步保證當前工作區是干凈的,并且和遠程分支代碼一致
②備份當前分支(如有必要)
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!