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

标签 git version-control jgit squash

我已经回顾了几个关于squashing the most recent commits的相关问题和 squashing a commit at the root ,但都不会帮助我压缩不在根目录下的非最近提交。

这是我的起始场景:

D---E---F---G---H---I---J master

和我想要的结果:

D---E---Z---I---J master

哪里ZF---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/$ 

我选择提交的地方 F---G---H成为squash ,同时保留最旧的提交 - rebase 交互中的第一行 - 作为 pick .为什么这行不通?

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

第二种方法:

我又做了一次尝试[通过 merge 一个新分支(论坛上的最后一个帖子)][ http://git.661346.n2.nabble.com/Non-interactive-squash-a-range-td5251049.html ) 使用 git checkout -b <clean-branch> <start-id> and git merge --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

似乎有这样的结果:

  -------------------  <clean-branch> with non-committed changes
 / 
D---E---F---G---H---I---J <master>

我有点难过,那么我该如何压缩这些提交呢?

最终,我计划在 JGit 中实现它, 所以 JGit实现也是可以接受的。

注意

可能有一个 duplicate here ,但它没有答案,我认为这个问题有点不清楚。

更新

这是对@ryenus 在下面的回答的回应:

cherry-pick 在提交时失败 I2 , 其中I2I---I2---J .当它失败时,我的 work 的状态分支机构有D---E---Z ,按照预期进行,直到选择为止,然后是未提交的更改。打电话git cherry-pick --abort清除这些未提交的更改,我验证了提交 Z是对的,就是F---G---H的南瓜.提交后Z , 然后 cherry-picking,为什么 cherry-pick 在 F 失败?

好像git cherry-pick I...J正在尝试挑选 I2 ,这会造成 merge 冲突并失败。有什么建议吗?

这是我的输出:

[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$ 

最佳答案

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

鉴于此,我会先创建一个分支,然后使用 vanilla git reset 然后是 git cherry-pick:

# D---E---F---G---H---I---J master
  1. git checkout -b work H

    H

  2. 创建分支 work
  3. git reset E

    现在工作E

  4. git commit -am "F-G-H 压缩为 Z"

    F---G---H 所做的更改提交为 Z

  5. 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

注意:提交范围已更正为I^..J,代表提交系列I---J(来自I code> 到 J,包括在内)。

关于Git:压缩不是最近提交的连续提交,并且不从根开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26029698/

相关文章:

git - 从刚刚初始化的 git repo 暂存区中删除所有文件

svn - 编辑先前提交的 SVN 消息

java - 使用 JGit - Bare Repo 提交并推送到 GitHub?

java - 干净地关闭 JGit

git - 找不到 gitconfig 文件

javascript - VSCode 未显示源代码控制 git 面板中的更改

git - merge 不相关分支 SVN 分支到 Git 转换

git - 从现有提交创建新分支,现在 origin 和 origin/master 不同步

git - JGit 列出远程标签并按创建日期排序

Gitolite:添加用户不起作用,并且在克隆为 root 时被 fallthru 拒绝?