PHP Manual
/
Experience from practice

Changing commit ownership in Git

08. 03. 2022

When migrating repositories between organizations, it often happens that we need to overwrite commit owners. The reason for this can be to transfer commits from one account to another, for example, because of a change in the user's email address.

For example, I needed to transfer all commits from my old email account on List to my second Gmail account. The second case where I might request such a change is when I accidentally commit under a private email, but a particular company wants commits under their domain.

Fortunately, there is a command to solve this problem, which I can just call in the project master to overwrite the entire history:

git filter-branch --env-filter "
if [ \"\$GIT_COMMITTER_EMAIL\" = \"janbarasek@seznam.cz\" ]
then
    export GIT_COMMITTER_NAME="Jan Barášek\"
    export GIT_COMMITTER_EMAIL="janbarasek@gmail.com\"
fi
if [ \"\$GIT_AUTHOR_EMAIL\" = \"janbarasek@seznam.cz\" ]
then
    export GIT_AUTHOR_NAME="Jan Barášek\"
    export GIT_AUTHOR_EMAIL="janbarasek@gmail.com\"
fi
" $@ --tag-name-filter cat -- --branches --tags

After the command is executed, the changes need to be flushed to the master with git push -f.

Warning:

After the command is executed, the entire commit history is overwritten and the hashes are changed. This is a BC break that should only occur rarely. If you make a mistake when overwriting commits, the history cannot be restored. At the same time, you must delete or overwrite all branches, otherwise there will be a conflict over all changed commits, which will be double-written (original and new commit) when resolved.

Jan Barášek   Více o autorovi

Autor článku pracuje jako seniorní vývojář a software architekt v Praze. Navrhuje a spravuje velké webové aplikace, které znáte a používáte. Od roku 2009 nabral bohaté zkušenosti, které tímto webem předává dál.

Rád vám pomůžu:

Související články

1.
5.
Status:
All systems normal.
2024