如何从git历史记录中删除提交,但在其他方面保持图形完全相同(包括合并)呢? [英] How to remove commits from git history but otherwise keep the graph exactly the same, including merges?

查看:84
本文介绍了如何从git历史记录中删除提交,但在其他方面保持图形完全相同(包括合并)呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所拥有的:

---A----B-----C-----D--------*-----E-------> (master)
                     \      /
                      1----2 (foo)

我需要什么:

---A---------------D--------*-----E-------> (master)
                    \      /
                     1----2 (foo)

前一段时间,我做了两次提交,我想从我的git repo中删除.我尝试了各种不同的变基教程",所有这些都以怪异的git历史结尾,因此我创建了一个示例存储库,结果与我的预期不同.谁能帮助我了解我所缺少的东西吗?

A while ago I made two commits I would like to remove from my git repo. I tried a variety of different rebasing "tutorials" and all of them ended in weird git histories, so I created an example repo, and the result is not what I expected. Can anyone help me to understand what I am missing?

我有两个分支,masterfoo.我对要删除的单个文件进行了 B 提交,并在修改该文件的位置提交了 C .除了其他提交,我再也没有碰过这个文件.

I have two branches, master and foo. I made a commit B with a single file I would like to remove, and commit C where I modified this file. Along the other commits, I never touched this file ever again.

提交ID:

A: f0e0796
B: 5ccb371
C: a46df1c
D: 8eb025b
E: b763a46
1: f5b0116
2: 175e01f

所以我使用rebase -i f0e0796并删除了 B 5ccb371 C a46df1c,对吗?如果我正确地解释了结果,尽管git branches仍列出了第二个分支,这是gitk向我显示的回购信息.

So I use rebase -i f0e0796 and remove B 5ccb371 and and C a46df1c, correct? If I interpret the result correctly, this is what gitk shows me for my repo, although git branches still lists the second branch.

---A-----1----2---E------> (master)

谁能告诉我这里发生了什么事?

Can anyone tell me what happened here?

修改: 这是从第一个图形重新创建存储库的方法:

This is how to recreate the repo from the first graph:

git init foo
cd foo

touch A
git add A
git commit -m "add A"

touch B
git add B
git commit -m "add B"

echo "modify" > B
git add B
git commit -m "modify B"

touch C
git add C
git commit -m "add C"

git checkout -b foo

touch 1
git add 1
git commit -m "add 1"

touch 2
git add 2
git commit -m "add 2"

git switch master
git merge foo --no-ff

touch E
git add E
git commit -m "add E"

推荐答案

尽管我提议的内容将为您提供清晰的线性历史记录;这就是rebase本质上应该做的事情.但是,希望这为您提供了一种从提交历史记录中删除B和B'的方法.解释如下:

While what I am proposing will give you a clean, linear history; that's what rebase is supposed to do essentially. However, am hoping this gives you a way to remove B and B' from the commit history. Here goes the explanation:

Repo recreation output:
---A----B-----B'-----C--------D-------> (master)
                      \      /
                       1----2 (foo)

git log --graph --all --oneline --decorate #initial view the git commit graph
* dfa0f63 (HEAD -> master) add E
*   843612e Merge branch 'foo'
|\  
| * 3fd261f (foo) add 2
| * ed338bb add 1
|/  
* bf79650 add C
* ff94039 modify B
* 583110a add B
* cd8f6cd add A

git rebase -i HEAD~5 #here you drop 583110a/add B and ff94039/modify B from
foo branch.

git log --graph --all --oneline --decorate
$ git rebase -i HEAD~5
* 701d9e7 (HEAD -> master) add E
* 5a4be4f add 2
* 75b43d5 add 1
* 151742d add C
| * 3fd261f (foo) add 2
| * ed338bb add 1
| * bf79650 add C
| * ff94039 modify B
| * 583110a add B
|/  
* cd8f6cd add A

$ git rebase -i master foo #drop 583110a/add B and ff94039/modify B again

$ git log --graph --all --oneline --decorate #view the git commit graph

* 701d9e7 (HEAD -> foo, master) add E
* 5a4be4f add 2
* 75b43d5 add 1
* 151742d add C
* cd8f6cd add A

最后,最后的结果可能与您期望的顺序A--C--1 --- 2 --- E不一致.但是,您可以再次在交互模式下重新排列顺序.尝试git rebase -i HEAD〜n.

Lastly, the final out might not be in the order you'd expected A--C--1---2---E. However, you can re-arrange the order within the interactive mode again. Try git rebase -i HEAD~n.

注意:最好避免更改提交/发布历史记录.我是新手,正在探索git,希望上面的解决方案能够坚持下去.那就是说,可以肯定的是,在线上还有许多其他更简单的解决方案可用.我发现此文章非常有用,可供所有人将来参考.

Note: It's best to avoid changing commit/publishing history. I am a newbie and exploring git, hopefully the above solution should stick. That said am sure there are tonnes of other easier solutions available online. I found this article quite helpful, for future reference for all.

这篇关于如何从git历史记录中删除提交,但在其他方面保持图形完全相同(包括合并)呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆