AndiDog
AndiDog

Reputation: 70218

Remove stale commits from Git

I have made some test commits (to test a commit hook) and always reset the index to my last normal commit. Now these stale commits still exist:

> git reflog
fcdabf7 HEAD@{0}: reset: moving to fcdabf7e01845d6f000fc3cef2edc999c57a7e29
5c97564 HEAD@{1}: commit: t
fcdabf7 HEAD@{2}: reset: moving to fcdabf7e01845d6f000fc3cef2edc999c57a7e29
ae52246 HEAD@{3}: commit: t
fcdabf7 HEAD@{4}: reset: moving to fcdabf7e01845d6f000fc3cef2edc999c57a7e29
c58aeef HEAD@{5}: commit: t
fcdabf7 HEAD@{6}: reset: moving to fcdabf7e01845d6f000fc3cef2edc999c57a7e29
3a2cc3b HEAD@{7}: commit: test

How can I remove them? And for technical understanding: If I leave them alone, will they be pushed to the upstream repo if I do a push?

Upvotes: 3

Views: 1157

Answers (2)

jørgensen
jørgensen

Reputation: 10579

Commits referenced by the reflog entries are not dangling per se. To clear the reflog,

git reflog expire --expire=0 --all

afterwards which git prune can be used to remove the - now-dangling - commits.

Upvotes: 1

manojlds
manojlds

Reputation: 301477

First of all, you need not worry about the dangling commits making their way into a remote repository. They will not be pushed, and will generally be cleaned up in due course.

However, if you want to clear off such dangling object, try running:

git gc --prune=now

Upvotes: 1

Related Questions