git代码库回滚

git代码库回滚: 指的是将代码库某分支退回到以前的某个commit id

【本地代码库回滚】:

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

3、git branch the_branch_backup //备份一下这个分支当前的情况

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

5、git push origin :the_branch //删除远程 the_branch

6、git push origin the_branch //用回滚后的本地分支重新建立远程分支

7、git push origin :the_branch_backup //如果前面都成功了,删除这个备份分支

如果使用了gerrit做远程代码中心库和code review平台,需要确保操作git的用户具备分支的push权限,并且选择了 Force Push选项(在push权限设置里有这个选项)

另外,gerrit中心库是个bare库,将HEAD默认指向了master,因此master分支是不能进行删除操作的,最好不要选择删除master分支的策略,换用其他分支。如果一定要这样做,可以考虑到gerrit服务器上修改HEAD指针。。。不建议这样搞

Deleting your master branch

You’ve forked some project on github.

You clone your fork.

Now you’ve got a master branch. It’s the master branch of your fork.

It can be tempting to do work in the master branch and ask for a pull request.

That is best avoided because:

  • It’s natural to carry on working in your master and that will pollute your pull request with other commits
  • master is not a descriptive name for a set of changes. It’s easier for the person reviewing your code to have something like fix-for-issue-12.
  • You may well also have a tracking branch pointing to the main upstream repo. You might call that something like upstream-master. It’s easy to lose concentration and forget you are on upstream-master instead of your master, and nasty errors can result.

To avoid this, I delete the master branch from my forked copy. However, to make that work, you have to tell github not to monitor yourmaster branch.

How to delete master

First you delete master in your local clone. To do this we first make a new branch called placeholder or similar, and delete master from there:

git branch placeholder
git checkout placeholder
git branch -D master

All good so far. We next want to delete the branch on github. However, if we do this the naive way:

git push origin :master

we just get an error like this:

remote: error: refusing to delete the current branch: refs/heads/master
To git@github.com:matthew-brett/datarray.git
! [remote rejected] master (deletion of the current branch prohibited)
error: failed to push some refs to 'git@github.com:matthew-brett/datarray.git'

That is because github is looking at the master branch to provide the web content when you browse that repository. So we first have to make github look at our placeholder branch instead, then delete master.

First push up the placeholder branch:

git checkout placeholder # if not on placeholder already
git push origin placeholder

Then set placeholder to be the github default branch. Go to the main github page for your forked repository, and click on the “Admin” button.

There’s a “Default branch” dropdown list near the top of the screen. From there, select placeholder. On the interface I’m looking at, a green tick appears above the dropdown list. Now you can do (from the command line):

git push origin :master

and - no master branch...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值