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.
Use git rebase -i <commit-id> , where id is a commit before the changeset you want to remove. It will open a list of commits between the head and the changeset, delete from the list the commit you want and continue.
Removing the last commit To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits.
git-filter-branch can be used to get rid of a subset of files, usually with some combination of --index-filter and --subdirectory-filter .
And empty commit is a commit without any actual changes.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With