모든 git history 지우기

오랫동안 사용한 git 프로젝트가 있는데, 시간이 많이 지나다 보니 history 용량이 많아져 clone 받는데 많은 시간이 걸렸다. 개인 프로젝트고 history 참조 안해도 되서 삭제하기로 결정

# 쉽고 간단한 방법

깃정보를 저장하고 있는 .git 폴더를 지우고 force push. 가끔 문제가 생기는 경우도 있으니 커맨드 실행전 백업해 놓을것. 참고로 submodule을 포함하고 있을 경우 작동하지 않는다.

rm -rf .git
git init
git add .
git commit -m "Initial commit"
git remote add origin <github-uri>
git push -u --force origin main 
1
2
3
4
5
6

# 안전한 방법

git checkout --orphan temp_branch
git add .
git commit -am "commit message"
git branch -D main
git branch -m main
git push -f origin main
1
2
3
4
5
6

# 그외 방법

git branch newbranch $(echo "commit message" | git commit-tree HEAD^{tree}) | git push --force origin newbranch:main
1