简单谈谈Git中的回滚操作
首先介绍下场景 首先,一定要想清楚,自己想干什么。 找redis源码作为例子,查看所在的分支 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". 临时切换到某个commit 有可能你并不需要回滚代码,你只想将代码库暂时切换到之前的某个版本,看看那个时候的实现,然后回来继续写之前没有完成的任务。比如想看看945a2f9这个 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. 再执行 * (HEAD detached at 945a2f9) 3.0 3.2 unstable 可以看到处于一个临时的分支上面,如果想以这个分支作为基础开发新的功能,可以创建一个新的分支进行开发。 git checkout -b old-bottle 945a2f9 如果想回到之前的的分支,只要 当然如果在切回以前的 使用 生成一个新的分支用于保存修改的代码 删除未发布的提交 如果确定要删除某几个 可以这样 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之前的 当然如果想保存当前没有提交的代码,可以和之前一样使用stash git stash git reset --hard 945a2f9 git stash pop 注意: reset --hard 一定要慎用!! 这回让你彻底丢失本地的代码(除非有备份过) 删除已发布的提交 假设代码已经提交到远程版本库了,这时应该使用 git revert e9d861e e565632 e1eccf9 或者可以这样做, git revert HEAD~3..HEAD 之后就可以将现在的HEAD push到版本库了。 如果发现之前 总结 以上就是这篇文章的全部内容了,本文只是简单讲了一些我个人的一些操作实践。希望能对大家的学习或者工作带来一定的帮助,如果有疑问大家可以留言交流。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |