1. 程式人生 > >git已經push程式碼到gitlab如何回退

git已經push程式碼到gitlab如何回退

開發經常會遇到程式碼提交後又想回到上一個提交的版本,但是不知道怎麼操作,我也經常忘了需要百度才能搞定,現在寫下這篇文章作為記錄:

已經提交到git但是沒有push的

1、回退到上一個版本,並保留修改記錄,先檢視提交歷史:

$ git log
commit b69a4ced352ec9d5bd9dbf0036a052f9812854fb (HEAD -> master, origin/master)
Author: zhuhualong <[email protected]>
Date:   Thu Oct 12 18:29:53 2017 +0800

    選擇伺服器控制元件修改

commit
eb3378a32d36c03825e444002e541a2d12af274c Author: yangze <[email protected]> Date: Wed Oct 11 10:01:05 2017 +0800 測試主機聯通性工具類 commit bd2a381042868a331730ba549065ed8aaba817e9 Merge: 5c4a2ff f0d82fa Author: yangze <[email protected]> Date: Wed Oct 11 10:00:18 2017 +0800 Merge branch 'master'
of http://172.16.15.19/cop2/cop_task.git commit f0d82fa46a818525cf042a157e0fc889e0c813f6 Author: [email protected] <[email protected]> Date: Tue Oct 10 20:48:56 2017 +0800
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

2、回退到指定的版本處:

$ git reset eb33
Unstaged changes after reset:
M       cop.task.web/src/main/webapp/statics/api/assets/server/serverList.json
M cop.task.web/src/main/webapp/statics/css/app.css M cop.task.web/src/main/webapp/statics/js/app/operation/tpl_task.js M cop.task.web/src/main/webapp/statics/js/config.lazyload.js M cop.task.web/src/main/webapp/statics/js/controllers/selectServerModal.js M cop.task.web/src/main/webapp/statics/tpl/operation/template/selectServerModal.html M cop.task.web/src/main/webapp/statics/tpl/operation/template/targetServer.html
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3、或者回退所有內容到上一個版本 
git  reset  HEAD^ 

這裡寫程式碼片
  • 1

4、#回退test這個檔案的版本到上一個版本      

git  reset  HEAD^  test.py 

這裡寫程式碼片
  • 1

5、將本地的狀態回退到和遠端的一樣      

git  reset  –hard  origin/master 
  • 1

6、回退到上一次提交的狀態,按照某一次的commit完全反向的進行一次commit      

git  revert  HEAD
  • 1

7、將本地狀態回退到某個指定版本,回退前所有的修改丟掉

$ git reset --hard eb33
HEAD is now at eb3378a 測試主機聯通性工具類
  • 1
  • 2
  • 3

已經提交到gitlab如何同步回退gitlab的程式碼

1、git本地回退到指定版本後,按以往的提交順序進行提交時會出現這個問題

$ git push origin master
Username for 'http://172.16.15.19': lbp
To http://172.16.15.19/cop2/cop_task.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'http://172.16.15.19/cop2/cop_task.git'
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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2、這是因為gitlab已經在你提交歷史前面了,你無法把push過的再次push進行覆蓋,這個時候加個引數–force就行

$ git push origin master --force
Username for 'http://172.16.15.19': lbp
Total 0 (delta 0), reused 0 (delta 0)
remote: GitLab: You are not allowed to force push code to a protected branch on this project.
To http://172.16.15.19/cop2/cop_task.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'http://172.16.15.19/cop2/cop_task.git'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3、加完引數後發現gitlab不允許強行push到保護的分支上,解決方法是讓專案的管理員暫時在gitlab上把你要提交的分支設定為不受保護的分支即可

$ git push origin master --force
Username for 'http://172.16.15.19': lbp
Total 0 (delta 0), reused 0 (delta 0)
To http://172.16.15.19/cop2/cop_task.git
 + b69a4ce...eb3378a master -> master (forced update)