简单谈谈Git中的回滚操作
首先,一定要想清楚,自己想干什么。 找redis源码作为例子,查看所在的分支git branch 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这个commit,执行操作 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. 再执行git branch * (HEAD detached at 945a2f9) 3.0 3.2 unstable 可以看到处于一个临时的分支上面,如果想以这个分支作为基础开发新的功能,可以创建一个新的分支进行开发。 git checkout -b old-bottle 945a2f9 如果想回到之前的的分支,只要checkout回去就好了。 当然如果在切回以前的commit的之前已经改了一些代码,可能不会那么顺利地切到之前的commit,可以 使用reset将修改了的代码丢弃 stash->checkout->stash pop将修改的代码找回来 生成一个新的分支用于保存修改的代码 删除未发布的提交 如果确定要删除某几个commit,并且那几个commit的代码还没有提交到远程版本库里的话。 可以这样 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之前的commit记录了。 当然如果想保存当前没有提交的代码,可以和之前一样使用stash git stash git reset --hard 945a2f9 git stash pop 注意: reset --hard 一定要慎用!! 这回让你彻底丢失本地的代码(除非有备份过) 删除已发布的提交 假设代码已经提交到远程版本库了,这时应该使用revert命令,revert命令实际是提交一个新的commit来回滚之前的commit。还是以945a2f9为例,现在想让代码会滚到945a2f9的状态,那么需要revert掉之前的commit git revert e9d861e e565632 e1eccf9 或者可以这样做,revert掉HEAD的前三个commit git revert HEAD~3..HEAD 之后就可以将现在的HEAD push到版本库了。 如果发现之前revert操作错了,可以再执行一次revert来取消上次的revert。 (编辑:焦作站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |