[git] stash

younoah·2021년 2월 7일
0

[Git]

목록 보기
8/14

stash

git에는 stash stack이 존재한다.

stash를 이용하면 git histoty에 저장하지 않고 임시로 stash에 저장할 수 있다.

워킹 디렉터리에서 작업하다가 add, commit을 하지 않고 잠시 다른 브랜치를 이동해야하거나

혹은 워킹디렉터리에서 버그를 고치고 있는데 여러가지 시도를 할 때 각각의 시도를 잠시 저장할 때

stash를 유용하게 사용할 수 있다.

어떤 브랜치에서 작업하다가 commit을 하지 않고 다른 브랜치로 이동을 하게 되면 워킹 디렉터리에서 작업중이던 내용과 함께 다른 브랜치로 이동하게 된다. 따라서 어떤 작업을 하다가 다른 브랜치로 이동을 할 때는 커밋을 하고 이동해야한다.

stash 명령어

  • 저장
git stash # git stash push와 동일
git stash push -m "message" # make a new stash with message
git stash --keep-index # stash but keep them in the staging area 
git stash -u # --include-untracked

git stash --keep-index 명령어를 사용하면 stash하고 staged area도 유지하게 된다.

이 상태에서 다른 브랜치로 이동하면 이동한 브랜치에서 staged area도 같이 이동되어 적용되기 때문에 이점을 유의하고 사용해야한다.

새로 추가한 파일을 stash 해보면 안되는것을 확인할 수 있다. untracked 파일은 stash할 수 없고 tracked 파일만 stash할 수 있다. 하지만 git stash -u 명령어를 사용하면 untracked 파일도 stash가 가능핟.

  • stash 리스트 확인
git stash list #see all the stashes
git stash show hash #see the given stash
git stash show hash -p #see the given stash with details

hash는 stash@{0} , stash@{1} ... 이런 아이들

  • apply
git stash apply hash # applies the given stash
git stash apply # applies the latest stash
git stash apply --index # apply the stated changes
git stash pop # apply the latest stash and drop
git stash branch branchName # creat new branch and apply stash in a new branch

git stash apply 명령만으로는 staged 파일을 자동으로 staged로 만들어 주지 않는다.

git stash apply --index 를 이용해야 staged 상태까지 복원이 된다.

  • stash 삭제
git stash drop hash #deletes the given stash 
git stash clear #deletes all the stashes

참고

어느 브랜치든 stash에 있는 내용을 확인(list)할 수 있고, 적용(apply)와 삭제(drop, clean) 모두 할 수 있다.

profile
console.log(noah(🍕 , 🍺)); // true

0개의 댓글