๐Ÿช„ ๊ณผ๊ฑฐ๋กœ ๋Œ์•„๊ฐ€๋Š” ๋ฐฉ๋ฒ• : reset๊ณผ revert ์ฐจ์ด์ ๊ณผ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

Lilyยท2021๋…„ 10์›” 23์ผ
2

reset๊ณผ revert

๊นƒ์—์„œ ๊ณผ๊ฑฐ์˜ ํŠน์ • commit์œผ๋กœ ๋Œ์•„๊ฐ€๋Š” ๋ฐฉ๋ฒ•์—” ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

ํŠน์ • commit์œผ๋กœ ๋Œ์•„๊ฐ€๊ณ  ์ดํ›„์˜ commit๋“ค์€ ์‚ญ์ œํ•˜๋Š” reset๊ณผ
ํŠน์ • commit์ „์˜ ์ƒํƒœ๋กœ ๋˜๋Œ๋ฆฌ๋ฉด์„œ, ๋˜๋Œ์•„๊ฐ”๋‹ค๋Š” ๊ธฐ๋ก์„ ์ƒˆ๋กœ์šด commit์œผ๋กœ ๋‚จ๊ธฐ๋Š” revert๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.


๐Ÿช„ reset

reset์€ ๊ณผ๊ฑฐ ํŠน์ • commit์œผ๋กœ ๋Œ์•„๊ฐ€๋ฉฐ, ์ดํ›„์˜ commit๋“ค์€ ๋ฌดํšจํ™”ํ•ฉ๋‹ˆ๋‹ค.

git reset [commit hash]


ํŠน์ • commit ์ดํ›„์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ๋“ค์„ ์–ด๋–ป๊ฒŒ ๋‚จ๊ฒจ๋‘๋Š๋ƒ์— ๋”ฐ๋ผ 3๊ฐ€์ง€ ์˜ต์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  • git reset --hard : ์ดํ›„์˜ commit๊ณผ ๋ณ€๊ฒฝ๋‚ด์—ญ๋“ค์„ ๋ชจ๋‘ ์‚ญ์ œํ•œ๋‹ค.

  • git reset --soft : ์ดํ›„์˜ commit์€ ์—†์–ด์ง€์ง€๋งŒ, ๋ณ€๊ฒฝ ๋‚ด์—ญ์„ staging area์— ๋‚จ๊ฒจ๋‘”๋‹ค.

  • git reset --mixed (default): ์ดํ›„์˜ commit์€ ์—†์–ด์ง€์ง€๋งŒ, ๋ณ€๊ฒฝ ๋‚ด์—ญ์„ unstage์ƒํƒœ๋กœ ๋‚จ๊ฒจ๋‘”๋‹ค.


git reset -- hard

ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ์ˆœ์„œ๋Œ€๋กœ first commit, second commit, third commit์„ ๋‚จ๊ฒผ์Šต๋‹ˆ๋‹ค.
แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-10-24 แ„‹แ…ฉแ„Œแ…ฅแ†ซ 2 52 12

hard ์˜ต์…˜์œผ๋กœ first commit์œผ๋กœ ๋Œ์•„๊ฐ€๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. git reset --hard [first commit]
แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-10-24 แ„‹แ…ฉแ„Œแ…ฅแ†ซ 2 52 41

first commit์œผ๋กœ ๋Œ์•„๊ฐ”์Šต๋‹ˆ๋‹ค. git log๋ฅผ ์ฐ์–ด๋ณธ ๊ฒฐ๊ณผ, second commit, third commit์€ ์‚ญ์ œ๋˜๊ณ  first commit๋งŒ ๋‚จ์€ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž‘์—… ์ค‘ ์ด๋˜ ํŒŒ์ผ์€ first commit์ดํ›„์˜ ๋ณ€๊ฒฝ ๋‚ด์—ญ์€ ๋ชจ๋‘ ์—†์–ด์ง„, first commit ์‹œ์ ์œผ๋กœ ๋ณต๊ตฌ๋ฉ๋‹ˆ๋‹ค.


git reset -- soft

soft์˜ต์…˜๋„ hard์˜ต์…˜๊ณผ ๊ฐ™์ด second commit, third commit์„ ์‚ญ์ œํ•˜๊ณ  first commit๋งŒ ๋‚จ๊ฒจ๋‘ก๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ณ€๊ฒฝ ๋‚ด์—ญ์€ ์—†์–ด์ง€์ง€ ์•Š๊ณ  staging area์— ๋‚จ์•„์žˆ์Šต๋‹ˆ๋‹ค.

์ฆ‰, add ์—†์ด commit์„ ์ง„ํ–‰ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-10-20 แ„‹แ…ฉแ„’แ…ฎ 9 55 24


git reset -- mixed

๋”ฐ๋กœ ์˜ต์…˜์„ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  git reset [์ปค๋ฐ‹ ๋ฒˆํ˜ธ] ๋ช…๋ น์–ด๋ฅผ ์“ฐ๋ฉด ์ ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ์˜ต์…˜์ž…๋‹ˆ๋‹ค.

mixed์˜ต์…˜๋„ hard์˜ต์…˜๊ณผ ๊ฐ™์ด second commit, third commit์„ ์‚ญ์ œํ•˜๊ณ  first commit๋งŒ ๋‚จ๊ฒจ๋‘ก๋‹ˆ๋‹ค. ๋ณ€๊ฒฝ ๋‚ด์—ญ์€ ์—†์–ด์ง€์ง€ ์•Š๊ณ  unstage์ƒํƒœ๋กœ ๋‚จ์•„์žˆ์Šต๋‹ˆ๋‹ค(add ์ด์ „์˜ ์ƒํƒœ) hard์™€ soft์˜ ์ค‘๊ฐ„ ๋ฒ„์ „์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฆ‰, commit์„ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด add๋กœ staging area์— ์˜ฌ๋ฆฌ๊ณ  commit์„ ์ง„ํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2021-10-20 แ„‹แ…ฉแ„’แ…ฎ 10 28 47

add ๋ฌดํšจํ™”

์ปค๋ฐ‹์„ ๋Œ๋ฆฌ๋Š” ๊ฒƒ ์ด์™ธ์—๋„ reset์€ staging area๋กœ add ํ•œ ๊ฑธ ์ทจ์†Œํ•˜๋Š”๋ฐ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. add๋กœ staging area์— ํŒŒ์ผ์„ ์˜ฌ๋ฆฌ๊ณ  git reset [ํŒŒ์ผ๋ช…]๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉ ํ•ด์ฃผ๋ฉด add ํ•˜๊ธฐ ์ด์ „์˜ ์ƒํƒœ๋กœ ๋Œ์•„๊ฐ‘๋‹ˆ๋‹ค. ์ฆ‰, add ๋ช…๋ น์–ด๊ฐ€ ํ•œ ์ž‘์—…๋งŒ ๋˜๋Œ๋ ค์ค๋‹ˆ๋‹ค!


๐Ÿšซ ์›๊ฒฉ ์ €์žฅ์†Œ์— pushํ•œ ์ดํ›„์—๋Š” reset ๋ถˆ๊ฐ€

์›๊ฒฉ ์ €์žฅ์†Œ์— ์ด๋ฏธ push๋ฅผ ํ•œ ์ƒํƒœ๋ผ๋ฉด reset์„ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ pushํ•œ ์ดํ›„ ๋กœ์ปฌ์—์„œ reset์„ ํ•œ๋‹ค๋ฉด, ๋กœ์ปฌ๊ณผ ์›๊ฒฉ์˜ commit history๊ฐ€ ๋‹ฌ๋ผ์ ธ ์ถฉ๋Œ์ด ์ผ์–ด๋‚˜๋ฉฐ commit์„ ํ•  ์ˆ˜ ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


๐Ÿช„ revert

revert๋Š” ํŠน์ • commit์„ ์ทจ์†Œํ•˜๋ฉด์„œ, commit์„ ํ•˜๊ธฐ ์ „์˜ ์ƒํƒœ๋กœ ๋˜๋Œ๋ฆฝ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋˜๋Œ์•„๊ฐ„ ์ƒํƒœ์—์„œ ์ƒˆ๋กœ์šด commit์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. reset๊ณผ๋Š” ๋‹ฌ๋ฆฌ, ๊ธฐ์กด commit ๋‚ด์—ญ๋“ค์€ ๋ณด์กด๋˜๊ณ , ์ƒˆ๋กœ์šด commit์ด ์ถ”๊ฐ€๋˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํžˆ์Šคํ† ๋ฆฌ ์œ ์ง€ ์ฐจ์›์—์„œ๋„ ์ข‹์Šต๋‹ˆ๋‹ค.

git revert [commit hash]


๐Ÿš€ ๊ณผ๊ฑฐ์˜ ์ค‘๊ฐ„ commit์œผ๋กœ ๋Œ์•„๊ฐ€๊ณ  ์‹ถ๋‹ค๋ฉด?

์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜์™€ ๊ฐ™์€ commit ํžˆ์Šคํ† ๋ฆฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ "๊ตฌ์กฐ์ฒด ์ƒ์„ฑ" commit์˜ ์ƒํƒœ๋กœ ๋Œ์•„๊ฐ€๊ณ  ์‹ถ์„ ์ˆ˜ ์žˆ๊ฒ ์ฃ ? ๊ทธ๋Ÿด ๋• ์ตœ๊ทผ commit๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ revert์‹œํ‚ต๋‹ˆ๋‹ค.

commit "ํ”„๋กœํผํ‹ฐ๋ช… ์ˆ˜์ •"
commit "๋ฉ”์†Œ๋“œ ์ถ”๊ฐ€"
commit "ํ”„๋กœํผํ‹ฐ ์ถ”๊ฐ€"
commit "๊ตฌ์กฐ์ฒด ์ƒ์„ฑ"

๋จผ์ € "ํ”„๋กœํผํ‹ฐ๋ช… ์ˆ˜์ •" ์„ ์ทจ์†Œํ•˜๊ณ  revert "ํ”„๋กœํผํ‹ฐ๋ช… ์ˆ˜์ •"
๊ทธ ๋‹ค์Œ "๋ฉ”์†Œ๋“œ ์ถ”๊ฐ€" ๋ฅผ ์ทจ์†Œํ•˜๊ณ  revert "๋ฉ”์†Œ๋“œ ์ถ”๊ฐ€"
๊ทธ ๋‹ค์Œ "ํ”„๋กœํผํ‹ฐ ์ถ”๊ฐ€" ๋ฅผ ์ทจ์†Œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. revert "ํ”„๋กœํผํ‹ฐ ์ถ”๊ฐ€"

revert "ํ”„๋กœํผํ‹ฐ ์ถ”๊ฐ€"
revert "๋ฉ”์†Œ๋“œ ์ถ”๊ฐ€"
revert "ํ”„๋กœํผํ‹ฐ๋ช… ์ˆ˜์ •"
commit "ํ”„๋กœํผํ‹ฐ๋ช… ์ˆ˜์ •"
commit "๋ฉ”์†Œ๋“œ ์ถ”๊ฐ€"
commit "ํ”„๋กœํผํ‹ฐ ์ถ”๊ฐ€"
commit "๊ตฌ์กฐ์ฒด ์ƒ์„ฑ"

์ˆœ์ฐจ์ ์œผ๋กœ revertํ•˜์ง€ ์•Š๊ณ , ์ค‘๊ฐ„ commit๋งŒ revertํ•œ๋‹ค๋ฉด ์ถฉ๋Œ์ด ์ผ์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

git revert --no-commit

revertํ•œ ๊ฒฐ๊ณผ๋ฅผ commitํ•˜์ง€ ์•Š๊ณ , stage ์ƒํƒœ๋งŒ ์œ ์ง€ํ•˜๋ ค๋ฉด git revert --no-commit ๋˜๋Š” git revert -n ์˜ต์…˜์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

git revert [1๋ฒˆcommit hash][2๋ฒˆcommit hash]...

์—ฌ๋Ÿฌ๊ฐœ์˜ commit์„ ํ•œ๊บผ๋ฒˆ์— revertํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

git revert [ํ”„๋กœํผํ‹ฐ๋ช… ์ˆ˜์ • hash] [๋ฉ”์†Œ๋“œ ์ถ”๊ฐ€ hash] [ํ”„๋กœํผํ‹ฐ ์ถ”๊ฐ€ hash]



๐Ÿ˜Š ์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ

[Git] ๋ช…๋ น์–ด(5) - reset, revert
git reset, revert๋กœ ์ด์ „ ์ปค๋ฐ‹์œผ๋กœ ๋Œ๋ฆฌ๊ธฐ

profile
i๐ŸŽS ๊ฐœ๋ฐœ์„ ํ•ฉ๋‹ˆ๋‹ค

0๊ฐœ์˜ ๋Œ“๊ธ€