Git - remove commits with empty changeset using filter-branch

21,369

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
Share:
21,369

Related videos on Youtube

Paul Pladijs
Author by

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, 2022

Comments

  • Paul Pladijs
    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
      vmrob over 9 years
      For future visitors, --index-filter with git 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
      Ger Hobbelt over 7 years
      And 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 the filter-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 report Found nothing to rewrite
  • Fabio
    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
    SeB.Fr about 10 years
    This command does not remove all empty commits in my case but unfortunately I do not know why. Any other idea ?
  • CB Bailey
    CB Bailey over 9 years
    @SeB.Fr: I don't know if this is your situation but possibly this ?
  • Alexander Puchkov
    Alexander Puchkov almost 7 years
    It 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
    Alexander Puchkov almost 7 years
    Update: it didn't work on Windows 10, but luckily there's ubuntu inside Windows 10 anniversary update. Worked well there
  • bart
    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
    Vladimir Vukanac about 3 years
    Before 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
    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.