git已经push代码到gitlab如何回退

开发经常会遇到代码提交后又想回到上一个提交的版本,但是不知道怎么操作,我也经常忘了需要百度才能搞定,现在写下这篇文章作为记录:

已经提交到git但是没有push的

1、回退到上一个版本,并保留修改记录,先查看提交历史:

$ git log
commit b69a4ced352ec9d5bd9dbf0036a052f9812854fb (HEAD -> master, origin/master)
Author: zhuhualong <zhuhualong@beyondcent.com>
Date:   Thu Oct 12 18:29:53 2017 +0800

    选择服务器控件修改

commit eb3378a32d36c03825e444002e541a2d12af274c
Author: yangze <yangze@beyongcent.com>
Date:   Wed Oct 11 10:01:05 2017 +0800

    测试主机联通性工具类

commit bd2a381042868a331730ba549065ed8aaba817e9
Merge: 5c4a2ff f0d82fa
Author: yangze <yangze@beyongcent.com>
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: wangxi@beyondcent.com <wangxi@beyondcent.com>
Date:   Tue Oct 10 20:48:56 2017 +0800

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

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

这里写代码片

4、#回退test这个文件的版本到上一个版本      

git  reset  HEAD^  test.py 

这里写代码片

5、将本地的状态回退到和远程的一样      

git  reset  –hard  origin/master 

6、回退到上一次提交的状态,按照某一次的commit完全反向的进行一次commit      

git  revert  HEAD

7、将本地状态回退到某个指定版本,回退前所有的修改丢掉

$ git reset --hard eb33
HEAD is now at eb3378a 测试主机联通性工具类

已经提交到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.

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'

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)
  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在GitLab中进行版本回退,你可以使用git reset命令。在GitLab中,你可以使用以下步骤来回退版本: 1. 首先,你需要找到你要回退到的目标版本的commit ID。你可以使用git log命令或在GitLab提交历史中查找该ID。 2. 打开Git Bash或进入git cmd页面。你可以通过鼠标右键选中【Git Bash Here】进入git cmd页面。 3. 在命令行中使用git reset命令,加上--hard参数,后面跟着你要回退到的commit ID。例如,git reset --hard 46b66217d92af8c64bcd1d796fe67695022c9d54。 4. 执行命令后,GitLab回退到指定的commit版本,同时会丢弃该commit之后的所有修改。 需要注意的是,这个操作是不可逆的,所以请谨慎使用。在回退版本之前,请确保你已经备份了重要的代码或文件。 希望这可以帮助你进行GitLab版本的回退。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [gitlab 回退到指定版本](https://blog.csdn.net/yayayu_/article/details/117774849)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [gitlab回退操作](https://blog.csdn.net/weixin_44909514/article/details/123899764)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值