20대 마무리

[Git] GitHub 관리를 위한 기본 정리 - branch명 이슈 해결 (master to main) 본문

오류 및 환경

[Git] GitHub 관리를 위한 기본 정리 - branch명 이슈 해결 (master to main)

AndaCondaa 2022. 6. 5. 01:06

깃허브 커밋하는데 자꾸 git push 명령어에서 오류가 발생했고, 깃허브 레포지토리에도 업로드가 되지 않는 문제가 발생했다.

 

branch명이 깃허브 페이지와 Git이 서로 달라서 생기는 문제임을 알게됐고 이를 해결했다.

(기존 default branch name인 master는 Racism 이슈가 있어서 Github에서 main으로 변경되었지만, 아직 git CLI에서는 master로 되어있고, branch name이 다른 상태로 push 했기 때문에 발생한 문제였다.)

 

설정 단계

 

1. 새 Repository 생성

2. Git 실행 (Git bash here)

3. git init

4. git remote -v

5. git remote rm origin

6. git remote add origin https://github.com/[아이디]/[repository이름.git]  

7. git branch

8. git branch -m [현재 branch name] [변경 branch name]

9. git add [추가하려는 것]

10. git commit -m "commit message"

11. git push origin [branch name]

 

 

*********  추가로 repository를 만들 때 readme파일을 생성했다면, 해당 파일은 이전 branch name과 충돌하여 branch name을 변경하더라도 git push 오류가 발생할 수 있다. 따라서 readme 파일을 생성한 경우에는 8번에서 branch name을 변경한 후 pull을 해주어야 한다.

 

git pull origin [branch name]

근데 여기서 순서가 잘못된 경우에는 

fatal:refusing to merge unrelated histories 오류가 발생하는데, 이때는 다음과 같이 해결한다.

 

git pull origin [branch name] --allow-unrelated-histories

이후 add 부터 시작하면 진행이 될 것이다.

 

 

설정 후

1. git pull origin [branch name]

2. git add [add file]

3. git commit -m "commit message"

4. git push origin [branch name]