728x90
    
    
  반응형
    
    
    
  branch 작업을 하다보면, 작업한 내용을 commit 하지 못한 채 다른 branch로 checkout을 해야하는 경우가 있다.
이때, git stash를 사용하여 작업한 내용을 보관하고, working directory를 reset할 수 있다.
# git_test directory에서 git init을 한다. 
$ git init
Initialized empty Git repository in C:/Project/git_test/.git/
# f1.txt 파일을 생성해서 a를 입력하고 저장한다. 
$ vim f1.txt
# f1.txt 파일을 add한다. 
$ git add  f1.txt
warning: LF will be replaced by CRLF in f1.txt.
The file will have its original line endings in your working directory
# commit한다. 
$ git commit -m 1
[master (root-commit) 06cb427] 1
 1 file changed, 1 insertion(+)
 create mode 100644 f1.txt
# exp라는 branch를 만들고 checkout한다. 
$ git checkout -b exp
Switched to a new branch 'exp'
# exp branch에서 f1.txt파일을 수정한다. 
$ vim f1.txt
# master branch를 checkout한다. 
$ git checkout master
Switched to branch 'master'
M       f1.txt
# exp에서 수정한 f1.txt가 master branch에도 영향을 준다. 
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   f1.txt
no changes added to commit (use "git add" and/or "git commit -a")
# 다시 exp branch로 돌아가도 역시, f1.txt 수정사항이 유지된다. 
$ git checkout exp
Switched to branch 'exp'
M       f1.txt
# 이때, git stash를 하면 exp branch에 수정사항이 저장되고 working tree는 clean해진다. 
$ git stash
warning: LF will be replaced by CRLF in f1.txt.
The file will have its original line endings in your working directory
Saved working directory and index state WIP on exp: 06cb427 1
$ git status
On branch exp
nothing to commit, working tree clean
# master를 checkout 한다. 
$ git checkout master
Switched to branch 'master'
# 다시 exp branch를 checkout한다. 
$ git checkout exp
Switched to branch 'exp'
# git stash apply하여, git stash했던 내용을 불러온다. 
$ git stash apply
On branch exp
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   f1.txt
no changes added to commit (use "git add" and/or "git commit -a")
# git stash list를 보면, git stash했던 1건이 그대로 저장되어 있다. 
$ git stash list
stash@{0}: WIP on exp: 06cb427 1
# git reset한다. 
$ git reset --hard HEAD
HEAD is now at 06cb427 1
# git stash apply하여, 다시 수정사항을 불러올 수 있다. 
$ git stash apply
On branch exp
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   f1.txt
no changes added to commit (use "git add" and/or "git commit -a")
# git reset한다. 
$ git reset --hard
HEAD is now at 06cb427 1
# f2.txt 파일을 생성하여 저장한다. 
$ vim f2.txt
# f2.txt 파일을 add한다. 
$ git add f2.txt
warning: LF will be replaced by CRLF in f2.txt.
The file will have its original line endings in your working directory
# git stash한다. 
$ git stash
Saved working directory and index state WIP on exp: 06cb427 1
# 2번 stash한 내역을 확인할 수 있다. 맨 위가 가장 최신 stash한 내역이다. 
$ git stash list
stash@{0}: WIP on exp: 06cb427 1
stash@{1}: WIP on exp: 06cb427 1
# git stash apply하면 가장 최신 stash 내용을 가져온다. 
$ git stash apply
On branch exp
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   f2.txt
# git stash drop하면 가장 최신 stash 내역을 삭제한다. 
$ git stash drop
Dropped refs/stash@{0} (46be97384c105a2dbdf76b560dd4a698d4a3a70b)
# 하나만 저장되어 있음을 확인할 수 있다. 
$ git stash list
stash@{0}: WIP on exp: 06cb427 1
# git stash apply하여 삭제되지 않은 stash 저장내용을 가져온다. 
# f2.txt와 f1.txt 수정사항이 working directory에 반영된다. 
$ git stash apply
On branch exp
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   f2.txt
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   f1.txt
# 하나 남은 stash도 삭제한다. 
$ git stash drop
Dropped refs/stash@{0} (0bef7bf0932985a526464ef83bacb092ab295a1f)
$ git stash apply
No stash entries found.
# git reset한다. 
$ git reset --hard
HEAD is now at 06cb427 1
# f1.txt를 생성하고 저장한다. 
$ vim f1.txt
$ git stash
Saved working directory and index state WIP on exp: 06cb427 1
$ git stash list
stash@{0}: WIP on exp: 06cb427 1
# git stash pop을 하면 git stash를 불러오고 drop을 동시에 할 수 있다. 
$ git stash pop
On branch exp
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   f1.txt
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (1bf3ca1d20fff712e6fbfa78c778adf6104f372a)
#git reset한다. 
$ git reset --hard
HEAD is now at 06cb427 1
# f2.txt파일을 생성하고 저장한다. 
$ vim f2.txt
$ git status
On branch exp
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        f2.txt
nothing added to commit but untracked files present (use "git add" to track)
# track되지 않는 파일은 git stash할 수 없다. new파일은 add한 후에 git stash가능하다. 
$ git stash
No local changes to save728x90
    
    
  반응형
    
    
    
  'Git' 카테고리의 다른 글
| Git의 원리] 6. git remote, origin, push (0) | 2022.09.07 | 
|---|---|
| Git의 원리] 5. git merge, conflict (TortoiseGit) (0) | 2022.09.06 | 
| GitLab CI/CD] 3. Openssh 설정, scp로 웹서버에 파일전송하기 (0) | 2022.09.06 | 
| Git의 원리] 3. git branch, merge (1) | 2022.08.26 | 
| Git의 원리] 2. git commit (tree, commit) (0) | 2022.08.25 | 
 
										
									
댓글