git push rejected non-fast-forward 해결
git push rejected (fetch first · non-fast-forward) 는 원격에 내게 없는 커밋이 있어서입니다. pull --rebase 로 잇고 강제는 --force-with-lease 로.
! [rejected] main -> main (fetch first)error: failed to push some refs to '/srv/remote.git'원격 브랜치에 내 로컬에 없는 커밋이 생겨서, 그대로 올리면 그 커밋이 사라지기 때문에 Git 이 푸시를 거절한 것입니다. git pull --rebase 로 원격 커밋 위에 내 커밋을 다시 얹고 푸시하면 됩니다. 내가 이미 올린 커밋을 고쳐 쓴(amend·rebase) 경우에만 강제 푸시가 필요하고, 그때도 --force 대신 --force-with-lease 를 씁니다.
[1] 괄호 안 문구로 상황을 가립니다. 두 문구는 같은 문제를 다른 시점에 본 것입니다.
| 괄호 안 | 뜻 |
|---|---|
(fetch first) | 원격의 새 커밋을 로컬이 아직 받지도 않았습니다 |
(non-fast-forward) | 받긴 했지만(git fetch) 내 브랜치에 합치지 않았습니다 |
(stale info) | --force-with-lease 가 막았습니다 — [5] |
(remote ref updated since checkout) | --force-if-includes 가 막았습니다 — [6] |
git fetch 만 하고 다시 올리면 문구만 바뀌고 똑같이 거절됩니다.
$ git fetch originFrom /srv/remote 45604ee..48d7eb6 main -> origin/main$ git push origin mainTo /srv/remote.git ! [rejected] main -> main (non-fast-forward)error: failed to push some refs to '/srv/remote.git'hint: Updates were rejected because the tip of your current branch is behindhint: 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.[2] 원격에 무엇이 새로 왔는지 봅니다. 합치기 전에 남의 커밋을 확인해 두면 충돌이 나도 당황하지 않습니다.
$ git status -sb## main...origin/main [ahead 1, behind 1]$ git log --oneline main..origin/main48d7eb6 B: add b.txtahead 1, behind 1 은 내 쪽에만 커밋 1개, 원격 쪽에만 1개 있다는 뜻입니다. behind 만 있고 ahead 가 없으면 내 커밋이 없으니 git pull 로 따라가기만 하면 됩니다.
[3] git pull --rebase 로 내 커밋을 원격 커밋 뒤로 옮기고 푸시합니다. 옵션도 설정도 없이 git pull 하면 git 2.43 은 병합할지 rebase 할지 고르라는 안내와 함께 멈춥니다.
$ git pullhint: You have divergent branches and need to specify how to reconcile them.…fatal: Need to specify how to reconcile divergent branches.$ git pull --rebaseSuccessfully rebased and updated refs/heads/main.$ git log --oneline --graph* a6e72fe A: add a.txt* 48d7eb6 B: add b.txt* 45604ee first$ git push origin mainTo /srv/remote.git 48d7eb6..a6e72fe main -> main매번 고르기 싫으면 git config --global pull.rebase true 로 기본값을 정합니다. 아직 아무에게도 올리지 않은 로컬 커밋만 옮겨지므로, 이 상황의 rebase 는 남의 이력을 바꾸지 않습니다.
[4] 이력에 합친 흔적을 남기려면 병합합니다. 팀 규칙이 병합 커밋을 쓰는 쪽이면 이쪽입니다.
$ git pull --no-rebase --no-editFrom /srv/remote a6e72fe..7fad772 main -> origin/mainMerge made by the 'ort' strategy. b.txt | 1 + 1 file changed, 1 insertion(+)$ git log --oneline --graph -5* a8b95e2 Merge branch 'main' of /srv/remote|\ | * 7fad772 B: edit b.txt* | a9dfe59 A: edit a.txt|/ * a6e72fe A: add a.txt* 48d7eb6 B: add b.txt같은 줄을 양쪽에서 고쳤다면 rebase 도 병합도 충돌에서 멈춥니다. 고칠 자신이 없으면 되돌리고 다시 생각합니다.
$ git pull --rebase…CONFLICT (content): Merge conflict in app.txterror: could not apply e3b236e... A: app.txt…$ git rebase --abort; git status -sb## main...origin/main [ahead 1, behind 1]--abort 뒤에는 pull 전 상태로 돌아갑니다. 충돌을 풀 때는 파일을 고치고 git add app.txt → git rebase --continue 입니다(병합이면 git commit).
[5] 올린 커밋을 고쳐 썼을 때만 강제 푸시합니다. git commit --amend 나 대화형 rebase 뒤에는 원격에 있던 커밋이 내 이력에 없으니 pull 하지 말고(같은 변경이 두 번 들어갑니다) --force-with-lease 로 올립니다.
$ git commit --amend -m "A: typo fixed"[main f42458d] A: typo fixed…$ git push origin mainTo /srv/remote.git ! [rejected] main -> main (non-fast-forward)…$ git push --force-with-lease origin mainTo /srv/remote.git + c8ce267...f42458d main -> main (forced update)--force-with-lease 는 「원격 브랜치가 내가 마지막으로 본 그대로일 때만」 덮어씁니다. 그 사이 다른 사람이 올렸으면 이렇게 거절합니다. --force 였다면 그 사람의 커밋이 조용히 지워졌을 자리입니다.
$ git commit --amend -m "A: typo fixed again"[main 7ae8b4e] A: typo fixed again…$ git push --force-with-lease origin mainTo /srv/remote.git ! [rejected] main -> main (stale info)error: failed to push some refs to '/srv/remote.git'되돌릴 길을 남겨 두려면 덮어쓰기 전에 원격에 있던 커밋을 ref 하나로 보존하고, 그 값을 기대값으로 줍니다. 잘못 덮어썼으면 그 ref 를 다시 올려 원래대로 돌립니다. 아래는 따로 만든 시험 저장소에서 돌린 것이라 해시가 위와 다릅니다.
$ git fetch origin$ git update-ref refs/backup/main-before-force origin/main$ git log --oneline -1 refs/backup/main-before-force6d5d612 A: add line$ git push --force-with-lease=main:refs/backup/main-before-force origin mainTo /srv/remote.git + 6d5d612...75f5a8d main -> main (forced update)$ git push --force-with-lease=main:HEAD origin refs/backup/main-before-force:mainTo /srv/remote.git + 75f5a8d...6d5d612 refs/backup/main-before-force -> main (forced update)$ git log --oneline -2 origin/main6d5d612 A: add lineffe1da0 first--force-with-lease=main:<기대값> 은 원격 main 이 정확히 그 커밋일 때만 덮어쓰므로, 뒤에서 fetch 가 돌아도 보호가 남습니다. 마지막 두 명령이 복원입니다(원격이 내가 올린 HEAD 일 때만 보존해 둔 커밋으로 되돌림). 다른 사람이 그 사이 내 커밋 위에 올렸다면 복원도 거절되니, 그때는 그 사람과 먼저 맞춥니다. 끝나면 git update-ref -d refs/backup/main-before-force 로 지웁니다.
[6] 편집기·IDE 가 뒤에서 git fetch 를 돈다면 --force-if-includes 를 함께 붙입니다. fetch 로 원격 추적 브랜치가 새로 바뀌면 --force-with-lease 는 그 새 값을 「본 것」으로 쳐서 남의 커밋을 덮어쓰게 됩니다. 아래 --dry-run 이 그 상황입니다.
$ git fetch originFrom /srv/remote f42458d..860426d main -> origin/main$ git push --force-with-lease --dry-run origin mainTo /srv/remote.git + 860426d...7ae8b4e main -> main (forced update)$ git push --force-with-lease --force-if-includes origin mainTo /srv/remote.git ! [rejected] main -> main (remote ref updated since checkout)error: failed to push some refs to '/srv/remote.git'…$ git log --oneline -3 origin/main860426d B: b3f42458d A: typo fixed960b02c B: app.txt--force-if-includes 는 원격 커밋이 내 브랜치 reflog 에 들어온 적이 있는지까지 봅니다. 받아만 놓고 합치지 않은 B: b3 를 지키고 거절했습니다. git 2.30 에 들어온 옵션이고, git config --global push.useForceIfIncludes true 로 늘 켤 수 있습니다. git 공식 문서도 값 없는 --force-with-lease 는 백그라운드 fetch 와 「아주 나쁘게」 맞물린다고 적고 있습니다.
로컬에서 git init 한 저장소를 처음 올리다 이 오류를 만났고 pull 에서 refusing to merge unrelated histories 가 나오면 그 글로 넘어갑니다. 거절 문구가 Permission denied (publickey) 면 권한이 아니라 인증 문제입니다 — GitHub Permission denied (publickey) 해결.
공식 근거: git-push — --force-with-lease · --force-if-includes · Note about fast-forwards · git-pull · git 2.30 릴리스 노트 · GitHub — Dealing with non-fast-forward errors