Listing and deleting Git commits that are under no branch (dangling?)
Asked Answered
G

11

212

I've got a Git repository with plenty of commits that are under no particular branch, I can git show them, but when I try to list branches that contain them, it reports back nothing.

I thought this is the dangling commits/tree issue (as a result of -D branch), so I pruned the repo, but I still see the same behavior after that:

$ git fetch origin

$ git fsck --unreachable
$ git fsck

No output, nothing dangling (right?). But the commit exists

$ git show 793db7f272ba4bbdd1e32f14410a52a412667042
commit 793db7f272ba4bbdd1e32f14410a52a412667042
Author: ...

and it is not reachable through any branch as

$ git branch --contains 793db7f272ba4bbdd1e32f14410a52a412667042

gives no output.

What exactly is the state of that commit? How can I list all commits in a similar state? How can I delete commits like those?

Gardener answered 21/9, 2010 at 23:29 Comment(1)
possible duplicate of How to delete erroneous merge commits?Kerguelen
N
86

No output, nothing dangling (right?)

Note that commits referred to from your reflog are considered reachable.

What exactly is the state of that commit? How can I list all commits with similar state

Pass --no-reflogs to convince git fsck to show them to you.

How can I delete commits like those?

Once your reflog entries are expired, those objects will then also be cleaned up by git gc.

Expiry is regulated by the gc.pruneexpire, gc.reflogexpire, and gc.reflogexpireunreachable settings. Cf. git help config.

The defaults are all quite reasonable.

Nosography answered 22/9, 2010 at 0:57 Comment(10)
so you're basically saying that the reflows for dangling commits will be removed after a while automatically?Latton
Basically: yes – except that the question is a bit confused. I’m saying that all reflog entries are removed automatically after a while, but you can change that through configuration settings. And because a commit is only called dangling when it has nothing pointing to it – including reflog entries –, “reflogs for dangling commits” are not a thing. They would be “reflogs for unreachable commits”.Nosography
'They would be “reflogs for unreachable commits”.' But you said "commits referred to from your reflog are considered reachable." So how can "reflogs for unreachable commits" be a thing? I'm so confused.Chinn
Yeah, I wasn’t consistent. Normally people don’t think about the reflog, and when they say “unreachable” it implies “from a ref”. Even git help glossary defines it that way… whereas its definition for “reachable” is not narrowed down that way, so they are contradictory. Funny – so what I said is actually consistent with the confusion in gitglossary… It’s not the concepts that are confusing, though, just the terminology. The point is that “dangling” commits are ones that nothing else points to. Would it help if I say “reflogs for otherwise unreachable commits”…?Nosography
This is all very confusing. Let's make it simple. When on branch master, you do git commit, and get a commit 000001. Then you do git commit --amend, which gives you commit 000002. There are no tags or branches pointing to 000001 anymore, and you can't see it in your log without the --reflog option, but if you want, you could still get to it with git checkout 000001. Now the question is, Is 000001 a dangling commit, or an unreachable commit, or neither, or both?Copyedit
Both. Every dangling commit is an unreachable commit as well. To expand your example scenario, let’s say you commit thrice, and then you do git reset HEAD~3 to throw away all of the commits you just made. Now you have three new unreachable commits (you cannot get to them from any branch or tag or other ref), the last one of which is dangling (you cannot even get to it from another unreachable commit).Nosography
@AristotlePagaltzis re: reflogs for dangling commits are not a thing, this isn't strictly true. git fsck --dangling may not show a dangling commit that has a reflog entry, but git fsck --lost-found will. You may test this by creating a branch, making a commit, deleting the branch, running git reflog and then git fsck --lost-found. You will see dangling commit: <hash> for a commit that has a reflog entry (as well as, now, an entry in .git/lost-found/commit)Mich
Then git fsck --lost-found misuses the term. Or at least uses it in a way that contradicts its definition in git help glossary.Nosography
I did all these git reflog expire --expire-unreachable=now --all git gc --prune=now git fsck --unreachable --no-reflogs # no output git branch -a --contains <commit> # no output git show <commit> # still shows up i checked for branch --contains, tag --contains, tag --points-to, but nothing shows up except for "git show <commit>" The reflog is empty, lost+found is also empty. Any idea what else can be checked ?Auction
@SenthilAKumar: the stash, as in Leo Zhao’s answer? It’s also conceivable that you have alternates set up (which won’t be affected by any cleanup performed within your own repository).Nosography
T
342

To remove all dangling commits (including those still reachable from stashes and other reflogs) do this:

git reflog expire --expire-unreachable=now --all
git gc --prune=now

But be certain that this is what you want. I recommend you read the man pages but here is the gist:

git gc removes unreachable objects (commits, trees, blobs (files)). An object is unreachable if it isn't part of the history of some branch. Actually it is a bit more complicated:

Stashes are implemented using the reflog (i.e not not branches or tags). That means that they are subject to garbage collection.

git gc does some other things but they are not relevant here and not dangerous.

Unreachable objects that are younger than two weeks are not removed so we use --prune=now which means "remove unreachable objects that were created before now".

Objects can also be reached through the reflog. While branches record the history of some project, reflogs record the history of these branches. If you amend, reset etc. commits are removed from the branch history but git keeps them around in case you realize that you made a mistake. Reflogs are a convenient way to find out what destructive (and other) operations were performed on a branch (or HEAD), making it easier to undo a destructive operation.

So we also have to remove the reflogs to actually remove everything not reachable from a branch. We do so by expiring --all reflogs. Again git keeps a bit of the reflogs to protect users so we again have to tell it not to do so: --expire-unreachable=now.

Since I mainly use the reflog to recover from destructive operations I usually use --expire=now instead, which zaps the reflogs completely.

Tambourine answered 24/12, 2010 at 22:42 Comment(8)
I tell you what commands to use which is not obvious - shouldn't gc be enough? If you never used git-reflog before you won't know. So now that you know what commands you have to use you should look up the mentioned options in their man pages. Of course I could instead just copy that information from there...Tambourine
Well actually I say exactly what it does: "remove all dangling commits and those reachable from the reflogs". If you don't know what reflogs are: again read the manual.Tambourine
While the answer given may be correct, @erikb85 is correct in pointing out that there was no education about what you were being told to do. Following up with RTFM is even less helpful. Yes, we should all read all of the documentation. In some cases perhaps the person doing the search doesn't grok the documentation enough to know what is going on. So, a little bit of education as to what the commands are doing would be helpful for everyone that finds this answer later.Annamaeannamaria
@LeeSaferite hope you're all happy now :-)Tambourine
git reflog expire --expire-unreachable=now --all drops all your stashes!Nutty
I think this answer needs a clear warning, preferably at the top. My edit suggestion was rejected, because I guess I should suggest it to the author in a comment? Please either accept this edit stackoverflow.com/review/suggested-edits/26023983 or add a warning your own way. That it drops all your stashes is a big deal too!Centiliter
I verified my unreachable commits were gone with git reflog, now I gather this may not be enough because they had gone after the git reflog expire --expire-unreachable=now --all, before any git gc --prune=now. OTOH when I tried this the first time, I looped through all the other answers on this page until I realised that commits would never be considered unreachable until I force pushed my squashed branch to the remote, just a FYI.Inpatient
This worked for me but it looked like it didn't because gitk still showed the removed commits (even after refresh with F5). Restarting gitk fixed it.Coolie
N
86

No output, nothing dangling (right?)

Note that commits referred to from your reflog are considered reachable.

What exactly is the state of that commit? How can I list all commits with similar state

Pass --no-reflogs to convince git fsck to show them to you.

How can I delete commits like those?

Once your reflog entries are expired, those objects will then also be cleaned up by git gc.

Expiry is regulated by the gc.pruneexpire, gc.reflogexpire, and gc.reflogexpireunreachable settings. Cf. git help config.

The defaults are all quite reasonable.

Nosography answered 22/9, 2010 at 0:57 Comment(10)
so you're basically saying that the reflows for dangling commits will be removed after a while automatically?Latton
Basically: yes – except that the question is a bit confused. I’m saying that all reflog entries are removed automatically after a while, but you can change that through configuration settings. And because a commit is only called dangling when it has nothing pointing to it – including reflog entries –, “reflogs for dangling commits” are not a thing. They would be “reflogs for unreachable commits”.Nosography
'They would be “reflogs for unreachable commits”.' But you said "commits referred to from your reflog are considered reachable." So how can "reflogs for unreachable commits" be a thing? I'm so confused.Chinn
Yeah, I wasn’t consistent. Normally people don’t think about the reflog, and when they say “unreachable” it implies “from a ref”. Even git help glossary defines it that way… whereas its definition for “reachable” is not narrowed down that way, so they are contradictory. Funny – so what I said is actually consistent with the confusion in gitglossary… It’s not the concepts that are confusing, though, just the terminology. The point is that “dangling” commits are ones that nothing else points to. Would it help if I say “reflogs for otherwise unreachable commits”…?Nosography
This is all very confusing. Let's make it simple. When on branch master, you do git commit, and get a commit 000001. Then you do git commit --amend, which gives you commit 000002. There are no tags or branches pointing to 000001 anymore, and you can't see it in your log without the --reflog option, but if you want, you could still get to it with git checkout 000001. Now the question is, Is 000001 a dangling commit, or an unreachable commit, or neither, or both?Copyedit
Both. Every dangling commit is an unreachable commit as well. To expand your example scenario, let’s say you commit thrice, and then you do git reset HEAD~3 to throw away all of the commits you just made. Now you have three new unreachable commits (you cannot get to them from any branch or tag or other ref), the last one of which is dangling (you cannot even get to it from another unreachable commit).Nosography
@AristotlePagaltzis re: reflogs for dangling commits are not a thing, this isn't strictly true. git fsck --dangling may not show a dangling commit that has a reflog entry, but git fsck --lost-found will. You may test this by creating a branch, making a commit, deleting the branch, running git reflog and then git fsck --lost-found. You will see dangling commit: <hash> for a commit that has a reflog entry (as well as, now, an entry in .git/lost-found/commit)Mich
Then git fsck --lost-found misuses the term. Or at least uses it in a way that contradicts its definition in git help glossary.Nosography
I did all these git reflog expire --expire-unreachable=now --all git gc --prune=now git fsck --unreachable --no-reflogs # no output git branch -a --contains <commit> # no output git show <commit> # still shows up i checked for branch --contains, tag --contains, tag --points-to, but nothing shows up except for "git show <commit>" The reflog is empty, lost+found is also empty. Any idea what else can be checked ?Auction
@SenthilAKumar: the stash, as in Leo Zhao’s answer? It’s also conceivable that you have alternates set up (which won’t be affected by any cleanup performed within your own repository).Nosography
S
33

I had the same issue, still after following all the advice in this thread:

git reflog expire --expire-unreachable=now --all
git gc --prune=now
git fsck --unreachable --no-reflogs   # no output
git branch -a --contains <commit>     # no output
git show <commit>                     # still shows up

If it's not a reflog and not a branch, ...it must be a tag!

git tag                             # showed several old tags created before the cleanup

I removed the tags with git tag -d <tagname> and redid the cleanup, and the old commits were gone.

Update: If it turns out it's not a tag, it might also be a stash! Check this answer and this answer

Spurling answered 9/3, 2017 at 14:28 Comment(6)
There's already an answer about tags (https://mcmap.net/q/12167/-listing-and-deleting-git-commits-that-are-under-no-branch-dangling), and it doesn't seem like this adds anything new. Shouldn't this be removed in favor of the earlier answer?Aftercare
Indeed, somehow I overlooked that answer. Though 4 people found my answer helpful, so maybe it's not that useless? Also I grouped all possibilities into one concise answer.Spurling
Even if duplicated, this page may appear in Google Result, and it immediately helps people with the same issue, better than just redirecting people over and over again to links that may have the correct answer.Snooker
In my case, all solutions were partial. The missing part was exactly the tags.Sometime
'If it's not a reflog and not a branch, ...it must be a tag!' saved my day!!! Thanks!Borgeson
If it's not a reflog, and not a branch, AND NOT EVEN A TAG ....it can be the stash! See this answer below https://mcmap.net/q/12167/-listing-and-deleting-git-commits-that-are-under-no-branch-danglingMatheny
L
14
git branch --contains 793db7f272ba4bbdd1e32f14410a52a412667042

probably just needs to be

git branch -a --contains 793db7f272ba4bbdd1e32f14410a52a412667042

to also report on branches from remotes

Lach answered 13/3, 2011 at 1:36 Comment(5)
thanks, now I found my remotes/origin/next that still holds this commit. how to remove it? git push -d origin next doesn't help.Disoperation
@Disoperation stackoverflow.com/questions/2003505/…Lach
thanks - the git fetch --prune did the trick. but in all answers I'm missing a check for tags that are referencing this commit. I still don't know how to check for tags with a commit (I removed all).Disoperation
But ... does this mean that commits that are only reachable from remote branches (and no local branches) are considered reachable, and therefore git fsck --unreachable is actually communicating over the network with the remote in order to find out which commits are reachable?Chinn
Answered my own question... yes, commits that are only reachable from remote branches (and no local branches) are considered reachable; but git fsck --unreachable doesn't need to communicate over the network with the remote in order to find out which remote branches contain which commits. The remote branch info is stored locally, under e.g. .git/refs/remotes/origin (or in packed-refs).Chinn
R
11

I accidentally hit the same situation and found my stashes contain reference to the unreachable commit, and thus the presumed unreachable commit was reachable from stashes.

These were what I did to make it truly unreachable.

git stash clear
git reflog expire --expire-unreachable=now --all
git fsck --unreachable
git gc --prune=now
Roulers answered 22/9, 2017 at 3:49 Comment(0)
L
8

I had a similar issue. I ran git branch --contains <commit>, and it returned no output just like in the question.

But even after running

git reflog expire --expire-unreachable=now --all
git gc --prune=now

my commit was still accessible using git show <commit>. This was because one of the commits in its detached/dangled "branch" was tagged. I removed the tag, ran the above commands again, and I was golden. git show <commit> returned fatal: bad object <commit> - exactly what I needed. Hopefully this helps someone else that was as stuck as I was.

Levorotation answered 19/5, 2016 at 23:28 Comment(2)
how did you remove the tag?Biddy
@Biddy List all the tags, find the one that references the commit in question, and then delete it. stackoverflow.com/questions/5480258/…Levorotation
M
5

TL:DR;

A commit can be referenced by:

  • a branch
  • a tag (!)
  • a stash (!!)
  • a "replace" (!!?)

Use this command to find what it is exactly:

git for-each-ref --contains <SHA>

Long version

I did a filter-repo of the repo to clean up some huge files.

Obviously ended up with some dangling commits. I tried all the answers, and nothing was working.

git branch --contains <SHA>           # nothing!
git fsck --unreachable --no-reflogs   # nothing!

Then I found and deleted a tag thanks to this answer

Still the commit kept shwoing up!

So I finally ran this command:

git for-each-ref --contains <SHA>

And it returned

1b9bf9c63209a4728b2d3dc7946da836dc331bbd commit refs/stash

Bingo!

I ran git stash - and it was (oddly) empty. How can a stash reference a commit if I have nothing in my stash?

However running git stash drop several times has deleted some god forsaken "dangling" stashes based on obsolete branches and finally I was able to gc that commit, and my repo instantly got 15MB smaller.

Longer version

(From another user) I used the tips above but it returned

4c79e05b102c673e1f60242c022ff06e017b9a1d commit refs/replace/2acd8f182e14d7d367b3469fd8751f2d9738a884

what the heck is replace?

git replace -h

shows you can list delete and edit these 'replacement objects'. Weird. Running git replace -d 2acd8f removes the commit!

Matheny answered 20/11, 2022 at 10:22 Comment(0)
F
2

git gc --prune=<date> defaults to prune objects older than two weeks ago. You could set a more recent date. But, git commands that create loose objects generally will run git gc --auto (which prunes loose objects if their number exceeds the value of configuration variable gc.auto).

Are you sure that you want to delete these commits? gc.auto's default setting will ensure that the loose objects do not take up an unreasonable amount of memory, and storing loose objects for some amount of time is generally a good idea. That way, if you realize tomorrow that your deleted branch contained a commit you needed, you can recover it.

Fosdick answered 21/9, 2010 at 23:45 Comment(0)
G
0

If the stash is truly a stash that "doesn't exist" and not a tag,
git fsck --full
may help. It worked for me when no other solution did.

(Git: Remove broken stash describes my problem more accurately than this thread)

Goodell answered 14/8, 2021 at 22:4 Comment(0)
C
0

I know this is not a good solution but I did a filter-branch and ended up with duplicate, unreachable commits which didn't belong to any branch but couldn't remove them automatically, I tried every single solution posted here, absolutely nothing worked. So I pushed to a remote repository (github), deleted my local repository and then pulled again and got rid of all those unreachable commits

Cooley answered 15/6, 2022 at 9:2 Comment(0)
S
0

First you should consider whether you really need to aggressively delete/prune all unreachable commits.

You might have gotten this message:

warning: There are too many unreachable loose objects; run 'git prune' to remove them.

And then git prune didn’t seem to solve the problem, so you are looking for knobs to delete things more aggressively.

The problem with pruning recent commits is that you can in the worst case (I don’t know how likely this is) corrupt things if you also interact with a remote.

The racy behavior occurs when a repository receives one or more pushes during this process. The main culprit is that the server advertises its objects at a different point in time from processing the objects that the client sent based on that advertisement.

[…] If one of these pushes happens before C is actually removed, then the repository can end up in a corrupt state.

So if “loose objects” is your direct problem—rather than lack of disk space, or just too many objects—then you can enable this experimental feature (as of Git 2.40.1):

git config gc.cruftPacks true

Now git gc will pack unreachable objects, which should get rid of that warning.


It seems on balance safer to use an experimental feature if it means that you won’t have a reason to prune very recent unreachable objects.

Stoic answered 8/5, 2023 at 20:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.