Getting an error when trying to run `git rebase --continue`, cannot lock ref 'refs/heads/Head-feature'

Viewed 2779

I'm running the command git rebase --continue and getting an error:

error: update_ref failed for ref 'refs/heads/HEAD-feature': cannot lock ref 'refs/heads/HEAD-feature': is at db50dd34de1e90c0616bf9318be489ee8d9a012a but expected 83f09532b2352418c0f562f48929dc504e6a0452 error: could not update refs/heads/HEAD-feature

I looked at a couple similar questions but none of the answers worked.

I tried the following:

1. git update-ref -d refs/remotes/origin/HEAD-feature
2. git --no-optional-locks fetch --prune origin
3. git gc --prune=now
4. git remote prune origin

The only way I've found to overcome this is to do git rebase --abort but then I lose all the changes I made during an interactive rebase. Any ideas?

1 Answers

You or some rogue process was messing with your repo state in a way that conflicts with git rebase

The error message indicates that you or someone else or something did something to your repo state, something that you can't do while a rebase is in progress. It was expecting a ref to point to a specific commit, but it got changed out from under it.

How to recover

Here's how to recover from the situation without losing all your rebase work so-far (assuming you haven't yet aborted):

  1. Save your rebase progress so-far

    git branch PARTIAL-REBASE HEAD
    

    This will create a branch ref named PARTIAL-REBASE pointing to the last rebased commit -- i.e. your rebase work so far.

    You must do this before git rebase --abort, otherwise you'll have to search the reflogs to recover your partial rebase, which is more complicated.

  2. Abort the rebase

    git rebase --abort
    
  3. Resume the rebase where it left off

    There are many ways to do this, but this is the easiest.

    First see which commits are included in your saved partial rebase:

    git log PARTIAL-REBASE
    

    Make sure you are on the correct branch, then rerun your original rebase command, but add the following options to the command:

    --onto PARTIAL-REBASE --interactive 
    

    A list of commits to rebase will appear in an editor. Carefully delete only those lines for the commits that are already included in the git log for PARTIAL-REBASE above. DO NOT make any other changes unless you know what you are doing.

    Save and close the rebase list. The commits in the rebase list will now be rebased onto PARTIAL-REBASE, effectively resuming your rebase at the point you aborted it.

  4. Verify and cleanup

    Verify that your rebased branch is good.

    When you're sure that your rebase was successful and all is good, delete the PARTIAL-REBASE ref:

    git branch -D PARTIAL-REBASE
    

See also How to fix "corrupted" interactive rebase? and Git rebase failing, though I'm surprised that no one gave a "How to recover" solution such as mine.

Related