Aug 19, 2008

How to change author of git commit?


I recently needed to rewrite history of commits in a private
Git repository, because I wanted to change my email address in every commit. Note that you should not try following tip in a repository that anyone has pulled from. Normally Git doesn't allow you to do this kind of thing, since changing authorship is...well bad (usually also against the law).

Let's assume that email address changed from dev@comp1.com to dev@comp2.com. To create copy of repository $PROJECT_DIR to new repository $NEW_REPO with changed emails we can do following:

$ cd $PROJECT_DIR # change to project repository

$ git fast-export --all > /tmp/project.git # export repository to temporary file

$ sed 's/^author\(.*\)<dev@comp1.com>/author\1<dev@comp2.com>/' /tmp/project.git # replace emails on every line starting with 'author'

$ cd $NEW_REPO # change to empty directory

$ git init # initialize git

$ git fast-import < /tmp/project.git # import modified repository

Third step is potentially dangerous, because you have to make sure that you don't edit contents of any file aside from metadata. If you change content of files, git fast-import will complain because sha1 hash will not be correct.

Be sure to read git fast-import and git fast-export man pages for additional information. It took me a while playing with git-rebase and similar stuff to realize that they do not offer such feature, so if this tip helps anyone else I'll be glad.
Share/Save/Bookmark

3 comments:

Post a Comment
  1. Thanks! You rock!

  2. Thanks, your post gave me a great hint how to change author information.
    Reading the manpage of git-fast-export pointed me to git-filter-branch which is what i've done in the end:

    git-filter-branch --env-filter "export GIT_AUTHOR_NAME='New name'; export GIT_AUTHOR_EMAIL='New email'" HEAD

    This seems to work great, for replacing all author information, a bit more shell scripting would allow more selective changes.

  3. This is a great script to rename git user :
    http://coffee.geek.nz/how-change-author-git.html