git commit撤销_Git 实用操作:撤销 Commit 提交
有的时候,改完代码提交 commit 后发现写得实在太烂了,连自己的都看不下去,与其修改它还不如丢弃重写。怎么操作呢?
使用 reset 撤销
如果是最近提交的 commit 要丢弃重写可以用 reset 来操作。比如你刚写了一个 commit:
写完回头看了看,你觉得不行这得重新写。那么你可以用 reset --hard 来撤销这条 commit。
git reset --hard HEAD^
HEAD^ 表示往回数一个位置的 commit`,上篇刚说过。
因为你要撤销最新的一个 commit,所以你需要恢复到它的父 commit ,也就是 HEAD^。那么在这行之后,你要丢弃的最新一条就被撤销了:
不过,就像图上显示的,你被撤销的那条提交并没有消失,只是你不再用到它了。如果你在撤销它之前记下了它的 SHA-1 码,那么你还可以通过 SHA-1 来找到他它。
使用 rebase -i 撤销
假如有一个 commit,你在刚把它写完的时候并没有觉得它不好,可是在之后又写了几个提交以后,你突然灵光一现:哎呀,那个 commit 不该写,我要撤销!
不是最新的提交,就不能用 reset --hard 来撤销了。这种情况的撤销,就要用之前介绍过的一个指令交互式变基:rebase -i。
之前介绍过,交互式变基可以用来修改某些旧的 commit。其实除了修改提交,它还可以用于撤销提交。比如下面这种情况:
你想撤销倒数第二条 commit,那么可以使用 rebase -i:
git rebase -i HEAD^^
Git 引导到选择要操作的 commit 页面:
pick 310154e 第 N-2 次提交pick a5f4a0d 第 N-1 次提交# Rebase 710f0f8..a5f4a0d onto 710f0f8## Commands:# p, pick = use commit# r, reword = use commit, but edit the commit message# e, edit = use commit, but stop for amending# s, squash = use commit, but meld into previous commit# f, fixup = like "squash", but discard this commit's log message# x, exec = run command (the rest of the line) using shell# b, break = stop here (continue rebase later with 'git rebase --continue')# d, drop = remove commit...
在上篇中,讲到要修改哪个 commit 就把哪个 commit 前面的 pick 改成 edit。而如果你要撤销某个 commit ,做法就更加简单粗暴一点:直接删掉这一行就好(使用 d 命令)。
pick a5f4a0d 第 N-1 次提交# Rebase 710f0f8..a5f4a0d onto 710f0f8## Commands:# p, pick = use commit# r, reword = use commit, but edit the commit message# e, edit = use commit, but stop for amending# s, squash = use commit, but meld into previous commit# f, fixup = like "squash", but discard this commit's log message# x, exec = run command (the rest of the line) using shell# b, break = stop here (continue rebase later with 'git rebase --continue')# d, drop = remove commit...
把这一行删掉就相当于在 rebase 的过程中跳过了这个 commit,从而也就把这个 commit 丢弃了。
如果你通过 git log 查看,就会发现之前的倒数第二条 commit 已经不在了。
使用用 rebase --onto 撤销
除了用交互式 rebase,你还可以用 rebase --onto 来更简便地撤销提交。
rebase 加上 --onto 选项之后,可以指定 rebase 的「起点」。一般的 rebase, 的「起点」是自动选取的,选取的是当前 commit 和目标 commit 在历史上的交叉点。
例如下面这种情况:
如果在这里执行:
git rebase 第3个commit
那么 Git 会自动选取 3 和 5 的历史交叉点 2 作为 rebase 的起点,依次将 4 和 5 重新提交到3 的路径上去。
而 --onto 参数,就可以额外给 rebase 指定它的起点。例如同样以上图为例,如果我只想把 5提交到 3 上,不想附带上 4,那么我可以执行:
git rebase --onto 第3个commit 第4个commit branch1
选项 --onto 参数后面有三个附加参数:目标 commit、起点 commit(注意:rebase 的时候会把起点排除在外)、终点 commit。所以上面这行指令就会从 4 往下数,拿到 branch1 所指向的5,然后把 5 重新提交到 3 上去。
同样的,你也可以用 rebase --onto 来撤销提交:
git rebase --onto HEAD^^ HEAD^ branch1
上面这行代码的意思是:以倒数第二个 commit 为起点(起点不包含在 rebase 序列里),branch1 为终点,rebase 到倒数第三个 commit 上。
也就是这样:
总结
撤销最近一次的 commit 直接使用 reset --hard,撤销过往历史提交。方法有两种:
- 用 git rebase -i 在编辑界面中删除想撤销的 commit
- 用 git rebase --onto 在 rebase 命令中直接剔除想撤销的 commit
这有两种理念是一样的,即在 rebase 的过程中去掉想撤销的 commit,让它消失在历史中。
git commit撤销_Git 实用操作:撤销 Commit 提交相关推荐
- Git 实用操作 | 撤销 Commit 提交
有的时候,改完代码提交 commit 后发现写得实在太烂了,连自己的都看不下去,与其修改它还不如丢弃重写.怎么操作呢? 使用 reset 撤销 如果是最近提交的 commit 要丢弃重写可以用 res ...
- git的基本使用命令操作
Linux操作命令行: mkdir - 创建文件夹, cd - 切换文件路径 pwd - 显示文件路径 ls -ah - 可以查看隐藏的文件夹名(.git) c ...
- git revert 之后怎么撤销_git撤销操作
git的撤销操作:reset.checkout和revert 这三个命令都可以用于撤销. reset和checkout可以作用于commit或者文件,revert只能作用于commit. 工作区有改动 ...
- Git撤销对远程仓库的push 或 Git撤销对远程仓库的commit提交
Git撤销对远程仓库的push 或 Git撤销对远程仓库的commit提交 [一]撤销push 执行 git log 查看日志,获取需要回退的版本号 执行 git reset –-soft <版 ...
- git 撤销全部的commit_Git 撤销 Commit
1.第一种情况:还没有push,只是在本地commit git reset --soft|--mixed|--hard git push develop develop --force (本地分支和远 ...
- git revert 之后怎么撤销_Git 如何优雅地回退代码
点击上方"Java知音",选择"置顶公众号" 技术文章第一时间送达! 作者:枕边书 cnblogs.com/zhenbianshu/p/12018714.htm ...
- Git:撤销回滚操作
Git撤销&回滚操作(git reset 和 get revert) git的工作流 工作区:即自己当前分支所修改的代码,git add xx 之前的!不包括 git add xx 和 git ...
- Git——撤销和删除操作【git restore / git rm 】
前言:针对文件的撤销修改或者删除文件操作一般有以下几种情况: 1. 撤销文件修改 1.1 git restore – filename - 工作区文件改动未添加到暂存区 当修改后的文件还未放到暂存 ...
- git撤回已经commit未push的操作
一.使用IntelliJ idea VSC => Git => reset head => 退回到上次commit => 退回到第2次提交之前 => 退回到指定commi ...
最新文章
- 解决redhat的未注册问题
- 游戏《蔚蓝山》教我的编程道理
- 【机器学习】SVM线性可分
- [转] 在 Mac OS X 下编译 Objective-C 运行时
- 移动5G套餐价格被工作人员曝光了?这价格没让我失望!
- OpenShift 4 - 使用ArgoCD Operator
- Form各键盘触发子所对应的“按键”
- Cookie和Session的关系(个人理解)
- ikbc机械键盘打字出现重复_入手第一把机械键盘,打字打到上瘾——ikbc 新Poker键盘 体验...
- linux调时区北京,Linux修改系统时间和中国时区
- c语言变量表达式计算器,C语言表达式计算器~
- SNF快速开发平台--规则引擎介绍和使用文档
- 葫芦书笔记----CNN和RNN
- 国内十大优质黄金期货交易平台排名榜单(最新版一览)
- 最长的英文单词十大排行榜
- 国产化服务器兼容系统,必须兼容中国芯,国产操作系统再迎来发展机遇?
- SOX的一些命令和kaldi使用sox音频数据增强
- 【转载】基于Office Online Server 2016 的office在线编辑
- 金庸不朽,将在区块链世界中永生
- 【译】如何提高工作效率——HOWTO: Be more productive