rebase 후에는 항상 git push --force-with-lease. git pull은 사용하지 말 것.
Answer from Chat GPT 5
# Git Rebase 후 Push 에러와 해결방법
## 문제 상황
`git rebase`를 수행한 뒤 `git push`를 하면 다음과 같은 에러가 발생한다:
```bash
➜ git push
To https://github.com/curinginnos/racketime-api.git
! [rejected] feat/group-lesson-CU-86etfu1kb -> feat/group-lesson-CU-86etfu1kb (non-fast-forward)
error: failed to push some refs to 'https://github.com/curinginnos/racketime-api.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
git pull을 실행하면 merge 커밋이 생기면서 커밋 히스토리가 꼬여버리는 문제가 발생한다.
원인
-
git rebase는 로컬 커밋 히스토리를 다시 쓴다.
-
하지만 원격 저장소에는 예전 히스토리가 남아 있어 non-fast-forward 푸시가 거부된다.
-
이때 git pull을 사용하면 자동으로 merge가 일어나 히스토리가 더 복잡해진다.
해결 방법
1. 표준 절차
# 0) 최신 원격 가져오기
git fetch origin
# 1) 내가 앞선 커밋만 있는지 확인
git rev-list --left-right --count origin/feat...feat
# 출력 예: "0 5" → 로컬이 5개 앞서고 원격은 0개 앞섬
# 2) 안전 강제 푸시
git push --force-with-lease origin feat/group-lesson-CU-86etfu1kb
- --force-with-lease: 원격에 다른 사람의 새 커밋이 있으면 푸시를 거부하여 안전하다.
2. 원격에 새 커밋이 존재할 경우
git fetch origin
git rebase origin/feat/group-lesson-CU-86etfu1kb
# 충돌 해결 후
git push --force-with-lease origin feat/group-lesson-CU-86etfu1kb
3. 이미
git pull
로 꼬였을 때 되돌리기
git reflog # rebase 직후 커밋 해시 확인
git reset --hard <rebase_끝난_커밋>
git push --force-with-lease origin feat/group-lesson-CU-86etfu1kb
재발 방지 방법
git config --global pull.rebase true
git config --global rebase.autoStash true
git config --global alias.fp "push --force-with-lease"
-
pull.rebase = true: 항상 rebase 기반 동기화
-
rebase.autoStash = true: 작업 중이던 변경사항 자동 보관
-
alias.fp: 안전 강제 푸시 단축 명령어
브랜치 보호 규칙이 걸려 있을 때
-
--force 푸시가 차단된 경우:
- 새 브랜치를 만들어 푸시
git checkout -b feat/rebased
git push origin feat/rebased
-
- 또는 보호 규칙을 일시 해제 후 --force-with-lease 한 번만 수행
빠른 진단 명령어 모음
git status -sb
git log --oneline --graph --decorate --all --max-count=25
git rev-list --left-right --count origin/feat...feat
핵심 요약
-
git rebase 후에는 반드시 git push --force-with-lease 사용.
-
절대 git pull로 병합하지 말 것.