이미 커밋된 main branch 초기화하기


개인 프로젝트라 귀찮아서 git flow 전략을 안쓰고 대충 main에 넣었더니 후회가 빗발치기 시작했다..
고로 main branch를 깔끔하게 초기 상태로 돌리는 방법에 대해 해보겠다.

사전 세팅

  • 현재 위치가 main branch인지 확인
  • commit이 안된 파일이 있는지 확인: 아래 명령어를 오류없이 돌리기 위해서는 모든 commit이 push된 상태일 것

main branch를 초기화 시키기

# 기존 main코드를 develop으로 이전 (=코드 백업)
git checkout -b develop
git add .
git commit -m "feat: initial setup"

# 과거 기록 없는 새 branch 생성
git checkout --orphan main-clean

# 기존 파일 전부 지워버리기 (로컬에서만 삭제됨)
git rm -rf .

# 최소한의 파일 생성 및 첫 커밋
## develop의 .gitignore 복붙 추천
## 아니면 merge conflict이 나는데, develop 내용을 덮어씌우면 됨
touch .gitignore
git add .
git commit -m "feat: initial commit"

# main으로 강제임명 (기존 main은 강제로 덮어씌우기 당함)
git branch -M main

# 원격 저장소에 강제 푸시
git push -f origin main

# develop으로 이동
git checkout develop

# main을 develop으로 강제 병합 (뿌리가 달라도 허용한다는 옵션)
git merge main --allow-unrelated-histories
 
# 푸시
git push origin develop

이렇게 하면 두 branch는 공통된 역사를 공유하게 되며, develop에서 main으로 깔끔하게 merge available이 뜨는 것을 볼 수 있다.

다시는 git flow를 무시하지 말자 ^_^,,,


Author: Ruby Kim
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Ruby Kim !
Comments
  TOC