Deep_Dev
article thumbnail

git diff

โœ… diff๋ž€ ๋ฌด์—‡์ด๊ณ  ์–ธ์ œ ์‚ฌ์šฉํ•˜๋Š”์ง€ ?

 git diff  : ๋ณ€๊ฒฝ๋‚ด์—ญ๋“ค๋ผ๋ฆฌ์˜ ๋น„๊ต ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค๋ฉด, ์šฐ๋ฆฌ๊ฐ€ ๋ฒ„์ „์„ ๊ด€๋ฆฌํ•˜๋Š” ๋„์ค‘์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ƒํ™ฉ์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

 

1. 10๋ฒˆ ์ปค๋ฐ‹์ค‘ 1๋ฒˆ ์ปค๋ฐ‹๊ณผ 2๋ฒˆ ์ปค๋ฐ‹์„ ๋น„๊ตํ•˜๊ณ  ์‹ถ๋‹ค.

2. ์ง€๊ธˆ pushํ•œ commit๊ณผ ์ด์ „ commit์„ ๋น„๊ตํ•˜๊ณ  ์‹ถ๋‹ค.

3. ํ˜„์žฌ staging area์— ์žˆ๋Š” ๋ณ€๊ฒฝ ๋‚ด์—ญ๊ณผ ์ง์ „์˜ commit์„ ๋น„๊ตํ•˜๊ณ  ์‹ถ๋‹ค.

4. branch1 ๋ธŒ๋žœ์น˜์— ์žˆ๋Š” commit๊ณผ main ๋ธŒ๋žœ์น˜์— ์žˆ๋Š” commit์„ ๋น„๊ตํ•˜๊ณ  ์‹ถ๋‹ค.

 

์ด๋Ÿด ๋•Œ, ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๊ฒŒ diff์ด๋‹ค. ๋ง ๊ทธ๋Œ€๋กœ ํŒจ์น˜๊ฐ„์˜ ์ฐจ์ด์ ์„ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

 

โœ… ๋‘ ์ปค๋ฐ‹๊ณผ์˜ ๋น„๊ต

ํ˜„์žฌ ์ €์žฅ์†Œ์— test.txt ํ•˜๋‚˜ ์žˆ๊ณ  1๋ฒˆ๋ถ€ํ„ฐ 5๋ฒˆ๊นŒ์ง€ commit์ด ์žˆ์œผ๋ฉฐ, ๊ฐ๊ฐ์˜ commit์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค.

1๋ฒˆ commit : text.txt์— A๋ผ๋Š” ๋ฌธ์ž์—ด ํ•˜๋‚˜ ์ €์žฅ
2๋ฒˆ commit : text.txt์— B๋ฌธ์ž ์ถ”๊ฐ€
3๋ฒˆ commit : text.txt์— C๋ฌธ์ž ์ถ”๊ฐ€
4๋ฒˆ commit : text.txt์— D๋ฌธ์ž ์ถ”๊ฐ€
5๋ฒˆ commit : text.txt์— E๋ฌธ์ž ์ถ”๊ฐ€

์—ฌ๊ธฐ์„œ ๊ฐ๊ฐ์˜ commit๋“ค ๋‚ด์—ญ์„ ๋ณด๊ธฐ ์œ„ํ•ด์„œ๋Š” git log ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ™•์ธํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ log๋ฅผ ์ฐ์œผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์„ ๋ณผ ๊ฒƒ์ด๋‹ค.


$ git log

commit f7fe32715c4bd705110196134271d8f873384316 (HEAD -> master) Author: Kang Minchul <tegongkang@gmail.com> Date: Tue Dec 1 23:06:03 2020 +0900

                    5

commit 2845ce53054627b3381c9f2515dc7545cff2347b Author: Kang Minchul <tegongkang@gmail.com> Date: Tue Dec 1 23:05:49 2020 +0900

                    4

commit de6d5c1148981e15617999c7ecaa6ec2ea21ff29 Author: Kang Minchul <tegongkang@gmail.com> Date: Tue Dec 1 23:05:38 2020 +0900

                    3

commit 1d7fce052aafb388ddf092ea315835f5154683f7 Author: Kang Minchul <tegongkang@gmail.com> Date: Tue Dec 1 23:04:52 2020 +0900

                    2

commit 6958b6b21e15aa0be36736016c5bc955b57a61be Author: Kang Minchul <tegongkang@gmail.com> Date: Tue Dec 1 23:04:38 2020 +0900

                    1


commit ์˜†์— ์ƒ๊ธด ๋ฌธ์ž์—ด(f7fe32715c4bd705110196134271d8f873384316 ๋“ฑ๋“ฑ…)

์ด๊ฑด ๊ฐ๊ฐ์˜ commit์„ ๋‚˜ํƒ€๋‚ด๋Š” commit hash์ด๋‹ค. ์ด commit์„ ๊ฐ€๋ฆฌํ‚ค๋Š”๊ฒƒ์ด๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.

 

์ž, ์—ฌ๊ธฐ์„œ git diff๋ฅผ ์ด์šฉํ•ด 5๋ฒˆ commit์ด 4๋ฒˆ commit์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌด์—‡์ด ๋ฐ”๋€Œ์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒํ•ด์•ผ ํ• ๊นŒ ?

 

git diff๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๋ช…๋ น์–ด๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์‰ฝ๊ฒŒ ๋งํ•ด์„œ

๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๋น„๊ตํ•  ๊ฒƒ์€ 4๋ฒˆ commit์— ๋น„ํ•ด 5๋ฒˆ commit์ด ๋ฌด์—‡์ด ๋‹ฌ๋ผ์กŒ๋Š”์ง€ ํ™•์ธํ•˜๋ ค๋Š”๊ฑฐ๋‹ˆ๊นŒ

 

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

 

๊ทธ๋Ÿผ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

โœ… ์›๊ฒฉ ์ €์žฅ์†Œ์™€ ๋กœ์ปฌ ์ €์žฅ์†Œ ๊ฐ„์˜ ๋น„๊ต

์œ„์™€ ๊ฐ™์€ ์‚ฌ์šฉ์˜ˆ๋Š” ์›๊ฒฉ ์ €์žฅ์†Œ์—์„œ๋„ ๋™์ผํ•˜๊ฒŒ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ์ฒ˜๋Ÿผ๋ง์ด๋‹ค.

 

โœ… ์ด์ „ commit๊ณผ ์ „์ „ commit์˜ ๋น„๊ต

๊ทผ๋ฐ ๋งค๋ฒˆ ์ด๋ ‡๊ฒŒ commit hash๋ฅผ ์“ฐ๋Š”๊ฒŒ ๊ท€์ฐฎ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.

HEAD๋Š” ํ˜„์žฌ branch๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฐ€์žฅ ์ตœ๊ทผ commit, HEAD^๋Š” ํ˜„์žฌ branch์˜ ๊ฐ€์žฅ ์ตœ๊ทผ commit์—์„œ ํ•˜๋‚˜ ์ด์ „ commit์„ ๊ฐ€๋ฆฌํ‚ค๋ฏ€๋กœ,

์ฐธ๊ณ ๋กœ HEAD์—์„œ ^์˜ ๊ฐœ์ˆ˜๋Š” ํ˜„์žฌ commit์—์„œ ๋ช‡ ๋ฒˆ์งธ ๋–จ์–ด์ง„ commit์ธ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. HEAD^^ ๋ผ๊ณ  ์“ฐ๋ฉด ๊ฐ€์žฅ ์ตœ๊ทผ commit์—์„œ ๋‘ ๊ฐœ ์ด์ „ commit ์„ ์˜๋ฏธํ•œ๋‹ค.

์œ„ ๋ช…๋ น์–ด๋Š” ์ด์ „ commit(5๋ฒˆ)๊ณผ ์ „์ „commit(4๋ฒˆ)์„ ๋น„๊ตํ•˜๋ผ๋Š” ๋ช…๋ น์–ด์ด๋‹ค.

๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

โœ… ์ด์ „ commit๊ณผ ํ˜„์žฌ ์ˆ˜์ •๋œ ๋‚ด์šฉ ๋น„๊ต

์•„์ง commitํ•˜์ง€ ์•Š์€, ์ˆ˜์ •๋œ ๋‚ด์šฉ๊ณผ ์ด์ „ commit(์ €ํฌ ์˜ˆ์ œ์—์„œ๋Š” 5๋ฒˆ commit)๊ณผ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, test,txt์— F๋ผ๋Š” ๋ฌธ์ž๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์œ„ ๋ช…๋ น์–ด๋ฅผ ์นœ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์ฆ‰, ์•„์ง commitํ•˜์ง€ ์•Š์€ ํ˜„์žฌ ์ž‘์—… ์ค‘์ธ ๋‚ด์šฉ์„ ๊ฐ€์žฅ ์ตœ๊ทผ commitํ•œ ๋‚ด์šฉ๊ณผ ๋น„๊ตํ•œ ๊ฒฐ๊ณผ๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

โœ… ๋ธŒ๋žœ์น˜๊ฐ„์˜ ๋น„๊ต

๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜์˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๋˜ํ•œ branch๋ผ๋ฆฌ์˜ ๋น„๊ต๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

 


git revert

โœ… revert๋ž€ ๋ฌด์—‡์ธ๊ฐ€

git revert๋„ reset๊ณผ ๋™์ผํ•˜๊ฒŒ commit์„ ๋˜๋Œ๋ฆฌ๋Š” ๋ช…๋ น์–ด์ด๋‹ค.

 

git revert <๋˜๋Œ๋ฆฌ๊ณ  ์‹ถ์€ commit>์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๊ฐ„๋‹จํ•˜๊ฒŒ ์˜ˆ์‹œ๋ฅผ ๋“ค๋ฉด, ์œ„ ์˜ˆ์‹œ์—์„œ 3๋ฒˆ commit์„ revertํ•˜์—ฌ ๋˜๋Œ๋ฆฌ๊ณ  ์‹ถ๋‹ค๋ฉด

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

 

โœ… reset๊ณผ ์ฐจ์ด๋Š” ?

git reset๊ณผ git revert๋Š” commit์„ ๊ณผ๊ฑฐ ์‹œ์ ์œผ๋กœ ๋˜๋Œ๋ ค์ค€๋‹ค๋Š” ์ ์—์„œ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋‚ด์ง€๋งŒ, ํฐ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค๋ฉด ๋˜๋Œ๋ฆฌ๋Š” commit๊นŒ์ง€์˜ ์ด๋ ฅ์ด ์‚ฌ๋ผ์ง€๋Š๋ƒ์˜ ์—ฌ๋ถ€์— ์žˆ๋‹ค.

 

git reset์€ ๋˜๋Œ๋ฆฐ ๋ฒ„์ „ ์ดํ›„์˜ ๋ฒ„์ „๋“ค์ด ๋ชจ๋‘ ์‚ฌ๋ผ์ง€๊ฒŒ ๋˜์ง€๋งŒ, git revert๋Š” ๋˜๋Œ๋ฆฐ ๋ฒ„์ „ ์ดํ›„์˜ ๋ฒ„์ „๋“ค์€ ๋ชจ๋‘ ์œ ์ง€๋˜๊ณ , revert๋˜์—ˆ๋‹ค๋Š” ์‚ฌ์‹ค์„ ๋‹ด์€ commit๋งŒ ์ƒˆ๋กœ ์ถ”๊ฐ€๋˜๊ฒŒ ๋œ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด, 3๋ฒˆ commit์œผ๋กœ reset์„ ํ•˜๋ฉด 4,5๋ฒˆ commit์€ ์‚ญ์ œ๋˜์ง€๋งŒ 3๋ฒˆ commit์œผ๋กœ revert๋ฅผ ํ•˜๋ฉด 4,5๋ฒˆ commit์€ ๊ทธ๋Œ€๋กœ ์œ ์ง€๋œ๋‹ค.

 

์ฆ‰, reset์€ ๊ณผ๊ฑฐ ์ž์ฒด๋ฅผ ๋ฐ”๊พธ๋Š” ๋ช…๋ น์–ด์ด๊ณ  revert๋Š” ๊ณผ๊ฑฐ๋ฅผ ๋ณ€๊ฒฝ์‹œ์ผฐ๋‹ค๋Š” ์ƒˆ๋กœ์šด commit์œผ๋กœ์จ ์ƒˆ๋กœ์šด commit์„ ๋งŒ๋“œ๋Š” ๋ช…๋ น์–ด๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.

 

์‹ค์šฉ์ ์ธ ์ธก๋ฉด์œผ๋กœ ๋ดค์„๋•Œ revert๋Š” reset๋ณด๋‹ค ๋” ์•ˆ์ „ํ•˜๊ฒŒ commit์„ ๋˜๋Œ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์ด๊ณ ,

reset์€ revert๋ณด๋‹ค commit log๋ฅผ ๋” ๊น”๋”ํ•˜๊ฒŒ ์œ ์ง€ํ•ด์ฃผ๋ฉฐ commit์„ ๋˜๋Œ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด ๊ธ€์—์„œ๋Š” ์ผ๋ฐ˜์ ์ธ ๊ด€์ ์—์„œ reset๊ณผ ๋น„๊ตํ–ˆ์„ ๋•Œ์˜ ์ด์•ผ๊ธฐ์ด๊ธด ํ•˜์ง€๋งŒ, 
-n ์˜ต์…˜์„ ์ด์šฉํ•˜๋ฉด revert๋ฅผ ์จ๋„ commit์„ ๋‚จ๊ธฐ์ง€ ์•Š์„ ์ˆ˜๋Š” ์žˆ๋‹ค.

 

 

 

๐Ÿ“ ์ถœ์ฒ˜

https://www.inflearn.com/course/%EB%B9%A0%EB%A5%B4%EA%B2%8C-git