Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - remove commits with empty changeset using filter-branch

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.

like image 200
Paul Pladijs Avatar asked Mar 16 '11 11:03

Paul Pladijs


People also ask

How do you delete a changeset in git?

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.

How do I remove old commits from a branch?

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.

What is the purpose of git filter branch`?

git-filter-branch can be used to get rid of a subset of files, usually with some combination of --index-filter and --subdirectory-filter .

What is an empty commit?

And empty commit is a commit without any actual changes.


2 Answers

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.

like image 172
Cascabel Avatar answered Oct 20 '22 16:10

Cascabel


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
like image 51
CB Bailey Avatar answered Oct 20 '22 17:10

CB Bailey