threads / discuss / 17531

git rebase -i onto HEAD~n

Subject: git rebase -i onto HEAD~n

## tl;dr

6 messages between Feb 3, 2009 and Feb 3, 2009.

replies: 5people: 3as markdown or json

Bisani, Alok· Feb 3, 2009, 15:17 UTC · lore
Hi,

Is it possible to use git rebase -i for the following use case, in a local branch?

$ git reset --soft HEAD~3 $ # magically squash HEAD~2 & HEAD, ignoring HEAD~1 (which should be discarded) $ git commit -m "new commit replacing current HEAD~2 HEAD, without the commit HEAD~1 (to be discarded)"

I get the error below.
    grep: /home/user/project/.git/rebase-merge/done: No such file or
directory
    Cannot 'squash' without a previous commit
Or could you please suggest another command which is meant to this.

I must mention, that HEAD~3, in my case is the first commit, but I don't think that makes any difference.

$ git --version git version 1.6.0

Best regards, Alok

============================================================================== Please access the attached hyperlink for an important electronic communications disclaimer:

http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html ==============================================================================

Björn Steinbrink· Feb 3, 2009, 15:44 UTC · re: Bisani, Alok · lore

Re: git rebase -i onto HEAD~n

On 2009.02.03 15:32:35 -0000, Bisani, Alok wrote:
Show 16 quoted lines
> Hi,
> 
> Is it possible to use git rebase -i for the following use case, in a
> local branch?
> 
> $ git reset --soft HEAD~3
> $ # magically squash HEAD~2 & HEAD, ignoring HEAD~1 (which should be
> discarded)
> $ git commit -m "new commit replacing current HEAD~2 HEAD, without the
> commit HEAD~1 (to be discarded)"
> 
> I get the error below.
> 
>     grep: /home/user/project/.git/rebase-merge/done: No such file or
> directory
>     Cannot 'squash' without a previous commit

It would be helpful if you told us what command you used, and what you changed in the editor.

This should do: git rebase -i HEAD~3

And in the editor: pick HEAD~2 squash HEAD

Björn
Bisani, Alok· Feb 3, 2009, 16:15 UTC · re: Björn Steinbrink · lore

RE: git rebase -i onto HEAD~n

Show 26 quoted lines
>On 2009.02.03 15:32:35 -0000, Bisani, Alok wrote:
>> Hi,
>> 
>> Is it possible to use git rebase -i for the following use case, in a 
>> local branch?
>> 
>> $ git reset --soft HEAD~3
>> $ # magically squash HEAD~2 & HEAD, ignoring HEAD~1 (which should be
>> discarded)
>> $ git commit -m "new commit replacing current HEAD~2 HEAD, without the 
>> commit HEAD~1 (to be discarded)"
>> 
>> I get the error below.
>> 
>>     grep: /home/user/project/.git/rebase-merge/done: No such file or 
>> directory
>>     Cannot 'squash' without a previous commit
>
>It would be helpful if you told us what command you used, and what you changed in the editor.
>
>This should do:
>git rebase -i HEAD~3
>
>And in the editor:
>pick HEAD~2
>squash HEAD
Sure. Here is the sequence of commands.
    mkdir gittest; cd gittest/
    git init
    echo "one" > one.txt; git add one.txt; git commit -m "one"
    echo "two" > two.txt; git add two.txt; git commit -m "two"
    echo "bad" > discard.txt; git add discard.txt; git commit -m "discard"
    echo "three" > three.txt; git add three.txt; git commit -m "three"
    git rebase -i HEAD~3
    # Leave editor for ~/gittest/.git/rebase-merge/git-rebase-todo with
    squash 8df4c33 two
    # deleted line with discard
    squash 91f8267 three
    # And get this error
    grep: /home/user/gittest/.git/rebase-merge/done: No such file or directory
    Cannot 'squash' without a previous commit
I guess the error could be introduced after commit -m "two" is first squashed, leaving commit -m "three" parentless, in the (no branch) branch?

Best regards, Alok

============================================================================== Please access the attached hyperlink for an important electronic communications disclaimer:

http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html ==============================================================================

Björn Steinbrink· Feb 3, 2009, 17:04 UTC · re: Bisani, Alok · lore

Re: git rebase -i onto HEAD~n

On 2009.02.03 16:22:22 -0000, Bisani, Alok wrote:
Show 8 quoted lines
> >And in the editor:
> >pick HEAD~2
> >squash HEAD
> 
>     # Leave editor for ~/gittest/.git/rebase-merge/git-rebase-todo with
>     squash 8df4c33 two
>     # deleted line with discard
>     squash 91f8267 three

Notice the difference? You're trying to squash 8df4cee into nothing. And _if_ that would work, it would still squash 8df4c33 into the previous commit, which is not what you wanted anyway.

Björn
Bisani, Alok· Feb 3, 2009, 17:55 UTC · re: Björn Steinbrink · lore

RE: git rebase -i onto HEAD~n

Show 11 quoted lines
>On 2009.02.03 16:22:22 -0000, Bisani, Alok wrote:
>> >And in the editor:
>> >pick HEAD~2
>> >squash HEAD
>> 
>>     # Leave editor for ~/gittest/.git/rebase-merge/git-rebase-todo with
>>     squash 8df4c33 two
>>     # deleted line with discard
>>     squash 91f8267 three
>
>Notice the difference? You're trying to squash 8df4cee into nothing. And _if_ that would work, it would still squash 8df4c33 into the previous commit, which is not what you wanted anyway.
Yes, it is clear now. Thank you. And actually, the help page http://www.kernel.org/pub/software/scm/git/docs/git-rebase.html states this clearly as well: 'If you want to fold two or more commits into one, replace the command "pick" with "squash" for the *second_and_subsequent* commit.'
However, I _did_ want to squash 8df4c33 (HEAD~2) etc, into HEAD~3 (the first commit). But unfortunately I cannot do $ git rebase -i  HEAD~4, as HEAD~3 is the first commit. Although, I understand that it will hardly be ever used in practise.
Is there a way to have git rebase --amend the --onto commit?

Best regards, Alok

============================================================================== Please access the attached hyperlink for an important electronic communications disclaimer:

http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html ==============================================================================

Sitaram Chamarty· Feb 3, 2009, 17:18 UTC · re: Bisani, Alok · lore

Re: git rebase -i onto HEAD~n

On 2009-02-03, Bisani, Alok <alok.bisani@credit-suisse.com> wrote:
Show 8 quoted lines
>     # Leave editor for ~/gittest/.git/rebase-merge/git-rebase-todo with
>     squash 8df4c33 two
>     # deleted line with discard
>     squash 91f8267 three
>
>     # And get this error
>     grep: /home/user/gittest/.git/rebase-merge/done: No such file or directory
>     Cannot 'squash' without a previous commit
Quoting from "git help rebase"'s interactive mode section:
    If you want to fold two or more commits into one,
    replace the command "pick" with "squash" for the second
    and subsequent commit.

Keep the first one as "pick", don't make both of them squash.

-- 
Sitaram

We've long needed a filesystem named after a vegetable --
Andrew Morton

← back to recent threads