2 ๋ถ„ ์†Œ์š”



reset vs revert

reset

  • https://git-scm.com/docs/git-reset
  • โ€œ์‹œ๊ณ„๋ฅผ ๋งˆ์น˜ ๊ณผ๊ฑฐ๋กœ ๋Œ๋ฆฌ๋Š” ๋“ฏํ•œ ํ–‰์œ„โ€
  • ํŠน์ • ์ปค๋ฐ‹์œผ๋กœ ๋˜๋Œ์•„๊ฐ€๋ฉฐ ๋˜๋Œ์•„๊ฐ„ ํŠน์ • ์ปค๋ฐ‹ ์ดํ›„์˜ ์ปค๋ฐ‹๋“ค์€ ๋ชจ๋‘ ์‚ฌ๋ผ์ง€๋ฉฐ, ํŒŒ์ผ ์ƒํƒœ๋Š” ์˜ต์…˜์„ ํ†ตํ•ด ๊ฒฐ์ •



3๊ฐ€์ง€ ์˜ต์…˜

1. --soft

  • resetํ•˜๊ธฐ ์ „๊นŒ์ง€ ํ–ˆ๋˜ SA, WD ์ž‘์—…์€ ๋‚จ๊ฒจ๋‘ 
  • ๋Œ์•„๊ฐ€๋ ค๋Š” ์ปค๋ฐ‹์œผ๋กœ ๋˜๋Œ์•„๊ฐ€๊ณ ,
  • ์ดํ›„์˜ commit๋œ ํŒŒ์ผ๋“ค์„ staging area๋กœ ๋Œ๋ ค๋†“์Œ (commit ํ•˜๊ธฐ ์ „ ์ƒํƒœ)
  • ์ฆ‰, ๋ฐ”๋กœ ๋‹ค์‹œ ์ปค๋ฐ‹ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๊ฐ€ ๋จ

2. --mixed

  • (๊ธฐ๋ณธ) SA reset, WD์ž‘์—…์€ ๋‚จ๊ฒจ๋‘ 
  • ๋Œ์•„๊ฐ€๋ ค๋Š” ์ปค๋ฐ‹์œผ๋กœ ๋˜๋Œ์•„๊ฐ€๊ณ ,
  • ์ดํ›„์˜ commit๋œ ํŒŒ์ผ๋“ค์„ working directory๋กœ ๋Œ๋ ค๋†“์Œ (add ํ•˜๊ธฐ ์ „ ์ƒํƒœ)
  • ์ฆ‰, unstaged ๋œ ์ƒํƒœ๋กœ ๋‚จ์•„์žˆ์Œ
  • ๊ธฐ๋ณธ๊ฐ’

3. --hard

  • resetํ•˜๊ธฐ ์ „ SA, WD ๋ชจ๋“  ์ž‘์—… ๋ฆฌ์…‹
  • ๋Œ์•„๊ฐ€๋ ค๋Š” ์ปค๋ฐ‹์œผ๋กœ ๋˜๋Œ์•„๊ฐ€๊ณ ,
  • ์ดํ›„์˜ commit๋œ ํŒŒ์ผ๋“ค(tracked ํŒŒ์ผ๋“ค)์€ ๋ชจ๋‘ working directory์—์„œ ์‚ญ์ œ
  • ๋‹จ, Untracked ํŒŒ์ผ์€ Untracked๋กœ ๋‚จ์Œ

08-2

# undoing ํด๋”์—์„œ ํ–ˆ๋˜ ๋‚ด์šฉ ์ด์–ด์„œ ์ง„ํ–‰

# --hard ์˜ˆ์‹œ
$ git log --oneline
d6175dd (HEAD -> master) foo & bar
d984105 text file
a46391e first commit
$ git reset --hard d984105
HEAD is now at d984105 text file
$ git log --oneline
d984105 (HEAD -> master) text file
a46391e first commit

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



reset ํŠน์ง•๋“ค

  • reset์€ ๊ณผ๊ฑฐ๋กœ ๋Œ์•„๊ฐ€๊ฒŒ ๋˜๋ฉด ๋Œ์•„๊ฐ„ ์ปค๋ฐ‹ ์ดํ›„์˜ ์ปค๋ฐ‹์€ ๋ชจ๋‘ ํžˆ์Šคํ† ๋ฆฌ์—์„œ ์‚ฌ๋ผ์ง
  • ์ปค๋ฐ‹ ํžˆ์Šคํ† ๋ฆฌ๊ฐ€ ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ์‚ฌ๋žŒ๊ณผ ๊ณต์œ ํ•˜๋Š” ๋ธŒ๋žœ์น˜์—์„œ ์‚ฌ์šฉ ์‹œ ์ถฉ๋Œ์ด ๋ฐœ์ƒ
  • ๊ณต์œ ํ•˜๋Š” ๋ธŒ๋žœ์น˜์—์„œ ์ด์ „ ์ปค๋ฐ‹์„ ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” git revert ์‚ฌ์šฉ



revert

  • https://git-scm.com/docs/git-revert

  • โ€œํŠน์ • ์‚ฌ๊ฑด์„ ์—†์—ˆ๋˜ ์ผ๋กœ ๋งŒ๋“œ๋Š” ํ–‰์œ„โ€
  • ์ด์ „ ์ปค๋ฐ‹ ๋‚ด์—ญ์„ ๊ทธ๋Œ€๋กœ ๋‚จ๊ฒจ๋‘” ์ฑ„ ์ƒˆ๋กœ์šด ์ปค๋ฐ‹(==์—†์—ˆ๋˜ ์ผ์ž…๋‹ˆ๋‹ค!๋ฅผ ์˜๋ฏธํ•˜๋Š” commit)์„ ์ƒ์„ฑ
  • ์ปค๋ฐ‹ ํžˆ์Šคํ† ๋ฆฌ ๋ณ€๊ฒฝ ์—†์ด ํ•ด๋‹น ์ปค๋ฐ‹ ๋‚ด์šฉ๋งŒ์„ ์‚ญ์ œํ•œ ์ƒํƒœ์˜ ์ƒˆ๋กœ์šด ์ปค๋ฐ‹์„ ์ƒ์„ฑ

08-1



์ถ”๊ฐ€ commit 2๊ฐœ๋งŒ ๋” ๋‚จ๊ธฐ์ž

# undoing์—์„œ ์ด์–ด์„œ ์ง„ํ–‰
$ touch c.txt d.txt
$ git add c.txt
$ git commit -m "Add c.txt"
[master d9c38f7] Add c.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 c.txt

$ git add d.txt
$ git commit -m "Add d.txt"
[master aaf2db9] Add d.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 d.txt
$ git log --oneline
aaf2db9 (HEAD -> master) Add d.txt
d9c38f7 Add c.txt
d984105 text file
a46391e first commit



revert commit ํŽธ์ง‘๊ธฐ ์‹คํ–‰

  • ๋‹ค๋ฅธ ์‚ฌ๋žŒ๊ณผ ๊ณต์œ ํ•˜๋Š” ๋ธŒ๋žœ์น˜์—์„œ ์ด์ „ ์ปค๋ฐ‹์„ ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ
  • ์ปค๋ฐ‹ ํžˆ์Šคํ† ๋ฆฌ๊ฐ€ ๋ฐ”๋€Œ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ
$ git revert d984105
Removing b.txt
hint: Waiting for your editor to close the file... error: There was a problem 
with the editor 'vi'.
                     Please supply the message using either -m or -F option.

$ git log --oneline
# ๊ธฐ์กด commit ์ด๋ ฅ์ด ๊ทธ๋Œ€๋กœ ๋‚จ์•„์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ํ•˜๋Š” ์‹œ์ ์œผ๋กœ ์–ธ์ œ๋“  
aaf2db9 (HEAD -> master) Add d.txt
d9c38f7 Add c.txt
d984105 text file
a46391e first commit



์ •๋ฆฌ

08-3

image



๊ทธ์™ธ ๋ฐฉ๋ฒ•

$ git reflog
aaf2db9 (HEAD -> master) HEAD@{0}: reset: moving to aaf2db9
d47f656 HEAD@{1}: reset: moving to d47f656
d47f656 HEAD@{2}: commit: text file amend
aaf2db9 (HEAD -> master) HEAD@{3}: commit: Add d.txt
d9c38f7 HEAD@{4}: commit: Add c.txt
d984105 HEAD@{5}: reset: moving to d984105
d6175dd HEAD@{6}: commit (amend): foo & bar
48f0541 HEAD@{7}: commit: foo & bar
d984105 HEAD@{8}: commit (amend): text file
2cc67ed HEAD@{9}: commit: amend text file
a46391e HEAD@{10}: commit (initial): first commit
  • reflog๋Š” ์ด๋Ÿฐ์‹์œผ๋กœ ์ด์ „๊นŒ์ง€ํ–ˆ๋˜ ์ž‘์—…๋“ค reflog๋ฅผ ํ™•์ธํ•ด ๋ช‡๋ฒˆ์งธ HEAD๋กœ ์ด๋™ํ• ์ง€ ํ™•์ธํ•œ๋‹ค.

  • ๋งŒ์•ฝ HEAD@{7}๋กœ ์ด๋™ํ• ๊บผ๋ผ๋ฉด

$ git reset --hard HEAD@{7}



References

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ