기능
A라는 branch에서 작업을 하다가 B 라는 branch로 이동을 하게 되면 A에서 한 작업이 모두 B branch로 옮겨진다. A에서의 작업을 유지하면서 B branch로 이동해 다른 작업을 하고 싶다면 stash를 사용하면 된다.
// 원하지 않는 상황
git branch
-----------------
*A
B
git status
-----------------
modified: /file1.txt
git checkout B
git status
-----------------
modified: /file1.txt
git stash [save]
git branch
-----------------
*A
B
git status
-----------------
modified: /file1.txt
git stash [save]
-----------------
Saved working directory and index state
git status
-----------------
nothing to commit, working tree clean
git chekcout B
git status
-----------------
nothing to commit, working tree clean
git stash save를 하면 현재 branch의 작업 현황이 저장된다. 따라서 branch를 변경해도 기존에 하던 작업이 반영 되지 않는다.
git stash list
git checkout A
git stash list
-----------------
stash@{0}: WIP on master: 049d078 added the index file
저장된 stash들을 볼 수 있다.
git stash apply [stash]
git stash apply [stash@{0}]
-----------------
modified: /file1.txt
git stash list
-----------------
stash@{0}: WIP on master: 049d078 added the index file
저장된 stash를 불러오고 싶다면 apply 명령어를 사용하면 된다. apply 뒤에 원하는 stash를 작성하면 원하는 순간으로 돌아 갈 수 있다.
다만 stash를 apply한다고 해서 저장된 stash가 사라지는 것은 아니다.
git stash drop
git stash drop [stash@{0}]
-----------------
Dropped stash@{0} (364e91f3f268f0900bc3ee613f9f733e82aaed43)
git stash list
-----------------
// 아무것도 없다
stash를 삭제하고 싶다면 drop을 사용하면 최 상단의 stash를 삭제 할 수 있다. 원하는 stash를 삭제하고 싶다면 drop뒤에 stash를 지정해 주면 된다.
아래는 원하는 상황을 나타내는 코드이다.
git branch
-----------------
*A
B
git status
-----------------
modified: /file1.txt
git stash [save]
-----------------
Saved working directory and index state
git status
-----------------
nothing to commit, working tree clean
git chekcout B
git status
-----------------
nothing to commit, working tree clean
git checkout A
git stash list
-----------------
stash@{0}: WIP on master: 049d078 added the index file
git stash apply [stash@{0}]
-----------------
modified: /file1.txt
git stash list
-----------------
stash@{0}: WIP on master: 049d078 added the index file
git stash drop [stash@{0}]
-----------------
Dropped stash@{0} (364e91f3f268f0900bc3ee613f9f733e82aaed43)
git stash list
-----------------
// 아무것도 없다
https://git-scm.com/book/ko/v2/Git-%EB%8F%84%EA%B5%AC-Stashing%EA%B3%BC-Cleaning
'computer science > knowlege' 카테고리의 다른 글
순환 참조 (1) | 2024.08.31 |
---|---|
Authentication, Authorization (0) | 2023.10.28 |
Cookie, Session, JWT (0) | 2023.08.16 |