源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

简单谈谈Git中的回滚操作

  • 时间:2021-04-20 16:45 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:简单谈谈Git中的回滚操作
[b]首先介绍下场景[/b] 首先,一定要想清楚,自己想干什么。 找redis源码作为例子,查看所在的分支[code]git branch[/code]
 3.0
 3.2
* unstable
取前5条commit看看
git log --pretty=format:"%h - %an, %ar : %s" -5
e9d861e - antirez, 27 hours ago : Clear child data when opening the pipes.
e565632 - antirez, 27 hours ago : Child -> Parent pipe for COW info transferring.
e1eccf9 - antirez, 31 hours ago : zmalloc: Make fp var non local to fix build.
945a2f9 - antirez, 31 hours ago : zmalloc: zmalloc_get_smap_bytes_by_field() modified to work for any PID.
b13759e - antirez, 31 hours ago : redis-cli: "allocator-stats" -> "malloc-stats".
[b]临时切换到某个commit[/b] 有可能你并不需要回滚代码,你只想将代码库暂时切换到之前的某个版本,看看那个时候的实现,然后回来继续写之前没有完成的任务。比如想看看945a2f9这个[code]commit[/code],执行操作
git checkout 945a2f9
Note: checking out '945a2f9'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

 git checkout -b <new-branch-name>

HEAD is now at 945a2f9... zmalloc: zmalloc_get_smap_bytes_by_field() modified to work for any PID.
再执行[code]git branch[/code]
* (HEAD detached at 945a2f9)
 3.0
 3.2
 unstable
可以看到处于一个临时的分支上面,如果想以这个分支作为基础开发新的功能,可以创建一个新的分支进行开发。
git checkout -b old-bottle 945a2f9
如果想回到之前的的分支,只要[code]checkout[/code]回去就好了。 当然如果在切回以前的[code]commit[/code]的之前已经改了一些代码,可能不会那么顺利地切到之前的[code]commit[/code],可以      使用[code]reset[/code]将修改了的代码丢弃      [code]stash->checkout->stash pop[/code]将修改的代码找回来      生成一个新的分支用于保存修改的代码 [b]删除未发布的提交[/b] 如果确定要删除某几个[code]commit[/code],并且那几个[code]commit[/code]的代码还没有提交到远程版本库里的话。 可以这样
git reset --hard 945a2f9

HEAD is now at 945a2f9 zmalloc: zmalloc_get_smap_bytes_by_field() modified to work for any PID.
再查看git log,就已经看不到945a2f9之前的[code]commit[/code]记录了。 当然如果想保存当前没有提交的代码,可以和之前一样使用stash
git stash
git reset --hard 945a2f9
git stash pop
[b]注意:[/b] [b]reset --hard 一定要慎用!! [/b] [b]这回让你彻底丢失本地的代码(除非有备份过)[/b] [b]删除已发布的提交[/b] 假设代码已经提交到远程版本库了,这时应该使用[code]revert[/code]命令,[code]revert[/code]命令实际是提交一个新的[code]commit[/code]来回滚之前的[code]commit[/code]。还是以945a2f9为例,现在想让代码会滚到945a2f9的状态,那么需要[code]revert[/code]掉之前的[code]commit[/code]
git revert e9d861e e565632 e1eccf9
或者可以这样做,[code]revert[/code]掉HEAD的前三个[code]commit[/code]
git revert HEAD~3..HEAD
之后就可以将现在的HEAD push到版本库了。 如果发现之前[code]revert[/code]操作错了,可以再执行一次[code]revert[/code]来取消上次的[code]revert[/code]。 [b]总结[/b] 以上就是这篇文章的全部内容了,本文只是简单讲了一些我个人的一些操作实践。希望能对大家的学习或者工作带来一定的帮助,如果有疑问大家可以留言交流。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部