특정 브랜치에서 파일을 수정하고 git checkout 명령어로 잠시 다른 브랜치로 이동하려고 하면 아래 메시지가 나온다.

Please commit your changes or stash them before you switch branches.

변경한 내용을 commit하거나 stash 해야만 다른 브랜치로 이동할 수 있다는 메시지다. 아직 완료하지 않은 작업을 commit하기 껄끄러울 때 stash 명령어를 활용할 수 있다. 마무리하지 않은 작업을 stash 스택에 임시 저장한 후 다른 브랜치로 이동할 수 있다.

Stash 명령어


<aside> 💡 stash했던 브랜치가 아니어도(다른 브랜치에서도) stash에 저장한 내용을 복원(적용)할 수 있다

</aside>

# 현재 상태 임시 저장 ⭐️
git stash
# 저장한 stash 목록 확인
git stash list
# 가장 최근 저장한 stash 불러오기(적용)
git stash apply

# stash@{2} 이름의 stash 불러오기(적용)
git stash apply stash@{2}

# Staged 했던 상태까지 복원하여 stash 불러오기(적용)
git stash apply --index

# stash 적용(불러오기)와 동시에 stash 제거 ⭐️
git stash pop
# stash 제거
git stash drop

# stash@{2} 이름의 stash 제거
git stash drop stash@{2}
# 가장 최근 stash를 적용했던 내용을 이전 상태로 복구
git stash show -p | git apply -R

# stash@{2} 이름의 stash로 적용했던 내용을 이전 상태로 복구
git stash show -p stash@{2} | git apply -R

Alias 명령어 등록


자주 사용하는 명령어를 원하는 전역 혹은 지역 Alias로 등록하여 사용할 수도 있다.

# git stash show -p | git apply -R 명령어를 git stash-unapply 전역 Alias로 등록
git config --global alias.stash-unapply '!git stash show -p | git apply -R'

# 등록한 전역 Alias 목록 보기
git config --global --get-regexp alias
# alias.stash-unapply !git stash show -p | git apply -R

# 등록한 전역 Alias 삭제
git config --global --unset alias.stash-unapply

참고


[Git] git stash 명령어 사용하기 - Heee's Development Blog

[Git]Alias 추가 / 삭제 / 목록 보기