페이지 트리

작업 복사본에 대한 숨김 보관함 (또는 숨김) 변경 내용을 숨겨서 다른 작업을 할 수있게 한 다음 나중에 다시 적용하여 나중에 다시 적용 할 수 있습니다. 숨기기는 컨텍스트를 신속하게 전환하고 다른 작업을해야하는 경우 편리하지만 코드를 변경하는 중반에 있으며 커밋 할 준비가되지 않았습니다.

작업 숨기기

git stash 명령은 커밋되지 않은 변경 (준비 및 언 스테이지 모두)을 취하고 나중에 사용하기 위해 저장 한 다음 작업 복사본에서 되돌립니다. 예 :

$ git status
On branch master
Changes to be committed:

 new file: style.css

Changes not staged for commit:

 modified: index.html

$ git stash
Saved working directory and index state WIP on master: 5002d47 our new homepage
HEAD is now at 5002d47 our new homepage

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

이 시점에서 여러분은 자유롭게 변경을 할 수 있고, 새로운 커밋을 만들고, 분기를 전환하고, 다른 Git 작업을 수행 할 수 있습니다. 그런 다음 다시 돌아와서 준비가되었을 때 은닉을 다시 적용하십시오.

숨겨진 저장소는 Git 저장소에 로컬로 저장됩니다. 밀어 넣을 때 은폐는 서버로 전송되지 않습니다.

숨겨진 변경 적용하기

git stash pop으로 이전에 숨긴 변경 사항을 다시 적용 할 수 있습니다.

$ git status
On branch master
nothing to commit, working tree clean
$ git stash pop
On branch master
Changes to be committed:

 new file: style.css

Changes not staged for commit:

 modified: index.html

Dropped refs/stash@{0} (32b3aa1d185dfe6d57b3c3cc3b32cbf3e380cc6a)

Popping your stash removes the changes from your stash and reapplies them to your working copy.

Alternatively, you can reapply the changes to your working copy and keep them in your stash with git stash apply:

$ git stash apply
On branch master
Changes to be committed:

 new file: style.css

Changes not staged for commit:

 modified: index.html

여러 분기에 동일한 숨겨진 변경 사항을 적용하려는 경우 유용합니다.

stashing의 기본 사항을 알았으므로 git stash를주의해야합니다. 기본적으로 Git은 추적되지 않거나 무시되는 파일의 변경 사항을 숨기지 않습니다.

untracked or ignored 파일 숨기기

기본적으로 git stash를 실행하면 다음 항목이 숨겨집니다.

  • 색인에 추가 된 변경 사항 (단계별 변경 사항)
  • 현재 Git에 의해 추적되는 파일에 대한 변경 사항 (unstaged changes)

그러나 다음 항목은 숨겨지지 않을 것입니다 :

  • 작업 복사본에서 아직 준비되지 않은 새 파일
  • 무시 된 파일

위 예제에 세 번째 파일을 추가했지만 스테이지를 만들지 않으면 (즉, git add를 실행하지 않음) git stash가 stash하지 않습니다.

$ script.js

$ git status
On branch master
Changes to be committed:

 new file: style.css

Changes not staged for commit:

 modified: index.html

Untracked files:

 script.js

$ git stash
Saved working directory and index state WIP on master: 5002d47 our new homepage
HEAD is now at 5002d47 our new homepage

$ git status
On branch master
Untracked files:

 script.js

-u 옵션 (또는 --include-untracked)을 추가하면 git stash가 untracked 파일을 숨긴다.

$ git status
On branch master
Changes to be committed:

 new file: style.css

Changes not staged for commit:

 modified: index.html

Untracked files:

 script.js

$ git stash -u
Saved working directory and index state WIP on master: 5002d47 our new homepage
HEAD is now at 5002d47 our new homepage

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

git stash를 실행할 때 -a 옵션 (또는 --all)을 전달하여 무시 된 파일에 대한 변경 사항을 포함 할 수 있습니다.

숨겨진 목록 (stash) 관리하기

git stash를 여러 번 실행하여 숨겨진 목록을 만든 다음 git stash list를 사용하여 볼 수 있습니다. 기본적으로 stash는 작성된 분기 또는 커밋 위의 "진행중인 작업"(WIP)으로 식별 됩니다. 잠시 후 각 은닉 내용을 기억하는 것은 어려울 수 있습니다.

$ git stash list
stash@{0}: WIP on master: 5002d47 our new homepage
stash@{1}: WIP on master: 5002d47 our new homepage
stash@{2}: WIP on master: 5002d47 our new homepage

좀 더 많은 문맥을 제공하기 위해 stash에 설명을 추가하고 git stash save "message"를 사용하여 주석을 달아주는 것이 좋습니다.

$ git stash save "add style to our site"
Saved working directory and index state On master: add style to our site
HEAD is now at 5002d47 our new homepage

$ git stash list
stash@{0}: On master: add style to our site
stash@{1}: WIP on master: 5002d47 our new homepage
stash@{2}: WIP on master: 5002d47 our new homepage

기본적으로 git stash pop은 가장 최근에 생성 된 stash를 다시 적용합니다 : stash @ {0}

식별자를 마지막 인수로 전달하여 다시 적용 할 스태프를 선택할 수 있습니다 (예 :

$ git stash pop stash@{2}

숨겨진 내용 비교 (stash diffs)

git stash show를 사용하여 stash 요약을 볼 수 있습니다.

$ git stash show
 index.html | 1 +
 style.css | 3 +++
 2 files changed, 4 insertions(+)

또는 stash 전체 diff를 보려면 -p 옵션 (또는 - patch)을 전달하십시오.

$ git stash show -p
diff --git a/style.css b/style.css
new file mode 100644
index 0000000..d92368b
--- /dev/null
+++ b/style.css
@@ -0,0 +1,3 @@
+* {
+ text-decoration: blink;
+}
diff --git a/index.html b/index.html
index 9daeafb..ebdcbd2 100644
--- a/index.html
+++ b/index.html
@@ -1 +1,2 @@
+<link rel="stylesheet" href="style.css"/>

부분 stashes

단일 파일, 파일 모음 또는 파일 내 개별 변경 사항을 숨기도록 선택할 수도 있습니다. git stash에 -p 옵션 (또는 --patch)을 전달하면 작업 복사본에서 변경된 각 "덩어리"를 반복하여 숨길 지 여부를 묻습니다.

$ git stash -p
diff --git a/style.css b/style.css
new file mode 100644
index 0000000..d92368b
--- /dev/null
+++ b/style.css
@@ -0,0 +1,3 @@
+* {
+ text-decoration: blink;
+}
Stash this hunk [y,n,q,a,d,/,e,?]? y
diff --git a/index.html b/index.html
index 9daeafb..ebdcbd2 100644
--- a/index.html
+++ b/index.html
@@ -1 +1,2 @@
+<link rel="stylesheet" href="style.css"/>
Stash this hunk [y,n,q,a,d,/,e,?]? n

일반적으로 유용한 것들은 다음과 같습니다 :


CommandDescription
/search for a hunk by regex
?help
ndon't stash this hunk
qquit (any hunks that have already been selected will be stashed)
ssplit this hunk into smaller hunks
ystash this hunk

명시 적 "중단"명령은 없지만 CTRL-C (SIGINT)를 누르면 숨김 프로세스가 중단됩니다.

Stash로부터 브랜치 생성하기

브랜치의 변경사항이 stash의 변경사항과 다른 경우  stash를 적용 할 때 충돌이 발생할 수 있습니다. 대신 git stash branch를 사용하여 숨겨진 변경 사항을 적용 할 새 브랜치를 만들 수 있습니다 :

$ git stash branch add-stylesheet stash@{1}
Switched to a new branch 'add-stylesheet'
On branch add-stylesheet
Changes to be committed:

 new file: style.css

Changes not staged for commit:

 modified: index.html

Dropped refs/stash@{1} (32b3aa1d185dfe6d57b3c3cc3b32cbf3e380cc6a)

이것은 당신이 숨긴 곳을 만든 커밋을 기반으로 새로운 브랜치를 체크 아웃하고 숨겨진 변경 사항을 그 위에 놓습니다.

stash 비우기

더 이상 stash가 필요 없다고 판단되면 git stash drop으로 삭제할 수 있습니다.

$ git stash drop stash@{1}
Dropped stash@{1} (17e2697fd8251df6163117cb3d58c1f62a5e7cdb)

또는 다음과 같이 모든 숨기기를 삭제할 수 있습니다.

$ git stash clear

How git stash works

Stash가 어떻게 동작하는지 궁금하다면 이 절을 읽어도 좋습니다. 단지 stash 사용 방법이 알고싶다면 이 절을 읽지 않아도 됩니다.

stash는 저장소에서 실제로 커밋 객체로 인코딩됩니다. .git / refs / stash의 특별한 ref는 가장 최근에 생성 된 stash를 가리키며, 이전에 생성 된 stash는 stash ref의 reflog에 의해 참조됩니다. 이것이 당신이 stash @ {n}을 은닉하여 참조하는 이유입니다. 실제로는 stash ref의 n 번째 reflog 항목을 참조하고 있습니다. stash는 커밋 일 뿐이므로 git log로 검사 할 수있습니다.

$ git log --oneline --graph stash@{0}
*-. 953ddde WIP on master: 5002d47 our new homepage
|\ \ 
| | * 24b35a1 untracked files on master: 5002d47 our new homepage
| * 7023dd4 index on master: 5002d47 our new homepage
|/ 
* 5002d47 our new homepage

숨긴 것에 따라, 하나의 git stash 연산은 2 ~ 3 개의 새로운 커밋을 생성합니다. 위 다이어그램의 커밋은 다음과 같습니다.

  • stash @ {0}, git stash를 실행할 때 작업 복사본에 있던 추적 된 파일을 저장하는 새로운 커밋
  • @ {0} 님의 첫 번째 부모를 숨기고 git stash를 실행할 때 HEAD에 있던 기존 커밋을 숨긴다.
  • @ {0}의 두 번째 부모를 숨기고, git stash를 실행할 때 색인을 나타내는 새로운 커밋
  • @ {0}의 세 번째 부모를 숨기고, git stash를 실행할 때 작업 복사본에 있던 untracked 파일을 나타내는 새로운 커밋. 이 세 번째 상위 항목은 다음 경우에만 생성됩니다.
    • 작업 복사본에는 실제로 untracked 파일이 포함되어 있습니다. 과
    • git stash를 호출 할 때 --include-untracked 또는 --all 옵션을 지정했습니다.

git stash가 작업 트리와 인덱스를 커밋으로 인코딩하는 방법은 다음과 같습니다.

숨겨지기 전에 작업 트리에 추적 된 파일, 추적 할 수없는 파일 및 무시 된 파일에 대한 변경 사항이 포함될 수 있습니다. 이러한 변경 사항 중 일부는 색인에 포함될 수도 있습니다.

git stash를 호출하면 추적 된 파일에 대한 모든 변경 사항이 DAG에서 두 개의 새로운 커밋으로 인코딩됩니다. 하나는 무단 변경에 대한 것이고 다른 하나는 인덱스에 준비된 변경에 대한 것입니다. 특수한 refs / stash ref가 그들을 가리 키도록 업데이트됩니다.

--include-untracked 옵션을 사용하면 추적되지 않은 파일에 대한 변경 사항을 추가 커밋으로 인코딩 할 수 있습니다.

--all 옵션을 사용하면 동일한 커밋에서 추적되지 않은 파일의 변경 사항과 함께 무시 된 파일의 변경 사항이 포함됩니다.

git stash pop을 실행하면 위 커밋의 변경 사항이 작업 복사본과 인덱스를 업데이트하는 데 사용되며 stash reflog는 셔플되어 팝 된 커밋을 제거합니다. 팝 된 커밋은 즉시 삭제되지 않지만 향후 가비지 수집을위한 후보가됩니다.

  • 레이블 없음