C.W.K.
Stream
Lesson 03 of 04 · published

bad pull/rebase에서 복구하기

~24 min · playbook, reflog, rebase

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

안 좋은 pull, rebase, reset 에서 회복

조만간 Git 명령 돌리고 빨간 텍스트 물결 보고 속이 내려앉을 거야. 좋은 소식: 거의 모든 'Git 작업 잃음' 상황이 회복 가능. 회복 절차는 매번 같은 모양: 파괴적 명령 그만, git reflog 로 잃은 tip 찾기, branch 에 anchor, 평가, 통합 방법 선택.

첫 동작은 항상 git reflog --date=relative -n 50. 출력은 90 일 동안의 모든 HEAD 이동 + timestamp. 원하는 상태 같은 entry 찾기 — 보통 파괴적 명령 직전 거. 각 entry 의 hash 가 돌아갈 수 있는 진짜 commit. Rescue target 식별 전엔 더 파괴적 명령 안 돌려.

Anchor 가 다음. git switch -c rescue HEAD@{N} 이 잃은 tip 가리키는 새 branch 생성. 거기서 평가: 이게 정확히 잃은 거인가, commit 일부 빠졌나? git log rescue --oneline 으로 확인. 잃은 작업 든 branch 가 있으면 재앙 끝 — 통합 방법 선택. 원본 branch 에 git reset --hard rescue 가 모두 복귀. 또는 git cherry-pick 으로 특정 잃은 commit 만 현재 main 에. 또는 rescue branch 를 병렬 기록으로 두고 계속.

흔한 시나리오 셋. 안 좋은 pull merge — 원하던 선형 history 대신 우발 merge commit. git reset --hard ORIG_HEAD 가 pull 전으로 rewind, 그 다음 git pull --rebase 로 선형 버전. 안 좋은 rebase — rebase 가 엉망 만들거나 중간 해결 불가 conflict. git rebase --abort 가 rebase 전으로 rewind. 안 좋은 rebase 결과 commit 했으면 git reflog + git reset --hard HEAD@{N}. 안 좋은 force-push — 동료 작업 덮어씀. 그들 reflog 에 여전히 있음, 그들이 보유한 상태에서 git push --force-with-lease 또는 force-push 전 그들 작업 봤으면 네 reflog 의 HEAD@{N} 으로 회복.

Code

일반 회복 절차·bash
# 1. 멈춰. 파괴적 명령 더 안 돌려.

# 2. Reflog 검사:
git reflog --date=relative -n 50

# 3. 원하는 상태 식별 — 보통 파괴적 명령 직전.
#    HEAD@{N} 또는 hash 기록.

# 4. Rescue branch 에 잃은 tip anchor:
git switch -c rescue HEAD@{3}        # 옳은 N 사용

# 5. 검증:
git log rescue --oneline -n 10

# 6. 통합 방법 선택:
#    - 원본 branch 로 되돌리기:
git switch original-branch
git reset --hard rescue

#    - 또는 특정 잃은 commit cherry-pick:
git cherry-pick rescue~2 rescue~1 rescue
흔한 시나리오 명령·bash
# 원치 않는 merge commit 만든 안 좋은 pull:
git reset --hard ORIG_HEAD
git pull --rebase                    # 다시, 선형

# Rebase 중간 엉망 — 빠져나가기:
git rebase --abort

# 이미 commit 된 안 좋은 rebase 결과:
git reflog
git reset --hard HEAD@{N}            # rebase 전

# Force-push 된 branch 회복 (동료 작업 덮어씀):
git reflog                            # 마지막 fetch 한 시각
git switch -c rescue-teammate HEAD@{N}
git push --force-with-lease origin rescue-teammate

External links

Exercise

Commit 5 개 이상인 scratch branch 에서 일부러 작업 셋 방식으로 파괴: (1) git reset --hard HEAD~3, (2) rebase 시작 + conflict 중간 git rebase --abort, (3) rewritten branch git push --force 후 reflog 로 회복. 각 후 git reflog 로 잃은 tip 찾고 복원. 절차 알기 전에 어느 회복이 가장 무서웠는지 적어.

Progress

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

댓글 0

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

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