다른 사람 코드 가져오기

이럴 때 참고하세요

“팀원이 올린 코드를 받고 싶어요” / “다른 저장소를 복제하고 싶어요”

상황별 빠른 해결

처음 프로젝트 가져오기

git clone https://github.com/user/repo.git
cd repo

최신 코드 받기

git pull origin main

확인 후 병합하기

git fetch origin
git log origin/main --oneline -5
git merge origin/main

상세 가이드

1. 새 프로젝트 복제

처음 프로젝트를 가져올 때:

# HTTPS
git clone https://github.com/user/awesome-project.git
 
# SSH
git clone git@github.com:user/awesome-project.git
 
# 폴더 이름 지정
git clone https://github.com/user/repo.git my-project

git clone 참고

2. 최신 변경사항 받기

이미 clone한 프로젝트의 업데이트:

# 바로 병합
git pull origin main
 
# 또는 rebase로
git pull --rebase origin main

git pull과 fetch 참고

3. 먼저 확인하고 병합

변경사항을 검토 후 병합하고 싶을 때:

# 1. 다운로드만
git fetch origin
 
# 2. 어떤 변경이 있는지 확인
git log main..origin/main --oneline
 
# 3. 변경 내용 보기
git diff main origin/main
 
# 4. 병합
git merge origin/main

4. 다른 브랜치 가져오기

# 원격 브랜치 목록 확인
git branch -r
 
# 특정 브랜치 체크아웃
git switch -c feature origin/feature
 
# 또는
git checkout -b feature origin/feature

5. Fork한 저장소에서 원본 변경사항 가져오기

# 1. 원본 저장소를 upstream으로 추가
git remote add upstream https://github.com/original/repo.git
 
# 2. 원본 변경사항 가져오기
git fetch upstream
 
# 3. 내 main에 병합
git switch main
git merge upstream/main
 
# 4. 내 원격 저장소에 push
git push origin main

충돌이 발생하면?

$ git pull
Auto-merging app.js
CONFLICT (content): Merge conflict in app.js

해결 순서

# 1. 충돌 파일 확인
git status
 
# 2. 파일 열어서 수정 (마커 제거)
 
# 3. 해결 완료
git add app.js
git commit
 
# 4. push
git push

충돌 해결하기 참고


상황별 팁

특정 파일만 가져오기

# 원격의 특정 파일만
git fetch origin
git checkout origin/main -- path/to/file.js

특정 커밋만 가져오기

git cherry-pick a1b2c3d

과거 버전으로 가져오기

# 특정 태그 버전
git clone --branch v1.0.0 https://github.com/user/repo.git
 
# 특정 커밋
git clone https://github.com/user/repo.git
git checkout a1b2c3d

자주 하는 실수

pull 시 “uncommitted changes” 에러

error: Your local changes would be overwritten by merge

해결:

# 방법 1: 커밋 후 pull
git add .
git commit -m "WIP"
git pull
 
# 방법 2: stash 사용
git stash
git pull
git stash pop

잘못된 브랜치에서 pull

# 현재 브랜치 확인
git branch
 
# 올바른 브랜치로 이동
git switch main
git pull

관련 문서

더 알아보기