git filter repo

잘못된 커밋 내용(name, email 등)을 수정할때 git rebase 를 하나씩 pick 해서 수정했다.

그런데 git filter repo(https://github.com/newren/git-filter-repo)툴을 사용하면 편하다.

# 설치
uv pip install git-filter-repo --system

# 도움말
git filter-repo -h

# aaa.bbb 이름으로 커밋된 모든곳에 ysoftman 이름과 ysoftman@gmail.com 멜주소로 수정하기
git filter-repo --commit-callback '
if commit.author_name == b"aaa.bbb":
   commit.author_name = b"ysoftman"
   commit.author_email = b"ysoftman@gmail.com"
   commit.committer_name = b"ysoftman"
   commit.committer_email = b"ysoftman@gmail.com"
' --force

# git filter-repo가 안전을 위해 origin remote를 자동으로 제거한다.
# remote를 다시 추가하고 force push
git remote add origin https://github.com/ysoftman/ysoftman
git push --force origin main

# origin/main 을 로컬 main 브랜치로 다시 연결(트랙킹)
git branch --set-upstream-to=origin/main main

참고로 force push 후에도 gitHub에 이전 커밋 객체가 남아있어 이전 sha 해시로 직접 접근하면 여전히 보인다. 수주가 지나면 github 에서 자동으로 정리된다고 한다.

# 예시) 커밋 메시지에 Co-Authored-By: Claude 내용이 있는 라인 제거
git filter-repo --message-callback '
  import re
  return re.sub(rb"\n*Co-Authored-By:.*Claude.*\n*", b"\n", message).rstrip() + b"\n"
  ' --force

comments:

댓글 쓰기