포스트

[GitHub 100일 챌린지] Day 35 - Push/Pull 트러블슈팅

[GitHub 100일 챌린지] Day 35 - Push/Pull 트러블슈팅

100일 챌린지 Day 35 - 자주 발생하는 push/pull 에러를 해결합니다

배울 내용

  1. rejected 에러 해결
  2. 충돌 해결 실전
  3. 네트워크 문제 해결

Topic1. rejected 에러 해결

push rejected = Remote가 최신이 아님

가장 흔한 에러를 해결하는 방법입니다.

에러 메시지

1
2
3
4
5
git push origin main

# 에러:
! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'origin'

의미:

1
2
Remote에 내가 모르는 커밋이 있음
먼저 pull 받아서 합쳐야 함

해결 방법 1: Pull 먼저

1
2
3
4
5
6
7
8
# 1. Remote 최신 내용 받기
git pull origin main

# 2. 충돌 없으면 자동 merge됨
# 충돌 있으면 해결

# 3. 다시 push
git push origin main

해결 방법 2: Fetch + Rebase

1
2
3
4
5
6
7
8
# 1. Fetch로 안전하게
git fetch origin

# 2. Rebase로 정리
git rebase origin/main

# 3. Push
git push origin main

해결 방법 3: Force Push (위험!)

1
2
3
4
5
# 주의: Remote 내용을 덮어씀!
git push --force origin main

# 안전한 방법:
git push --force-with-lease origin main

⚠️ 언제 force push?:

1
2
3
4
✅ 혼자 작업하는 브랜치
✅ 실수한 커밋 수정 후
❌ 팀 작업 브랜치 (절대 안 됨!)
❌ main/master 브랜치

해보기: Rejected 에러 해결

1
2
3
4
5
6
7
8
9
10
11
12
13
# 시뮬레이션

# 1. Push 시도
git push origin main
# rejected 에러!

# 2. Pull로 해결
git pull origin main
# 자동 merge 또는 충돌 해결

# 3. 다시 Push
git push origin main
# 성공!

결과: Rejected 에러를 성공적으로 해결했습니다


Topic2. 충돌 해결 실전

실제 프로젝트에서 자주 발생하는 충돌 패턴입니다.

충돌 패턴 1: 같은 줄 수정

상황:

1
2
3
4
5
// 내가 수정
const API_URL = 'https://api.mysite.com';

// 팀원이 수정
const API_URL = 'https://api.newsite.com';

충돌 파일:

1
2
3
4
5
<<<<<<< HEAD
const API_URL = 'https://api.mysite.com';
=======
const API_URL = 'https://api.newsite.com';
>>>>>>> origin/main

해결:

1
2
// 팀원과 상의 후 결정
const API_URL = 'https://api.newsite.com';

충돌 패턴 2: 파일 삭제 vs 수정

상황:

1
2
내가: index.html 삭제
팀원: index.html 수정

에러:

1
2
CONFLICT (modify/delete): index.html deleted in HEAD
and modified in origin/main.

해결:

1
2
3
4
5
6
7
8
# 파일 유지하려면
git add index.html

# 파일 삭제하려면
git rm index.html

# 커밋
git commit

충돌 패턴 3: 이진 파일 충돌

상황:

1
2
이미지 파일(.png, .jpg)
양쪽에서 수정

해결:

1
2
3
4
5
6
7
8
9
10
# 내 버전 사용
git checkout --ours image.png
git add image.png

# 또는 Remote 버전 사용
git checkout --theirs image.png
git add image.png

# 커밋
git commit

충돌 해결 도구

VS Code:

1
2
3
4
5
충돌 파일 열면:
[Accept Current Change]
[Accept Incoming Change]
[Accept Both Changes]
버튼 제공

Git GUI 도구:

1
2
3
4
# Git 기본 merge tool
git mergetool

# 설정된 도구 (예: vimdiff) 실행

해보기: 복잡한 충돌 해결

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 1. Pull 시 충돌 발생
git pull origin main
# CONFLICT in multiple files!

# 2. 충돌 파일 확인
git status
# both modified: file1.js
# both modified: file2.js
# both modified: styles.css

# 3. 하나씩 해결
vim file1.js
# 충돌 해결

git add file1.js

vim file2.js
# 충돌 해결
git add file2.js

vim styles.css
# 충돌 해결
git add styles.css

# 4. 커밋
git commit -m "Resolve merge conflicts"

# 5. Push
git push origin main

결과: 여러 파일의 충돌을 체계적으로 해결했습니다


Topic3. 네트워크 문제 해결

인증, 연결, 속도 관련 문제를 해결합니다.

문제 1: 인증 실패

에러:

1
fatal: Authentication failed

해결:

1
2
3
4
5
6
# 1. 토큰 재생성 (GitHub Settings)
# 2. 토큰 업데이트
git remote set-url origin https://TOKEN@github.com/user/repo.git

# 또는 SSH 사용
git remote set-url origin git@github.com:user/repo.git

문제 2: 연결 시간 초과

에러:

1
2
fatal: unable to access 'https://github.com/...':
Failed to connect to github.com port 443: Timed out

해결:

1
2
3
4
5
6
7
8
9
10
11
12
# 1. 프록시 확인
git config --global http.proxy
git config --global https.proxy

# 2. 프록시 제거
git config --global --unset http.proxy
git config --global --unset https.proxy

# 3. DNS 변경 (8.8.8.8)

# 4. SSH 사용
git remote set-url origin git@github.com:user/repo.git

문제 3: 대용량 파일 push 실패

에러:

1
remote: error: File large-file.zip is 120 MB; this exceeds GitHub's file size limit of 100 MB

해결:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 방법 1: 파일 제거
git rm --cached large-file.zip
git commit --amend
git push origin main

# 방법 2: Git LFS 사용
git lfs install
git lfs track "*.zip"
git add .gitattributes
git add large-file.zip
git commit -m "Use LFS for large files"
git push origin main

# 방법 3: .gitignore에 추가
echo "*.zip" >> .gitignore
git rm --cached large-file.zip
git commit -m "Remove large file"

문제 4: 느린 push/pull

원인과 해결:

1
2
3
4
5
6
7
8
9
10
11
# 원인 1: 큰 히스토리
# 해결: Shallow clone
git clone --depth 1 https://github.com/user/repo.git

# 원인 2: 많은 파일
# 해결: 압축
git gc --aggressive

# 원인 3: 네트워크
# 해결: HTTP 버퍼 크기 증가
git config --global http.postBuffer 524288000

해보기: 종합 트러블슈팅

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 체크리스트

# 1. Git 상태 확인
git status

# 2. Remote 연결 확인
git remote -v
git ls-remote origin

# 3. 인증 확인
git config user.name
git config user.email

# 4. 충돌 확인
git diff

# 5. 로그 확인
git log --oneline -5

# 6. 강제로 동기화 (최후의 수단)
git fetch origin
git reset --hard origin/main
# 주의: 로컬 변경사항 모두 삭제됨!

결과: 다양한 문제를 체계적으로 해결할 수 있습니다


정리

완료 체크:

  • rejected 에러를 해결할 수 있다
  • 다양한 충돌 패턴을 해결할 수 있다
  • 네트워크 문제를 해결할 수 있다

핵심 명령어:

1
2
3
4
5
6
7
8
git pull origin main              # rejected 해결
git push --force-with-lease       # 안전한 force push
git checkout --ours file          # 내 버전 사용
git checkout --theirs file        # Remote 버전 사용
git mergetool                     # merge 도구 실행
git remote set-url origin URL     # Remote URL 변경
git config --global http.proxy    # 프록시 설정
git lfs track "*.zip"             # LFS 사용

에러 해결 순서:

1
2
3
4
5
1. 에러 메시지 읽기
2. git status로 상태 확인
3. 해결 방법 선택
4. 테스트
5. push/pull 재시도

다음: Day 36 - git status 완벽 가이드


이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.