Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How to fix commit during rebase

Tags:

git

rebase

I came across the following problem:

During git rebase one of automatically resolved commits has an error, i.e. as a result of automatic resolution, a function declaration was introduced for the second time in the header file and compilation fails.

My question is: is it possible to go back to that automatically resolved commit, resolve it manually and then continue with the rebase, assuming that I am still within the rebase process?

like image 645
Tomek Avatar asked Jan 12 '13 15:01

Tomek


2 Answers

You should first finish the original rebase, so that you are in a known state with your repository. Then it is quite easy to edit the commit that introduced the error with interactive rebase. Check out the sha1 of the commit you want to fix, then do

git rebase -i <sha1>^

An editor will open containing commits from the HEAD up to the commit you want to fix. Find the commit from the list (it should be the first one), replace the word "pick" with "edit", save and exit editor.

Now you can fix the bug, then do

git commit -a --amend
git rebase --continue

That's it!

like image 186
Kalle Pokki Avatar answered Nov 09 '22 13:11

Kalle Pokki


Although a rebase within a rebase will not work, it is possible to git commit --amend to the last committed modification.
If the problem is caused by the commit just before the current one being rebased (i.e its the last one committed), you can modify it without any side effects to the rebase process.

So when I got into this situation, I did the following:

  1. Unstage the current manual modification being rebased:

    $ git reset HEAD <files being rebased>
    
  2. Stage my fix for the last commit causing the problem:

    $ git add <files with compilation fix>
    
  3. Add the compilation fix into the last committed modification:

    $ git commit --amend
    
  4. Return to the current manual modification being rebased:

    $ git add <files being rebased>
    

Hope this helps.

like image 1
Yoav Avatar answered Nov 09 '22 15:11

Yoav