GitLab에 있는 자료를 GitHub에 올려야 할 때가 있었는데, 서로 계정이 달라서 미러링을 하면 잔디가 옮겨 심어지지 않았다. 그래서 git filter-branch를 통해 브랜치를 재구성 하고자 했다.
git 에서는 filter-branch라고 브랜치를 재삭성 할 수 있는 기능을 제공하고 있다. 각개정에 사용자 지정 필터를 적용하여 Git 계정 기록을 다시 작성할 수 있다.
git clone git@gitlab.com:<username>/<repository>.git
cd <repository>
git filter-branch --env-filter '
OLD_EMAIL="old.email@example.com"
CORRECT_EMAIL="correct.email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
old.email@exaple.com 에는 gitlab의 이메일을 사용했고 correct.email@example.com에는 github의 이메일을 사용했다.
git push --mirror git@github.com:<username>/<repository>.git
공식홈페이지에 들어가보니 git filter-branch는 의도된 히스토리 재작성에 대한 명백하지 않은 맹글링을 생성할 수 있다고 나온다. 그래서 되도록이면 사용하지 말라고 하기 때문에 사용에 주의해야한다.