๐Ÿ‘ป OS & Tools/Git

Git Stash - ๋ณ€๊ฒฝ๋œ ํŒŒ์ผ๋“ค์„ ์ž„์‹œ ์ €์žฅํ•˜๊ธฐ

๋ณต๋งŒ 2022. 11. 8. 17:26

git stash ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด ์ž‘์—…์ค‘์ด๋˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ๋“ค์„ ์ž„์‹œ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

 

git status ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜๋ฉด stage์— ์žˆ๋Š” ํŒŒ์ผ๋“ค์˜ ๋ชฉ๋ก์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋“ค์€ tracked์ด๋ฉด์„œ modified ์ƒํƒœ์ด๊ฑฐ๋‚˜, ์ƒˆ๋กญ๊ฒŒ stage์— ์ถ”๊ฐ€๋œ ํŒŒ์ผ๋“ค์ด๋‹ค.

$ git status
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

 

์ด๋“ค์„ commitํ•˜์ง€ ์•Š๊ณ , stash์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. 

$ git stash
Saved working directory and index state \
  "WIP on master: 049d078 added the index file"
HEAD is now at 049d078 added the index file
(To restore them type "git stash apply")

 

Stash์— ํŒŒ์ผ์„ ์ €์žฅํ•˜๊ณ  ๋‚˜๋ฉด working directory๊ฐ€ ๊นจ๋—ํ•ด์ง„๋‹ค. ์ฆ‰ ์ˆ˜์ •๋œ ์‚ฌํ•ญ์ด ์—†๋Š” ์ƒํƒœ๋กœ ๋Œ์•„๊ฐ„๋‹ค.

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

 

์ €์žฅ๋œ stash์˜ ๋ชฉ๋ก์„ ํ™•์ธํ•˜๋ ค๋ฉด git stash list ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

$ 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๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋ ค๋ฉด git stash apply [stash ์ด๋ฆ„] ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•œ๋‹ค. git stash apply stash@{2}์™€ ๊ฐ™์ด, ๋ถˆ๋Ÿฌ์˜ฌ stash ์ด๋ฆ„์„ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๊ณ , stash ์ด๋ฆ„์ด ์ง€์ •๋˜์ง€ ์•Š์œผ๋ฉด ๊ฐ€์žฅ ์ตœ๊ทผ์˜ stash๋ฅผ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.

$ git stash apply
On branch master
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:   index.html
  modified:   lib/simplegit.rb

no changes added to commit (use "git add" and/or "git commit -a")

 

์ด ๋•Œ ๊ผญ stash๋ฅผ ์ €์žฅํ•œ branch์™€ ๊ฐ™์€ branch์—์„œ stash๋ฅผ ๋ถˆ๋Ÿฌ์™€์•ผ ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๋‹ค๋ฅธ branch๋กœ checkout ํ›„ ํ•ด๋‹น ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ์ ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

Stash๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋ฉด, staged์˜€๋˜ ํŒŒ์ผ๋“ค์ด ๋‹ค์‹œ ์ž๋™์œผ๋กœ stage์— ์˜ฌ๋ผ๊ฐ€์ง€ ์•Š๋Š”๋‹ค. (์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ๋งจ ์•„๋ž˜์— 'no changes added to commit'์ด๋ผ๋Š” ๋ฌธ๊ตฌ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.)

 

๋งŒ์•ฝ ๋ถˆ๋Ÿฌ์˜จ ํŒŒ์ผ๋“ค์ด stage์— ์˜ฌ๋ผ๊ฐ€ ์žˆ๊ฒŒ ํ•˜๋ ค๋ฉด git stash apply์— --index ์˜ต์…˜์„ ์ฃผ๋ฉด ๋œ๋‹ค.

$ 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

 

Stash๋ฅผ ๋ถˆ๋Ÿฌ์™€๋„ ํ•ด๋‹น stash๋Š” ์—ฌ์ „ํžˆ ์ €์žฅ๋œ ์ƒํƒœ๋กœ ๋‚จ์•„์žˆ๋‹ค. ํŠน์ • stash๋ฅผ ์ง€์šฐ๋ ค๋ฉด git stash drop ๋ช…๋ น์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

$ git stash drop stash@{0}
Dropped stash@{0} (364e91f3f268f0900bc3ee613f9f733e82aaed43)

 

Stash๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋˜๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ working directory๋ฅผ ์ฒญ์†Œํ•  ๋•Œ์ด๋‹ค. ๊ฐ™์€ ์ฝ”๋“œ๊ฐ€ ๋ณ€๊ฒฝ๋˜์–ด ์ถฉ๋Œ์ด ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ ๋ฉ”์„ธ์ง€๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค.

 

 

์ด ๋•Œ ๋‹จ์ˆœํžˆ ๋ณ€๊ฒฝ์‚ฌํ•ญ๋“ค์„ ์ง€์šฐ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ stash๋ฅผ ์ด์šฉํ•˜๋ฉด, ์ˆ˜์ •์‚ฌํ•ญ๋“ค์ด stash์— ์ž„์‹œ์ €์žฅ๋˜๋ฏ€๋กœ ๋ณด๋‹ค ์•ˆ์ „ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

์ถœ์ฒ˜: https://git-scm.com/book/ko/v2/Git-%EB%8F%84%EA%B5%AC-Stashing%EA%B3%BC-Cleaning

 

Git - Stashing๊ณผ Cleaning

์ž‘์—…ํ•˜๋˜ ์ €์žฅ์†Œ๊ฐ€ ์™„์ „ ์ง€์ €๋ถ„ํ•ด์ ธ์„œ Git์—๊ฒŒ ์ง„์งœ๋กœ ๊ฐ•์ œ๋กœ ์ •๋ฆฌํ•˜๋„๋ก ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Git ๋ฒ„์ „๊ด€๋ฆฌ ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ๋œ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋ณต์‚ฌํ•ด์™”๊ฑฐ๋‚˜ ์„œ๋ธŒ๋ชจ๋“ˆ ๋””๋ ‰ํ† ๋ฆฌ

git-scm.com

 

 

๋ฐ˜์‘ํ˜•

'๐Ÿ‘ป OS & Tools > Git' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Git commit ์ •ํ™•ํ•œ ์‹œ๊ฐ„ ํ™•์ธํ•˜๊ธฐ  (1) 2022.10.26
Git ์ด์šฉ๋ฒ•๊ณผ ๋ช…๋ น์–ด  (0) 2020.02.08