2014-07-29

How to discard committer info from git history

This blog post explains how to overwrite the git committer name, git committer e-mail and git committer date in previous commits in a git repository. This is useful e.g. after a series of git commit -C ... calls, which copy the author name, e-mail and date from the specified commit, but use the committer name, e-mail and date from the environment of the command.

Please note that rewriting git history is potentially dangerous because it can lead to data loss and synchronization issues with others who pull from the repository. Read more about it in Git Tools – Rewriting History.

First make sure you don't have any uncommitted changes: git status -s shouldn't print anything.

Then checkout the relevant branch, and run this command on a Unix systems (or within Git Bash on Windows), without the leading $:

$ git filter-branch -f --commit-filter '
    GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"
    GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
    GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
    git commit-tree "$@"' HEAD

Repeat running this command in each local branch you care about. For each branch, it will rewrite the history of the entire branch from its roots.

If you are sure that you didn't have a data loss, then run:

$ rm -rf .git/refs/original

No comments:

Post a Comment