git push
한 줄 요약
로컬 커밋을 원격 저장소에 업로드
선행 지식
- Remote란 - 원격 저장소 개념
- Commit - 커밋의 개념
- git remote - 원격 저장소 연결
기본 사용법
git push <remote> <branch>
git push origin main주요 옵션
| 옵션 | 설명 | 예시 |
|---|---|---|
| (없음) | 기본 Remote로 push | git push |
-u | upstream 설정 | git push -u origin main |
--force / -f | 강제 push | git push -f origin main |
--force-with-lease | 안전한 강제 push | git push --force-with-lease |
--delete | 원격 브랜치 삭제 | git push origin --delete feature |
--tags | 태그 push | git push --tags |
--all | 모든 브랜치 push | git push --all |
실제 예제
첫 push (upstream 설정)
# 새 저장소 첫 push
$ git push -u origin main
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (5/5), 420 bytes | 420.00 KiB/s, done.
Branch 'main' set up to track remote branch 'main' from 'origin'.-u (또는 --set-upstream)을 사용하면:
- 로컬 브랜치와 원격 브랜치 연결
- 이후
git push만으로 push 가능
일반 push
# upstream이 설정된 후
git push
# 또는 명시적으로
git push origin main새 브랜치 push
# 로컬에서 만든 브랜치를 원격에 생성
git push -u origin feature/login원격 브랜치 삭제
git push origin --delete feature/old-branch
# 또는
git push origin :feature/old-branchpush 흐름 이해
Before push:
┌─ Local ──────────────────────────┐
│ main: [c1]──▶[c2]──▶[c3]──▶[c4] │
└──────────────────────────────────┘
┌─ Remote (origin) ────────────────┐
│ main: [c1]──▶[c2] │
└──────────────────────────────────┘
After push:
┌─ Local ──────────────────────────┐
│ main: [c1]──▶[c2]──▶[c3]──▶[c4] │
└──────────────────────────────────┘
┌─ Remote (origin) ────────────────┐
│ main: [c1]──▶[c2]──▶[c3]──▶[c4] │
└──────────────────────────────────┘
push 거부 상황
원격에 내가 없는 커밋이 있으면 push가 거부됩니다:
$ git push
To https://github.com/user/repo.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/user/repo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart.해결 방법 1: pull 후 push (권장)
git pull origin main
# 충돌이 있다면 해결
git push origin main해결 방법 2: 강제 push (⚠️ 주의)
# 원격의 커밋을 덮어씀! 협업 시 위험!
git push -f origin main강제 push 주의
- 다른 사람의 커밋이 사라질 수 있음
- 협업 브랜치에서는 사용 금지
- 개인 브랜치에서만 사용
안전한 강제 push
# 내가 마지막으로 본 상태에서만 강제 push
git push --force-with-lease origin main다른 사람이 push한 커밋이 있으면 실패합니다.
push 권한 문제
인증 실패
$ git push
remote: Permission denied.
fatal: unable to access '...'해결:
- GitHub 로그인 상태 확인
- Personal Access Token 사용 (HTTPS)
- SSH 키 설정 확인 (SSH)
권한 없음 (다른 사람 저장소)
remote: Permission to user/repo.git denied.해결:
- Fork 후 내 저장소에 push
- 또는 Collaborator로 추가 요청
자주 하는 실수
”everything up-to-date” 메시지
$ git push
Everything up-to-date원인: 커밋할 것이 없음
확인:
git status # 커밋 안 한 변경사항?
git log origin/main..main # push 안 한 커밋?잘못된 브랜치로 push
예방:
# 현재 브랜치 확인
git branch
# 명시적으로 브랜치 지정
git push origin featureamend 후 push 실패
git commit --amend
git push
# rejected!이유: 히스토리가 변경되어 fast-forward 불가
해결 (개인 브랜치만):
git push -f origin feature→ push를 취소하고 싶을 때 참고
관련 명령어
- git remote - Remote 관리
- git pull과 fetch - 원격에서 가져오기
- git commit - 커밋 생성
더 알아보기
- Remote란 - 원격 저장소 개념
- Pull Request란 - push 후 PR 생성
- push를 취소하고 싶을 때 - push 되돌리기