Git: conquering consecutive commits that are not the last commits and do not start at the root

I looked at a couple of related questions about compressing recent commits and commit failures in the root directory , but none of them will help me squander recent commits that are not at the root.

Here is my starting script:

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

and my desired result:

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

where Z is the squash F---G---H , and F---G---H , D---E and I---J can be an arbitrarily long sequence of non-branching commits.

First approach:

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

where I select commits F---G---H as squash , leaving the old commit - the first line in rebase interactive - as pick . Why is this not working?

Update: at the end of the command, a transition to D from E is performed, which is a HEAD commit. Of course, there were no changes at the beginning, and calling git rebase --abort when you restart it has the same result. When I do this using root or HEAD, according to the links above, everything works fine.

Second approach:

I made another attempt [by merging a new branch (last post on the forum)] [ http://git.661346.n2.nabble.com/Non-interactive-squash-a-range-td5251049.html ) that uses git checkout -b <clean-branch> <start-id> and git merge --squash `, but I get the following:

 [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 

which seems to have this result:

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

I am a little puzzled, so how can I crush these commits?

In the end, I plan to implement it in JGit , so the implementation JGit will also be acceptable.

Note

There may be a duplicate here, but it has no answers, and I think the question is a bit unclear.

UPDATE

This is the answer to @ryenus answer below:

Vibration return fails on fixing I2 , where I2 is in I---I2---J When it fails, the state of my work branch has D---E---Z , as expected, before the cherry pick, and it is followed by uncommitted changes. Calling git cherry-pick --abort clears these uncommitted changes, and I checked that commit Z correct, which is squash F---G---H After committing Z and then picking up the cherry, why does the cherry picker not work at F ?

It seems that git cherry-pick I...J trying to select cherry-pick I2 , which creates a merge conflict and fails. Any suggestions?

Here is my conclusion:

 [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$ 
+5
source share
1 answer

First of all, if F---G---H are compressed, then I---J will be I'---J' .

Given this, I first create a branch, then use vanilla git reset , then git cherry-pick :

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

    create a work branch in H

  • git reset E

    Now work is in E

  • git commit -am "FGH squeezed as Z"

    commit changes made by F---G---H as Z

  • git cherry-pick I^..J

    take I---J

Now the work branch is in the desired form, just reset master to it:

 # D---E---Z---I`---J` work git checkout master git reset work git branch -d work 

Note: the fixation range has been adjusted to I^..J to represent the fixation series I---J (from I to J inclusive).

+5
source

Source: https://habr.com/ru/post/1203350/


All Articles