Skip to content

Instantly share code, notes, and snippets.

@mrkpatchaa
Created April 10, 2019 14:44
Show Gist options
  • Save mrkpatchaa/b641c935a23ad3eb268c13505acc8c81 to your computer and use it in GitHub Desktop.
Save mrkpatchaa/b641c935a23ad3eb268c13505acc8c81 to your computer and use it in GitHub Desktop.
Git Detached Head

The intermediate steps of an interactive rebase are done with a detached HEAD (partially to avoid polluting the active branch’s reflog). If you finish the full rebase operation, it will update your original branch with the cumulative result of the rebase operation and reattach HEAD to the original branch. My guess is that you never fully completed the rebase process; this will leave you with a detached HEAD pointing to the commit that was most recently processed by the rebase operation.

To recover from your situation, you should create a branch that points to the commit currently pointed to by your detached HEAD:

git branch temp git checkout temp (these two commands can be abbreviated as git checkout -b temp)

This will reattach your HEAD to the new temp branch.

Next, you should compare the current commit (and its history) with the normal branch on which you expected to be working:

git log --graph --decorate --pretty=oneline --abbrev-commit master origin/master temp git diff master temp git diff origin/master temp (You will probably want to experiment with the log options: add -p, leave off --pretty=… to see the whole log message, etc.)

If your new temp branch looks good, you may want to update (e.g.) master to point to it:

git branch -f master temp git checkout master (these two commands can be abbreviated as git checkout -B master temp)

You can then delete the temporary branch:

git branch -d temp Finally, you will probably want to push the reestablished history:

git push origin master You may need to use --force to push if the remote branch can not be “fast-forwarded” to the new commit (i.e. you dropped, or rewrote some existing commit, or otherwise rewrote some bit of history).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment