LinuxCommandLibrary

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

--tree-filter cmd

Rewrite tree and contents.
--env-filter cmd
Modify environment.
--msg-filter cmd
Rewrite commit messages.
--subdirectory-filter dir
Extract subdirectory.
--prune-empty
Remove empty commits.
-f, --force
Force overwrite backup.

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

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community