Git:压缩不是最近提交的连续提交,而不是从根开始

mod*_*tos 5 git version-control squash jgit

我已经回顾了几个关于压缩最新提交压缩根提交的相关问题,但这两个问题都不能帮助我压缩不是根本的非最近提交.

这是我的开始场景:

D---E---F---G---H---I---J master
Run Code Online (Sandbox Code Playgroud)

和我想要的结果:

D---E---Z---I---J master
Run Code Online (Sandbox Code Playgroud)

什么Z是壁球F---G---H,和F---G---H,D---E和,I---J可以是任意长的非分支提交序列.

第一种方法:

[lucas]/home/blah/$ git rebase -i D
rebase in progress; onto D
You are currently editing a commit while rebasing branch 'master' on 'D'.

No changes
You asked to amend the most recent commit, but doing so would make
it empty. You can repeat your command with --allow-empty, or you can
remove the commit entirely with "git reset HEAD^".

Could not apply F... "Comments from commit F"

[1]+  Done                    gitk
[lucas]/home/blah/$ 
Run Code Online (Sandbox Code Playgroud)

在这里我选择承诺F---G---Hsquash,而留下的最古老的承诺-在底垫互动的第一线-作为pick.为什么这不起作用?

更新:在命令结束时D,E作为HEAD提交,正在进行rebase .可以肯定的是,在开始时没有正在进行的重组,并且git rebase --abort在再次运行时调用具有相同的结果.当我在root或HEAD执行此操作时,根据上面的链接,一切正常.

第二种方法:

我做了另一次尝试[通过合并一个新的分支(论坛上的最后一篇文章)] [ http://git.661346.n2.nabble.com/Non-interactive-squash-a-range-td5251049.html)使用git checkout -b <clean-branch> <start-id> andgit合并--squash`,但我得到以下内容:

[lucas-ThinkPad-W520]/home/.../.Solstice_WS/7K_FGHF$ git checkout -b clean-branch D
Switched to branch 'clean-branch'
[lucas-ThinkPad-W520]/home/.../.Solstice_WS/7K_FGHF$ git merge --squash I
Updating D..I
Fast-forward
Squash commit -- not updating HEAD
 .../GraphUtilities/impl/DAG.java              | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
[lucas]/home/blah/$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
        asdf/GraphUtilities//DAG.java
Please, commit your changes or stash them before you can switch branches.
Aborting
Run Code Online (Sandbox Code Playgroud)

这似乎有这个结果:

  -------------------  <clean-branch> with non-committed changes
 / 
D---E---F---G---H---I---J <master>
Run Code Online (Sandbox Code Playgroud)

我有点难过,所以我怎么能压制这些提交?

最终,我计划实现这一点JGit,因此JGit实现也是可以接受的.

注意

这里可能有重复,但它没有答案,我认为这个问题有点不清楚.

UPDATE

这是对@yyenus的答案的回应:

在摘樱桃失败的提交I2,这里I2I---I2---J.当它失败时,我的work分支的状态D---E---Z按照预期直到樱桃选择,然后是未提交的更改.调用git cherry-pick --abort清除这些未提交的更改,并且我验证了提交Z是正确的,这是壁球F---G---H.在提交之后Z,然后采摘樱桃,为什么樱桃选择失败F

似乎git cherry-pick I...J正在尝试挑选I2,这会产生合并冲突并失败.有什么建议?

这是我的输出:

[lucas]/home$ git checkout -b work H
Switched to a new branch 'work'
[lucas]/home$ git reset E             
Unstaged changes after reset:
M       adf/GraphUtilities//graph/impl/DAG.java
[lucas]/home$ git commit -am "squashed commit here!"
[work Z] squashed commit here!
 1 file changed, 2 insertions(+), 5 deletions(-)
[lucas]/home$ git cherry-pick I...J
error: could not apply I2... <Comments from commit I2>
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
[lucas]/home/$ git status
On branch work
You are currently cherry-picking commit I2.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:      3b6863741967406c1888701eb139178187d429487b99787096441d67bed56/Gra
phUtilities/src/edu/washington/cs/utils/graph/impl/DAG.java

no changes added to commit (use "git add" and/or "git commit -a")
[lucas]/home$ 
Run Code Online (Sandbox Code Playgroud)

rye*_*nus 5

首先,如果F---G---H被压扁,那么I---J就是I'---J'.

鉴于此,我首先创建一个分支然后用香草git resetgit cherry-pick:

# D---E---F---G---H---I---J master
Run Code Online (Sandbox Code Playgroud)
  1. git checkout -b work H

    创建分支workH

  2. git reset E

    现在workE

  3. git commit -am "F-G-H squeezed as Z"

    提交由F---G---Has 完成的更改Z

  4. git cherry-pick I^..J

    接管 I---J

现在work分支处于您想要的形状,只需重置master为它:

# D---E---Z---I`---J` work

git checkout master
git reset work
git branch -d work
Run Code Online (Sandbox Code Playgroud)

注:提交范围已更正为I^..J,代表提交系列I---J(从IJ,包容性).