Undoing a git rebase --skip - reapply a commit during a rebase

14,573

Solution 1

I found a way which "worked for me":

During a rebase lots of things are happening in the .git/rebase-apply directory. Amongst others there is a file called next. next is containing a number which corresponds to a file which is residing in the .git/rebase-apply as well. This file contains information about the commit which is currently being processed. For example:

$ cat .git/rebase-apply/next
0260
$ less .git/rebase-apply/0260
<info about the commit which is currently processed (and has conflicts)

Git seems to keep the commits which are skipped as the above mentioned files. Whereas files corresponding to commits which have been applied are not there anymore. The commit I accidentally skipped was called 0259 and the file was still present.

Here is what I did:

$ echo "0258" > .git/rebase-apply/next

With that I told git that currently the 258th commit is processed (which previous applied correctly). Then I did

$ git rebase --skip

to tell git to forget this one and, voila, I could again work on the skipped commit, correct the conflicts and --continue. It has worked.

Solution 2

Git is great because it saves a log of basically everything you commit.

  1. Find your commit in ".git/logs/HEAD" and open in a text editor

  2. Find your SHA in the HEAD file

    3c8c... 2260dc... Full Name {[email protected]} 1471276956 -0600 commit: Saving Trial 1,2,3

  3. Type (note type enough of the sha so git knows which one to pull):

    git checkout -b recovery 2260d...

See link for reference: http://blog.screensteps.com/recovering-from-a-disastrous-git-rebase-mistake

Share:
14,573

Related videos on Youtube

Patrick B.
Author by

Patrick B.

Scott Meyers: "[..] This is C++; everything is a lie but it's really close to the truth. [..]"

Updated on June 06, 2022

Comments

  • Patrick B.
    Patrick B. about 2 years

    I'm doing a long git rebase with a lot of commits. I accidentally --skipped a commit where there were some conflicts which I resolved. I should have done git rebase --continue.

    Is there are way to re-apply this previous commit during this rebase phase and then continue the rebase?

    One way I see is to

    1. stop the rebase at this point by creating a branch on the last commit which was correctly applied
    2. restart the rebase starting with the previously skipped commit.

    Or can I do a cherry-pick whilst being in a rebase-phase?

  • goncalossilva
    goncalossilva almost 10 years
    This changed a bit in recent git versions, but I was still able to make it work by tweaking these files: git-rebase-todo and onto following the idea above.
  • codingdave
    codingdave almost 10 years
    Using git version 1.9.4.msysgit.1 I could succeed with the approach from Patrick B. I did neither use git-rebase-todo nor onto.
  • riroo
    riroo about 8 years
    I'm afraid this is no more possible after a git rebase --skip. At least for me. I don't have a "rebase-apply" in my ".git" folder
  • liberborn
    liberborn over 5 years
    awesome! saved my day!
  • hlovdal
    hlovdal over 5 years
    There is a dedicated command git-reflog to query the commit history, there is no direct need to open the file.
  • Changaco
    Changaco over 5 years
    For me today with git 2.20.1 the directory was .git/rebase-merge/ and the files I modified were git-rebase-todo and done. I ran git reset --hard <hash-of-parent-of-skipped-commit> to clean up the working directory, and finally git rebase --continue.
  • heyxh
    heyxh over 5 years
    I thought my hard work has been gone by my mistake, but no, you save my days. Thank you.
  • Tharanga
    Tharanga almost 5 years
    This is a life saving reply.
  • MDMoore313
    MDMoore313 over 4 years
    You are the business!
  • redstonemercury
    redstonemercury over 3 years
    You just saved my day of work after a bad rebase!