LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

git-filter-branch

Rewrite branch history by applying filters

TLDR

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

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
copy
sudo dnf install git
copy
sudo pacman -S git
copy
sudo apk add git
copy
sudo zypper install git
copy
brew install git
copy
nix profile install nixpkgs#git
copy

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

RESOURCES

Copied to clipboard
Kai