Git

중급 06. Git Stash 임시 저장 실무 활용 가이드

작업 중 긴급 버그 수정 요청이 왔을 때, 미완성 코드를 커밋 없이 안전하게 서랍에 숨겨두고 복원하는 실무 stash 완벽 활용법

· 1min read · 3 views
중급 06. Git Stash 임시 저장 실무 활용 가이드

핵심 요약

작업 중 긴급 버그 수정 요청 시 미완성 코드를 임시 보관(stash push -u -m)하고 hotfix 완료 후 안전하게 복원(stash pop)하는 컨텍스트 스위칭 기법

한창 복잡한 기능을 구현하느라 코드 10여 군데를 고치고 아직 컴파일 에러도 해결하지 못한 상태인데, 갑자기 "운영 서버 결제 버그 났으니 hotfix 브랜치로 가서 1줄만 고쳐주세요!"라는 요청이 들어왔을 때, 프롬프트 상의 브랜치 표시(branch-name) $를 확인하며 안전하게 작업을 대피하고 복원하는 실무 사이클입니다.


1. 왜 git switch가 안 될까?

(feature/coupon-discount) $ git switch hotfix/urgent-bug

error: Your local changes to the following files would be overwritten by checkout:
    app/Services/OrderService.php
Please commit your changes or stash them before you switch branches.
Aborting

2. 실무 긴급 전환 4단계 사이클

Step 1. 작업 중인 내용 서랍에 숨기기

# 수정된 파일 + 새로 만든 파일까지 모두 메모를 붙여 서랍에 보관
(feature/coupon-discount) $ git stash push -u -m "WIP: 쿠폰 할인 로직 작성 중"
Saved working directory and index state On feature/coupon-discount: WIP: 쿠폰 할인 로직 작성 중

# Working Tree가 깨끗해진 상태 확인
(feature/coupon-discount) $ git status

Step 2. 긴급 브랜치로 이동하여 버그 수정 및 배포

(feature/coupon-discount) $ git switch hotfix/urgent-bug
Switched to branch 'hotfix/urgent-bug'

# 버그 1줄 수정 후 푸시
(hotfix/urgent-bug) $ git commit -am "fix: 결제 통신 타임아웃 예외 처리"
(hotfix/urgent-bug) $ git push origin hotfix/urgent-bug

Step 3. 원래 내 개발 브랜치로 복귀

(hotfix/urgent-bug) $ git switch feature/coupon-discount
Switched to branch 'feature/coupon-discount'

Step 4. 서랍에서 작업 꺼내와서 이어서 개발하기

# 가장 최근에 넣어둔 스태시를 꺼내 복원하고 서랍에서 삭제
(feature/coupon-discount) $ git stash pop
Auto-merging app/Services/OrderService.php
Dropped refs/stash@{0}

# 작업이 온전히 복원됨
(feature/coupon-discount) $ git status

Related Docs

Comments (0)

No comments yet. Be the first to comment!

Leave a Comment

로그인 후 댓글을 남길 수 있습니다.