Search
castle

stash 기능

작업 도중 다른 branch로 이동하려면 현재 작업영역의 commit이 완료된 상태여야 한다.
아직 작업이 완성되지 않은 상태에서 다른 branch로 이동하기 위해 임시저장하는 기능이 stash다.


출처 : https://velog.io/@ewan/What-is-Git-stash

저장

  • git stash push 혹은 git stash save
    • 공식문서는 push 를 권장
  • -m 옵션을 통해 메세지 작성도 가능
  • 실행 후 마지막 commit 시점으로 이동(nothing to commit)
  • stash를 실행한 동일한 branch일 필요 없음
  • working directory가 깨끗할 필요는 없으며, 충돌이 있을 시 알려준다
$  git stash -m "stash sample"
$  git stash push -m "stash sample"
Saved working directory and index state On (no branch): stash sample

$  git status
# On branch master
nothing to commit, working directory clean

stash 목록 확인 및 이동

Stash 명령을 사용하면 워킹 디렉토리에서 수정한 파일들만 저장한다.
Stash는 Modified이면서 Tracked 상태인 파일과
Staging Area에 있는 파일들을 보관해두는 장소다.

  • UnTracked 상태의 파일은 저장하지 않음
    • -u 혹은 --include-untracked 옵션을 통해 untracked file 포함 가능

list

  • git stash list : 저장된 stash 목록 확인
  • 이때 stash@{<num>} 에서 <num> 을 통해 저장 및 삭제를 진행
  • <num>stack구조(0에 가까울 수록 최신, LIFO)
$  git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert "added file_size"
stash@{2}: WIP on master: 21d80a5 added number to log
stash@{3}: On (no branch): stash sample

apply, drop, pop

  • git stash apply <num> : stash@{<num>} 으로 이동
  • git stash drop <num> : stah@{<num>} 삭제
    • git stash clear : 모든 stash 삭제
  • git stash pop : stash@{0} 적용 및 삭제(apply&drop)
$  git stash apply 1
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
        modified:   123.md
no changes added to commit (use "git add" and/or "git commit -a")
stash@{0}: On (no branch): stash2

$  git stash drop 0
Dropped refs/stash@{0} (a5c5ad864e5ecf73457fedde64ef3573de7fd497)

$  git stash list
stash@{0}: On (no branch): stash sample

참고 : modified 내용 취소

마지막 commit 지점으로 working directory를 되돌린다.

  • git checkout -- <file>
    • git stashgit stash drop 0 와 동일
    • 띄어쓰기 <file>
    • 변경 후 git stash 혹은 git commit --amend 명령어와 달리 복구가 불가능

옵션

stash는 기본적으로 working directory의 변경사항만 적용한다.
staging 혹은 tracked 상태를 적용하기 위해서는 추가 옵션을 설정해야 한다.

–index

  • git stash apply --index : Staged 상태 적용
$  git stash apply --index
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: index.html
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working
directory)
modified: lib/simplegit.rb

–keep-index

  • stash 저장(push, save)에 사용
  • git stash --keep-index : 이미 Staging Area에 들어 있는 파일(git add)을 Stash 하지 않음
$  git status -s
M index.html
  M lib/simplegit.rb
  
$  git stash --keep-index
Saved working directory and index state WIP on master: 1b65b17 added the
index file
HEAD is now at 1b65b17 added the index file

$  git status -s
M index.html

-u, –include-untracked

  • git stash -u : untracked 파일 포함하여 stash
$  git status -s
M index.html
  M lib/simplegit.rb
?? new-file.txt

$  git stash -u
Saved working directory and index state WIP on master: 1b65b17 added the
index file
HEAD is now at 1b65b17 added the index file

$ git status -s
$

git stash branch

현재 working directory를 stash 후 새로운 branch를 생성하여 적용하는 기능이다.

  • git stash branch <브랜치>
  • 다음을 순차적으로 수행하는 것과 동일
    • git stash
    • git switch -c <branch>
    • git stash pop
$  git stash branch testchanges
M index.html
M lib/simplegit.rb
Switched to a new branch 'testchanges'
On branch testchanges
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
  
  modified: index.html
  
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working
directory)

  modified: lib/simplegit.rb
  
Dropped refs/stash@{0} (29d385a81d163dfd45a452a2ce816487a6b8b014)
left
right

C

Contents