git rebase修改历史提交内容
2024-10-14 13:55:10
简述
git提交历史中有一次提交的内容是有问题,因为每隔一段时间就要发一次版本,所以必须修改这次提交的内容,以便其不影响已经发布的版本。
大概是这样子的
A --- B ---- C ---- D ---- E ----- F ------
| \ \
有问题 \-----发布 \---- 发布
所以这里需要修改C这次提交的内容。
解决过程
相关的操作可以参考7.6 Git 工具 - 重写历史
这里我创建了一个新的仓库,用来描述解决这个问题的过程。
1、先看一下提交记录
$ git log
commit aa3f6b723abf030b1692f9b573092ec782600d91
Author: solym <solym@sohu.com>
Date: Sat Sep 29 14:34:36 2018 +0800
第三次提交
commit e186c75c5431a6eb683d4640ac30c4b8900ba0c1
Author: solym <solym@sohu.com>
Date: Sat Sep 29 14:34:11 2018 +0800
第二次提交
commit ebcd3120d30c52125593601f296607c5dcc520a3
Author: solym <solym@sohu.com>
Date: Sat Sep 29 14:33:48 2018 +0800
第一次提交
这里假设是第二次提交
的内容有问题,所以需要会到e186c75c5431a6eb683d4640ac30c4b8900ba0c1
这个提交记录之前一次提交的位置来解决这个问题。
2、先将当前的修改用stash存储一下,后面解决完之后再释放出来
$ git stash
Saved working directory and index state WIP on master: aa3f6b7 第三次提交
HEAD is now at aa3f6b7 第三次提交
3、将 HEAD 通过rebase回退到有问题的位置前
git rebase e186c75c5431a6eb683d4640ac30c4b8900ba0c1^ --interactive
warning: stopped at e186c75... 第二次提交
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
4、在出来的编辑界面,将有问题的提交前的pick
改为edit
,然后保存退出
# 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 类似于squash,但是丢弃此提交的日志消息
# x, exec = run command (the rest of the line) using shell 运行shell命令
# d,drop = remove commit 删除提交
5、修改有问题的文件,解决后重新提交。注意提交使用的参数是--amend
$ vim A.cpp
$ git add A.cpp
$ git commit --amend
[detached HEAD 8b4daa5] 第二次提交
Date: Sat Sep 29 14:34:11 2018 +0800
2 files changed, 8 insertions(+), 1 deletion(-)
create mode 100644 B.cpp
6、使用git rebase --continue
逐步前进到最新的提交位置。
$ git rebase --continue
error: could not apply aa3f6b7... 第三次提交
When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
Could not apply aa3f6b7... 第三次提交
Auto-merging A.cpp
CONFLICT (content): Merge conflict in A.cpp
上面执行后因为有两处都有修改,需要解决冲突。
$ git status
interactive rebase in progress; onto ebcd312
Last commands done (2 commands done):
edit e186c75 第二次提交
pick aa3f6b7 第三次提交
No commands remaining.
You are currently rebasing branch 'master' on 'ebcd312'.
(fix conflicts and then run "git rebase --continue")
(use "git rebase --skip" to skip this patch)
(use "git rebase --abort" to check out the original branch)
Unmerged paths:
(use "git reset HEAD <file>..." to unstage)
(use "git add <file>..." to mark resolution)
both modified: A.cpp
no changes added to commit (use "git add" and/or "git commit -a")
修改后再次提交即可
$ vim A.cpp
$ git add A.cpp
$ git commit -a
[detached HEAD 8070ac2] 第三次提交
1 file changed, 6 insertions(+), 1 deletion(-)
然后重新执行
$ git rebase --continue
Successfully rebased and updated refs/heads/master.
如果还有冲突,则重复执行上面两步骤。
7、最后将stash存储的内容释放出来,继续工作
$ git stash pop
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: B.cpp
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (fc32de3118386c30047df86670371c8ab049e0e0)
最新文章
- JavaScript之基础篇
- js中网页高度与宽度那些事
- 字符串匹配算法之KMP&;Boyer-Moore
- C的输入输出函数的基本用法
- 自己用的框架写了一个PHP模版解析类
- 房租管理小软件(六):通用功能包括时间,效验,MD5加密,XML 操作
- 快速集成图片浏览器快速集成图片浏览器->;MJPhotoBrowser的使用
- C#三层开发做学生管理系统
- Webservice 中涉及的几个概念
- java虚拟机 jvm 方法区实战
- Node.js简述
- Thread中的一些方法
- 内置函数——filter和map
- bitnami redmine配置全过程
- .Net使用DES加密,.Net和java分别解密,并正则匹配替换加密密码为明文
- Slack 开发入门之 Incoming Webhooks:往 Slack 的 Channel 中发消息
- FFMpeg音频重采样和视频格式转
- git 的认识
- 阅读 DPDK 中文论文两则
- iOS- 如何将应用集成发短信、发邮件、打电话
热门文章
- day11 装饰器---函数的使用方法
- day3逻辑运算符
- Lunch War with the Donkey CSU - 2084
- 错误: 在类中找不到 main 方法, 请将 main 方法定义为:public static void main(String[] args)否则 JavaFX 应用程序类必须扩展javafx.ap
- 从输入 URL 到页面加载完成的过程详解---【XUEBIG】
- RIPng 配置
- Qt程序继承QApplication发生崩溃的原因
- 241. String to Integer
- BZOJ2319 : 黑白棋游戏
- ironic驱动-IMPITool