Member-only story

Using different Git accounts in the same computer

Xoel López Barata
2 min readNov 19, 2020

--

When you use different emails for personal and work projects, it’s easy to mess up your Git repos committing to them with the wrong user. This is how to fix that

Photo by Todd Quackenbush on Unsplash

If it’s already happened

If you’ve committed to a personal project (with only you as a committer) with your work email, here’s how to rewrite history for that repo (from this Stackoverflow answer).

This is the one I used and worked like a charm.

git filter-branch -f --env-filter "
GIT_AUTHOR_NAME='Newname'
GIT_AUTHOR_EMAIL='new@email'
GIT_COMMITTER_NAME='Newname'
GIT_COMMITTER_EMAIL='new@email'
" HEAD

If there are multiple committers, don’t use the previous one or you’ll rewrite also the commits that weren’t made by you, use this one instead (source)

git filter-branch --commit-filter '
if [ "$GIT_COMMITTER_NAME" = "<Old Name>" ];
then
GIT_COMMITTER_NAME="<New Name>";
GIT_AUTHOR_NAME="<New Name>";
GIT_COMMITTER_EMAIL="<New Email>";
GIT_AUTHOR_EMAIL="<New Email>";
git commit-tree "$@";
else
git commit-tree "$@";
fi' HEAD

--

--

Xoel López Barata
Xoel López Barata

Written by Xoel López Barata

Freelance data scientist and software developer. On Twitter: @xoelipedes.

No responses yet