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

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

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

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

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


当前回答

下面是一种删除特定提交id的方法,只知道您想删除的提交id。

git rebase --onto commit-id^ commit-id

注意,这实际上删除了提交所引入的更改。

其他回答

进一步解释一下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

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

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

(例子来自这里)

下面是一种删除特定提交id的方法,只知道您想删除的提交id。

git rebase --onto commit-id^ commit-id

注意,这实际上删除了提交所引入的更改。

# detach head and move to D commit
git checkout <SHA1-for-D>

# move HEAD to A, but leave the index and working tree as for D
git reset --soft <SHA1-for-A>

# Redo the D commit re-using the commit message, but now on top of A
git commit -C <SHA1-for-D>

# Re-apply everything from the old D onwards onto this new place 
git rebase --onto HEAD <SHA1-for-D> master

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

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

或象征性的,

git rebase --onto A C HEAD

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

刚刚收集了所有人的答案:(我是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