Git - remove commits with empty changeset using filter-branch
Solution 1
Just add on the --prune-empty
option:
git filter-branch --tree-filter 'rm -rf my_folder' --prune-empty -f HEAD
(And of course, if you have other refs, you might want to rewrite everything with -- --all
instead of just HEAD
.)
Note that this isn't compatible with --commit-filter
; in that case, Charles Bailey has your answer.
Solution 2
Just looking a the documentation for filter-branch
, you should be able to do this:
git filter-branch --commit-filter 'git_commit_non_empty_tree "$@"' HEAD
Related videos on Youtube
Paul Pladijs
I'm passionate about delivering high quality software in the best and most pragmatic way. Quality, Continuous Improvement & Growth
Updated on July 05, 2022Comments
-
Paul Pladijs over 1 year
How do I remove commits which have no changeset using git filter-branch?
I rewrote my git history using:
git filter-branch --tree-filter 'rm -r -f my_folder' -f HEAD
this worked out well but now I have lots of commits with empty changesets. I would like to remove those commits. Preferably in msysgit.
Rebasing is not really an option because I have over 4000 commits and half of them must be removed.
-
vmrob over 9 yearsFor future visitors,
--index-filter
withgit rm -r --ignore-unmatch --cached my_folder
is much faster than--tree-filter
as it doesn't have to check out each revision. It operates entirely on the index. -
Ger Hobbelt over 7 yearsAnd in case you already did the work (
--tree-filter ...
) and now just want to get rid of the lingering empty commits, this saved my bacon:git filter-branch --prune-empty
(just thefilter-branch
command without any 'filter') -- as found in stackoverflow.com/questions/28313664/… -- this is useful when Jefromi's answer below (stackoverflow.com/questions/5324799/…) produces the error reportFound nothing to rewrite
-
-
Fabio almost 13 years+1 for this. Very useful if you have converted an svn repository, which normally generates a lot of empty commits
-
SeB.Fr about 10 yearsThis command does not remove all empty commits in my case but unfortunately I do not know why. Any other idea ?
-
CB Bailey over 9 years@SeB.Fr: I don't know if this is your situation but possibly this ?
-
Alexander Puchkov almost 7 yearsIt throws this message for me: fatal: ambiguous argument '$@'': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]'
-
Alexander Puchkov almost 7 yearsUpdate: it didn't work on Windows 10, but luckily there's ubuntu inside Windows 10 anniversary update. Worked well there
-
bart over 5 years@AlexanderPuchkov That is because you tried it in the Windows CMD prompt. It just happened to me too. If you had done it in the Git bash shell, it would have worked.
-
Vladimir Vukanac about 3 yearsBefore proceeding make an backup branch from your current. This will literary recreate every commit with new initial commit -> I got 2 completely separated trees. Definitely this is NOT useful to remove just one simple empty commit!
-
TamaMcGlinn over 2 years@VladimirVukanac that is expected if the first commit happened to be an empty commit. Note there is also a rebase option that allows you to specify an arbitrary bash script to be run prior to committing. That might be a better option, also because you can easily specify a starting point to the rebase action, hence leaving all earlier commits intact.