Binary Lion Studios

I code for fun and for food.

Rewrite history

Hypothetical situation: say you work on a new project with git, do some 20 commits while working on it, and now it is in a good place and ready to be pushed to github. When you push it, you realize you forgot to set your name and email in the git global config. Now if you look at the history on git, you see a lot of [unknown]s. That’s no good. Here is how to fix that:

Note: this is not a good idea if there is more than just you with a clone of your repository. Proceed with caution.

1
2
3
4
5
6
7
8
9
10
11
12
13
$ git filter-branch --commit-filter '
> if [ "$GIT_COMMITTER_NAME" = "unknown" ];
> then
> GIT_COMMITTER_NAME="Your Name";
> GIT_AUTHOR_NAME="Your Name";
> GIT_COMMITTER_EMAIL="you@example.com";
> GIT_AUTHOR_EMAIL="you@example.com";
> git commit-tree "$@";
> else
> git commit-tree "$@";
> fi' --tag-name-filter cat -- --all
$ git pull
$ git push -f

This will rewrite the commit history and give you credit for all that work. While you’re at it, you should probably:

1
2
$ git config --global user.name "Your Name"
$ git config --global user.email "you@example.com"

Also generally handy:

1
2
$ git reset 
$ git push -f