git-filter-branch

Rewrite branch history by applying filters

TLDR

Remove file from history
$ git filter-branch --tree-filter 'rm -f [file]' HEAD
Rewrite author
$ git filter-branch --env-filter 'GIT_AUTHOR_EMAIL="[new@email]"' HEAD
Subdirectory filter
$ git filter-branch --subdirectory-filter [dir] HEAD
Remove empty commits
$ git filter-branch --prune-empty HEAD
Force rewrite
$ git filter-branch -f --tree-filter '[command]' HEAD

SYNOPSIS

git filter-branch [options] [--] [rev-list]

DESCRIPTION

git filter-branch is a powerful but deprecated tool for rewriting Git history by applying filter commands to every commit in a branch. It walks through the entire commit history, allowing modifications to trees, commit messages, author information, or other metadata.The subdirectory-filter is particularly useful for extracting a subdirectory into a new repository with its history intact. Performance is notably poor on large repositories because it must check out every commit's tree. This limitation led to the development of git-filter-repo as the official replacement.

PARAMETERS

--env-filter cmd

Rewrite author/committer environment variables (name, email, date).
--tree-filter cmd
Rewrite tree and contents; checks out each commit, so slow on large repos.
--index-filter cmd
Rewrite the index without checking out the tree; much faster than --tree-filter. Commonly used with git rm --cached.
--parent-filter cmd
Rewrite a commit's parent list.
--msg-filter cmd
Rewrite commit messages.
--commit-filter cmd
Replace the commit-creation step entirely.
--tag-name-filter cmd
Rewrite tag names pointing at rewritten commits.
--subdirectory-filter dir
Only rewrite history touching dir, and make it the new project root.
--prune-empty
Remove commits that become empty after filtering.
--original namespace
Namespace for backup refs (default refs/original).
-d dir
Temporary directory to use (e.g. tmpfs, for I/O speed).
-f, --force
Force the operation, overwriting an existing backup namespace.

INSTALL

sudo apt install git
sudo dnf install git
sudo pacman -S git
sudo apk add git
sudo zypper install git
brew install git
nix profile install nixpkgs#git

CAVEATS

Officially deprecated in favor of git-filter-repo. Slow on large repositories. Changes history, invalidating commit hashes and requiring force pushes. Collaborators must reclone. Creates backup refs that must be manually cleaned.

SEE ALSO

git-filter-repo(1), git-rebase(1), git-rm(1)

RESOURCES

Source code · Homepage · Documentation