git - 更改存在提交到当前提交

标签 git git-rebase

<分区>

简单地说,我是这样做的:

A------B-------C
        \
         \
          B2

现在,我想将 B 更改为 B2。

A------B2-------C

这可能吗?

解决方案历史:

我刚刚添加了我的工作经历。

$ git log
commit b671c70b C
commit f4acdc2b B
commit 56f38939 A

$ git checkout f4acdc2b 

然后我修改了一些东西...然后使用 -amend 选项提交。

$ git commit -amend
$ git log
commit e2fd729 B'
commit 56f3893 A

现在,它变成了这样:

A------B-------C
        \
         \
          B'

将 B rebase 为 B'

$ git checkout b671c70b
$ git rebase -i 56f38939

打开交互式编辑器

pick f4acdc2b B
pick 56f38939 A

只需删除 pick f4acdc2b 行,保存并退出。

如果出现错误error: could not apply b671c70b... C, 编辑所有 merge 冲突,然后,

$ git add .
$ git rebase --continue

$ git log
commit 914c6bc C'
commit 56f3893 A

$ git checkout 914c6bc
$ git rebase e2fd729 
$ git log
commit 5c65190 C''
commit e2fd729 B'
commit 56f3893 A

现在,它看起来像这样。

A------B'-------C''

最佳答案

要完全删除 B,您必须在 A 上重新集成 B2,在 B2 上重新集成 C

git checkout C
git rebase B2

这个 rebases 分支 CB2

A-------B------B2-------C'

(注意 C' 是一个新的提交)

但是现在你在历史中仍然有 B 提交。您可以使用交互式 rebase 将其删除。

git rebase -i A

打开交互式编辑器

pick 05c98ef B
pick e31d9f0 B2
pick 5d30d05 C

只需删除 pick 05c98ef B 行,保存并退出。删除此 B 后,您的历史记录如下所示

A------B2'-------C''

关于git - 更改存在提交到当前提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27165713/

相关文章:

git - Git 中有某种反向挑选吗?

git - 将 git 存储库转换为浅存储库?

git - 从 svn 迁移到 git 保持 svn-externals

git commit 修改和未跟踪的内容

git - 在 git rebase 之后,我的本地分支和远程分支发生了分歧

Git:如何 rebase 到过去的特定提交?

git - pull 、 rebase 、推,在一个命令中(或几个)

php - 如何在本地运行 Laravel git 克隆

git别名获取cwd

git - 从我在本地没有的远程 git 历史记录中删除一个提交