Adrián
Adrián

Reputation: 85

Git rebase process gets stuck with detached head

I'm trying on a certain project to reword the penultimate commit to fix a typo by running git rebase -i HEAD~3, (using the "nano" editor) then changing the default pick option of that commit to r or reword (on the initial rebase file window), and, without modifying anything else. I'm doing it on the master branch, if useful.

As soon as I save the file, Git, instead of showing me the next rebase window to pick a new name for that commit as usual, it puts itself and informs me of a detached HEAD state with that commit, that is also shown upon git status command from then on, until I type git checkout master.

No matter how many times I try, the same thing happens.

Additional note: I had previously changed the used editor to "nano" by running the single command: git config --global core.editor nano

EDIT: As requested, this is the message that Git gives me when I save the TODO list:

adrian$ git rebase -i HEAD~1

Note: checking out 'da91bbcedc78cb2ebcaa9dc51f38c8d0a550195d'.

You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:

git checkout -b

HEAD is now at da91bbc... Test message

This repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting .git/hooks/post-checkout. could not detach HEAD

Output after running git rebase --continue at this point:

No rebase in progress?

Upvotes: 14

Views: 31104

Answers (4)

Daniel Wyatt
Daniel Wyatt

Reputation: 1151

I found I had a detached head when I made a new commit locally and tried a git rebase with squashes. I believe my issue was that I didn't push this local commit to the remote branch first.

It seemed to work when my local branch was up to date with remote before performing a git rebase after that.

Upvotes: 0

linjiejun
linjiejun

Reputation: 1678

I am facing the same problem when doing rebase squash. I am not found the reason about this .

My workaround is copy this master branch happening this problem to other new branch let's say branch "tiantale".

When I do the same rebase steps in new "tiantale" branch,It works fine.

Then I do cherry-pick to pick this commit created by rebase to master branch.

Upvotes: 0

Stephen Talley
Stephen Talley

Reputation: 1202

I had this same issue with git rebase -i, with the same exact output and absolutely no indication of why from git.

Through trial and error, I eventually found that my post-checkout hook (repodir/.git/hooks/post-checkout) was exiting with a non-zero status. Once I added an exit 0 to the end of it, the rebase succeeded without leaving the repo in the detached HEAD state.

Upvotes: 11

Melebius
Melebius

Reputation: 6695

The detached HEAD message appears normally when you put edit in the to-do file for the interactive rebase. You must have mistakenly put edit there instead of reword. Or Git might have entered this mode (which is also entered in conflicts) due to the error found in your output:

This repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting .git/hooks/post-checkout. could not detach HEAD

You should resolve this error before continuing. Git tells you what to do.

The edit mode allows modifying the commit message like reword mode but also the file contents. Therefore Git left you in a state where you can commit changes and then continue rebasing using git rebase --continue.

Editing the commit message (like reword) in the edit mode

When you want to just edit the commit message and continue rebasing, run

git commit --amend

which opens the editor to let you edit the commit message. After you have finished, run

git rebase --continue

Leaving unfinished rebase

As soon as I save the file, Git, instead of showing me the next rebase window to pick a new name for that commit as usual, it puts itself and informs me of a detached HEAD state with that commit, that is also shown upon git status command from then on, until I type git checkout master.

This is not the correct way of leaving unfinished rebase, you should use

git rebase --abort

instead.

Upvotes: 12

Related Questions