ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๊ณต๋ถ€

[Git] Stash ์ •๋ฆฌ ๐ŸŒ

Carrot๐Ÿฅ• 2021. 3. 29. 01:17

git stash๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์™„๋ฃŒ๋˜์ง€ ์•Š์€ ์ž‘์—…์„ stash stack ์— ์ž„์‹œ ์ €์žฅ ํ•  ์ˆ˜ ์žˆ์Œ

git stash === git stash push

  • ์ปค๋ฐ‹์ฒ˜๋Ÿผ -m flag๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฉ”์‹œ์ง€๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
โฏ git status -s
 M working.txt # ์›Œํ‚น ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ๋Š” ํŒŒ์ผ
M  staging.txt # staging๋œ ํŒŒ์ผ

โฏ git stash push -m "working + staging"
Saved working directory and index state On master: working + staging

โฏ git status -s
# ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด stash stack์— ๋“ค์–ด๊ฐ€๋ฉด์„œ working & staging ๋ชจ๋‘ ๋น„์›Œ์ง

โฏ git stash list
stash@{0}: On master: working + staging

git stash apply

  • Stash๋ฅผ ์ ์šฉํ•  ๋•Œ Staged ์ƒํƒœ์˜€๋˜ ํŒŒ์ผ์„ ์ž๋™์œผ๋กœ ๋‹ค์‹œ Staged ์ƒํƒœ๋กœ ๋งŒ๋“ค์–ด ์ฃผ์ง€ ์•Š๋Š”๋‹ค.
    ๊ทธ๋ž˜์„œ git stash apply ๋ช…๋ น์„ ์‹คํ–‰ํ•  ๋•Œ --index ์˜ต์…˜์„ ์ฃผ์–ด Staged ์ƒํƒœ๊นŒ์ง€ ์ ์šฉํ•œ๋‹ค. ๊ทธ๋ž˜์•ผ ์›๋ž˜ ์ž‘์—…ํ•˜๋˜ ์ƒํƒœ๋กœ ๋Œ์•„์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
  • ์œ„์˜ ์ƒํ™ฉ์—์„œ git stash apply๋ฅผ ํ•˜๋ฉด
โฏ git status -s
 M working.txt 
 M staging.txt # staged ์ƒํƒœ์˜€๋˜ ํŒŒ์ผ๋„ ์›Œํ‚น ๋””๋ ‰ํ† ๋ฆฌ๋กœ ๋‚ด๋ ค์˜ด
  • git stash apply --index๋ฅผ ํ•˜๋ฉด
โฏ git status -s
 M working.txt
M staging.txt # staged ๋œ ์ƒํƒœ ๊ทธ๋Œ€๋กœ ์œ ์ง€
  • apply ์˜ต์…˜์€ ๋‹จ์ˆœํžˆ Stash๋ฅผ ์ ์šฉํ•˜๋Š” ๊ฒƒ๋ฟ์ด๋‹ค. Stash๋Š” ์—ฌ์ „ํžˆ ์Šคํƒ์— ๋‚จ์•„ ์žˆ๋‹ค.
    git stash drop๋ช…๋ น์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น Stash๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  git stash pop์ด๋ผ๋Š” ๋ช…๋ น๋„ ์žˆ๋Š”๋ฐ ์ด ๋ช…๋ น์€ Stash๋ฅผ ์ ์šฉํ•˜๊ณ  ๋‚˜์„œ ๋ฐ”๋กœ ์Šคํƒ์—์„œ ์ œ๊ฑฐํ•ด์ค€๋‹ค.
  • ๊ผญ ๊นจ๋—ํ•œ ์›Œํ‚น ๋””๋ ‰ํ† ๋ฆฌ๋‚˜ Stash ํ•  ๋•Œ์™€ ๊ฐ™์€ ๋ธŒ๋žœ์น˜์— ์ ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค.
    ์–ด๋–ค ๋ธŒ๋žœ์น˜์—์„œ Stash ํ•˜๊ณ  ๋‹ค๋ฅธ ๋ธŒ๋žœ์น˜๋กœ ์˜ฎ๊ธฐ๊ณ ์„œ ๊ฑฐ๊ธฐ์— Stash๋ฅผ ๋ณต์›ํ•  ์ˆ˜ ์žˆ๋‹ค.
    ๊ทธ๋ฆฌ๊ณ  ๊ผญ ์›Œํ‚น ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ๊นจ๋—ํ•œ ์ƒํƒœ์ผ ํ•„์š”๋„ ์—†๋‹ค. ์›Œํ‚น ๋””๋ ‰ํ† ๋ฆฌ์— ์ˆ˜์ •ํ•˜๊ณ  ์ปค๋ฐ‹ํ•˜์ง€ ์•Š์€ ํŒŒ์ผ๋“ค์ด ์žˆ์„ ๋•Œ๋„ Stash๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ ์ถฉ๋Œ์ด ์žˆ์œผ๋ฉด ์•Œ๋ ค์ค€๋‹ค.

git stash push -m --keep-index

  • ์ด ์˜ต์…˜์„ ์ด์šฉํ•˜๋ฉด ์ด๋ฏธ Staging Area์— ๋“ค์–ด ์žˆ๋Š” ํŒŒ์ผ์„ Stash ํ•˜์ง€ ์•Š๋Š”๋‹ค.

git stash -u

  • -u ์˜ต์…˜์€ โ€”include-untracked ์˜ ์ค„์ž„ํ‘œํ˜„์ด๋‹ค.
    ์–ธํŠธ๋ž˜ํ‚น ํŒŒ์ผ๋“ค๋„ stashing area์— ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

์ถœ์ฒ˜

๋Œ“๊ธ€