我想从线性提交树中删除选定的提交日志项,这样这些项就不会显示在提交日志中。

我的提交树看起来像这样:

R--A--B--C--D--E--HEAD

我想删除B和C条目,这样它们就不会显示在提交日志中,但是应该保留从A到D的更改。也许通过引入一次提交,这样B和C就变成了BC,树看起来就像。

R--A--BC--D--E--HEAD

或者,理想情况下,A后面直接是D。D'表示从A到B, B到C, C到D的变化。

R--A--D'--E--HEAD

这可能吗?如果是,怎么做?

这是一个相当新的项目,所以目前还没有分支,因此也没有合并。


当前回答

还有一种方法,

git rebase -i ad0389efc1a79b1f9c4dd6061dca6edc1d5bb78a (C's hash)
and
git push origin master  -f

选择您想要使用它作为基础的散列,上面的命令应该使其具有交互性,以便您可以压缩所有顶部消息(您需要保留最老的消息)

其他回答

你可以使用git进行选择。'cherry-pick'将应用一个提交到你现在所在的分支上。

然后做

git rebase --hard <SHA1 of A>

然后应用D和E提交。

git cherry-pick <SHA1 of D>
git cherry-pick <SHA1 of E>

这将跳过B和C提交。已经说过,如果没有B,可能不可能将D提交应用到分支,所以是YMMV。

你可以在你的例子中非交互式地删除B和C:

git rebase --onto HEAD~5 HEAD~3 HEAD

或象征性的,

git rebase --onto A C HEAD

注意,B和C的变化不会出现在D中;他们会走的。

进一步解释一下J.F.塞巴斯蒂安的回答:

您可以使用git-rebase轻松地对提交历史进行各种更改。

运行git rebase——interactive后,你会在$EDITOR中得到以下内容:

pick 366eca1 This has a huge file
pick d975b30 delete foo
pick 121802a delete bar
# Rebase 57d0b28..121802a onto 57d0b28
#
# 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

您可以移动行来更改提交的顺序,也可以删除行来删除提交。或者您可以添加一个命令将两个提交合并(压缩)为一个提交(以前的提交就是上面的提交)、编辑提交(更改的内容)或改写提交消息。

我认为选择只是意味着你不想去做承诺。

(例子来自这里)

刚刚收集了所有人的答案:(我是git的新手,请仅供参考)

Git重基删除任何提交

git 日志

-first check from which commit you want to rebase

我们回来了

-Here i want to rebase on the second last commit- commit count starts from '1')
-this will open the command line editor (called vim editor i guess)

然后屏幕看起来会是这样的:

pick 0c2236d Added new line. Rebase 2a1cd65..0c2236d onto 2a1cd65 (1 command) # 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 d, drop = remove commit # These lines can be re-ordered; they are executed from top to bottom. # If you remove a line here THAT COMMIT WILL BE LOST. # However, if you remove everything, the rebase will be aborted. # Note that empty commits are commented out ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

这里根据您的需要更改第一行(使用上面列出的命令,例如。'drop'删除提交等) 编辑完成后,按“:x”保存并退出编辑器(这仅适用于vim编辑器)

然后

git push

如果它的显示问题,那么你需要强行将更改推到远程(非常关键:如果你是团队工作,不要强行推)

Git push -f origin

还有一种方法,

git rebase -i ad0389efc1a79b1f9c4dd6061dca6edc1d5bb78a (C's hash)
and
git push origin master  -f

选择您想要使用它作为基础的散列,上面的命令应该使其具有交互性,以便您可以压缩所有顶部消息(您需要保留最老的消息)