Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rebase process gets stuck with detached head

Tags:

git

rebase

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?

like image 567
Adrián Avatar asked Dec 29 '17 12:12

Adrián


People also ask

What causes git detached head?

If you checkout a tag, then you're again on a detached HEAD. The main reason is that if you make a new commit from that tag then given that that commit is not referenced by anything (not any branch or tag) then still its considered a detached HEAD. Attached HEADs can only happen when you're on a branch.

How do you reattach a detached head?

Now, the best way to reattach the HEAD is to create a new branch. We can do it as simple as git checkout -b <branch-name> . This will commit the changes from your temporary branch into the branch you need them.

Why do I have to pull after rebasing?

The reason why git status reports that feature and origin/feature diverge after the rebase is due to the fact that rebasing brings in new commits to feature , plus it rewrites the commits that were previously pushed to origin/feature .


1 Answers

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.

like image 170
Melebius Avatar answered Sep 22 '22 05:09

Melebius