Home > Enterprise >  I can't delete this Git detached head
I can't delete this Git detached head

Time:10-21

I have a git repo with two branches: kristof (my name) and master.

I messed something up. When running gitk, I can see the following:

enter image description here

As you can see, there is a commit (already pushed remotely) at the top, with a tag v0.5.11. It was a mistake, and I simply want to delete that commit and its tag alltogether.

I tried a few things, but it only got worse. Now, when I checkout into that commit (by using its SHA1 ID, which I just represent here ending in 'XXXX' for safety reasons), this is what I get:

>git checkout b36a22XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Note: switching to 'b36a22XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'.

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 switching back to a branch.

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

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at b36a22a79 Release v0.5.11

How do I delete that commit (and its tag) completely from the whole history?

 


EDIT 1
Thank you for the tip @rafathasan! However, now I see this:

enter image description here

 


EDIT 2
Thank you for the tips @torek. Indeed, after checking out in another branch, the commit I wanted to delete is gone. At least, it doesn't show up in gitk anymore. I did a git prune then, to cleanup the git repo:

>git prune -v --progress
02964e12e4bf2f3ebd203f8d6748045c7aa01c28 tree
2a10ddba4c16cecd76d627c09bb92dc3917a84e4 blob
42e816cd940659d3182f184220f18321c2590875 tree
69e3631b10070bd8dd475105e77958aa31785ed5 tag
7cf01c07408267820638723f04e74d1607dccd05 tree
d3d50b88f5b6e1279af13456610d7862053b4bea tree

The commit I just deleted had an ID starting with b36a22. Strange enough, there is no ID starting with b36a22 in the prune listing above.

Anyhow, I'll consider the issue solved. Thanks for the help!

 


EDIT 3
I just noticed the following comment from @LeGEC:

If the tag was pushed: the OP will also need to delete the remote tag : git push origin -d v0.5.11

So I followed this advice:

>git push origin -d v0.5.11
To git.embeetle.com:embeetle
 - [deleted]             v0.5.11

He also mentions:

If the central repo is a shared repo and other users may have fetched the tag on their own copy : they should be instructed to run git tag -d v0.5.11 on their own copy of the repo.

Thanks for letting me know!

After all this, just because I'm a little paranoid, I ran the following command:

>git gc --prune --aggressive
Enumerating objects: 68964, done.
Counting objects: 100% (68964/68964), done.
Delta compression using up to 20 threads
Compressing objects:  99% (68256/68421)

The command is still running, now more than an hour...

CodePudding user response:

The detached head is labeled with a tag. Here how to delete it:

git tag -d v0.5.11

  • Related