When dealing with git on a day-to-day basis, you may see new things which you have never seen before.
One of such things is a dangling commit.
Let's see what dangling commits are and not get confused, the next time you see them.
What is a dangling commit?
You can define a dangling commit as a commit which exists in your git repository but it's not reachable from any branch tag or HEAD.
So it is essentially a commit that is floating around with no branch pointing to it.
These can be created as a result of the following
- Resetting a branch
- Rebasing
- Deleting a branch
Now let's see how we can see dangling commits.
How to see dangling commits
You can run the following command
git fsck --lost-found
When executing, you may see messages like :
dangling commit 1a2b3c....
So how does git handle this? Let's see.
How does Git deal with dangling commits
Git garbage collection will delete unreachable commits after 30 days.
Before it wipes the commits away, if you need any commit you can do the following.
Checkout to the commit
git checkout dangling_commit
Create a branch from it.
git branch restore-branch <sha>
Wrapping up
So that's it about this article, I made it based on something I encountered recently and I thought there will be others discovering such new terms now and then.
If you have read till here, let me suggest you a handy tool.
If you’ve ever struggled with repetitive tasks, obscure commands, or debugging headaches, this platform is here to make your life easier. It’s free, open-source, and built with developers in mind.
👉 Explore the tools: FreeDevTools
👉 Star the repo: freedevtools

Top comments (0)