简体   繁体   中英

Git: How to fix commit during rebase

I came across the following problem:

During git rebase one of automatically resolved commits has an error, ie 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?

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!

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 (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM