Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-rebase

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?

like image 387
Patrick B. Avatar asked Feb 11 '14 14:02

Patrick B.


People also ask

What happens when you skip a commit during rebase?

You can run git rebase --abort to completely undo the rebase. Git will return you to your branch's state as it was before git rebase was called. You can run git rebase --skip to completely skip the commit. That means that none of the changes introduced by the problematic commit will be included.

Can you undo a rebase in git?

To undo the rebase , we can use the reflog command of Git. Using git reflog , we can determine the branch's head commit immediately before the rebase starts.

Do I need to commit again after rebase?

The purpose of rebase is make your commits look as if they were changes to the branch you rebase onto. So the most logical way is to incorporate merge conflicts into these commits. No additional commits is required thus.


2 Answers

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

like image 84
Cordell Avatar answered Oct 05 '22 19:10

Cordell


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.

like image 37
Patrick B. Avatar answered Oct 05 '22 19:10

Patrick B.