본문 바로가기
C.W.K.
Stream
Lesson 05 of 05 · published

force push는 lease와 함께

~18 min · push, force-with-lease

Level 0추적 전 새싹
0 XP0/47 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Force-push가 필요하다면 lease와 함께 써

push한 commit을 amend하거나 interactive rebase로 drop 또는 squash하면 로컬 branch와 remote branch는 같은 작업의 서로 다른 이력을 갖게 돼. Plain git push는 서버 이력을 덮지 않으려고 거부해. 다시 쓴 이력을 올리려면 force-push가 필요하지만 plain --force는 잘못된 도구야. --force-with-lease를 써.

차이는 안전 조건이야. git push --force는 remote tip을 로컬 tip으로 무조건 덮어써. 동료가 네가 아직 받지 않은 commit을 push했다면 서버에서 그 작업을 지울 수 있어. git push --force-with-lease는 마지막 fetch 때 본 remote tip을 기억하고, 그 뒤 tip이 움직였다면 push를 거부해. 평소 편의는 같고 동시 작업을 잃을 순간에만 멈춰.

--force-with-lease=<branch>:<sha> 형식은 기대하는 remote SHA를 정확히 지정해서 script에 유용해. Git 2.30+의 --force-if-includes--force-with-lease를 보강해 로컬 branch가 덮으려는 remote tip을 base로 삼았는지 확인해, 자동 fetch 환경에서 놓칠 수 있는 경우를 더 막아. 둘을 함께 쓰면 git push --force-with-lease --force-if-includes야.

규율은 단순해. git push --force를 반사적으로 쓰지 마. git config --global alias.pushf 'push --force-with-lease --force-if-includes'git pushf를 만들고 손버릇을 안전한 쪽으로 옮겨. plain --force는 동작을 정확히 통제하는 드문 script에 남겨둬.

Code

Default 로 안전한 force-push·bash
# Push 된 commit 의 local rebase 또는 amend 후:
git push --force-with-lease --force-if-includes

# --force-with-lease 체크:
#   Remote tip 이 마지막 fetch 시점 그대로여야 함.
#   동료가 동시에 push 했으면 거부.

# --force-if-includes 가 더하는 것:
#   Local branch 가 덮어쓸 remote tip 에 base 했는지 확인 —
#   'fetch 잊음' 케이스 잡음.

# 추천 alias:
git config --global alias.pushf 'push --force-with-lease --force-if-includes'
git pushf
Force-push 사고에서 회복·bash
# 동료가 force-push 해서 네 작업 덮어씀?
# 그들 reflog 에 네 tip 있을 수도. 또는 네 거 사용:

git reflog                       # pre-force-push tip 찾기
git reset --hard HEAD@{N}        # local 복원

# 그 다음:
git push --force-with-lease      # 재게시 (조율 후!)
# 또는:
git switch -c rescue HEAD@{N}    # 별도 branch 로 저장
git push -u origin rescue

External links

Exercise

개인 branch에 commit 두 개를 push하고 git rebase -i로 하나로 squash해. plain git push를 실행해 거부 message를 읽고 git push --force-with-lease로 다시 push해 성공을 확인해. lesson의 pushf alias도 추가해. 앞으로 git push --force 대신 git pushf를 쓰면서 손버릇이 어떻게 달라지는지 적어봐.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.