๐Ÿ“

5. Git Branch

1. Branch

๊ฐœ๋ฐœ์„ ํ•˜๋‹ค ๋ณด๋ฉด ์ฝ”๋“œ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ๋กœ ๋ณต์‚ฌํ•ด์•ผ ํ•˜๋Š” ์ผ์ด ์ž์ฃผ ์ƒ๊น๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ํ†ต์งธ๋กœ ๋ณต์‚ฌํ•˜๊ณ  ๋‚˜์„œ ์›๋ž˜ ์ฝ”๋“œ์™€๋Š” ์ƒ๊ด€์—†์ด ๋…๋ฆฝ์ ์œผ๋กœ ๊ฐœ๋ฐœ์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋งŒ์•ฝ, ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•˜์ง€ ์•Š๊ณ  ํ•œ ์ค„๋กœ ์ปค๋ฐ‹ํ•  ๊ฒฝ์šฐ ๋˜‘๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ๋™์‹œ์— ์ปค๋ฐ‹ํ•˜๋ฉด ์ถฉ๋Œ์ด ์ผ์–ด๋‚˜์ง€ ์•Š์„๊นŒ์š”? ์ด๋Ÿด ๋•Œ ๋…๋ฆฝ์ ์œผ๋กœ ๊ฐœ๋ฐœํ•˜์—ฌ ์—ฌ๋Ÿฌ์ค„๋กœ ์ปค๋ฐ‹ํ•˜๋Š” ๊ฒƒ์ด ๋ธŒ๋žœ์น˜์ž…๋‹ˆ๋‹ค.
์ด๋ ‡๊ฒŒ ๋ธŒ๋žœ์น˜๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ์—ฌ๋Ÿฌ ์ค„๋กœ ์ปค๋ฐ‹ํ•˜๋ฉด ์ถฉ๋Œ์ด ์ผ์–ด๋‚˜๋Š” ์‹œ์ ์„ ์ฐพ์•„ ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
notion imagenotion image
ย 
๋ธŒ๋žœ์น˜๋Š” ํ•˜๋‚˜์˜ ๊ธฐ๋Šฅ์„ ๊ฐ€๋ฆฌํ‚ค๊ฑฐ๋‚˜ ํŠน์ • ์ปค๋ฐ‹์„ ๊ฐ€๋ฅดํ‚ค๋Š” ๋ฐ”๋กœ๊ฐ€๊ธฐ ๊ฐ™์€ ๊ฒƒ ์ž…๋‹ˆ๋‹ค. ๋ธŒ๋žœ์น˜๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ํ˜„์žฌ ์ปค๋ฐ‹์„ ๊ฐ€๋ฅดํ‚ค๋Š” ๋ธŒ๋žœ์น˜๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.
ํŠน์ • ๋ธŒ๋žœ์น˜์—์„œ ์ž‘์—…์ค‘์ธ๋ฐ ์ƒˆ๋กœ์šด ์ปค๋ฐ‹์„ ํ•˜๋ฉด ๋ธŒ๋žœ์น˜๋Š” ๊ทธ ์ƒˆ๋กœ์šด ์ปค๋ฐ‹์„ ๊ฐ€๋ฅดํ‚ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
ย 
๊ธฐ๋ณธ ๋ธŒ๋žœ์น˜๋Š” master ๋ธŒ๋žœ์น˜ ์ž…๋‹ˆ๋‹ค. git์ €์žฅ์†Œ๋ฅผ ์ดˆ๊ธฐํ™” ํ•  ๋•Œ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด ์ง‘๋‹ˆ๋‹ค. ์ด๋•Œ, 'HEAD'๋ผ๋Š” ํŠน์ˆ˜ํ•œ ํฌ์ธํ„ฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํฌ์ธํ„ฐ๋Š” ์ง€๊ธˆ ์ž‘์—…์ค‘์ธ ๋ธŒ๋žœ์น˜๋ฅผ ๊ฐ€๋ฅดํ‚ต๋‹ˆ๋‹ค.
์œ„๋‹ˆ๋ธŒ์œ„๋‹ˆ๋ธŒ
์œ„๋‹ˆ๋ธŒ
ย 
ํ•˜๋‚˜์˜ ๊ธฐ๋Šฅ์˜ ๊ฐœ๋ฐœ์ด ์™„๋ฃŒ๋˜๋ฉด ํŠน์ • ๋ธŒ๋žœ์น˜์˜ ๋‚ด์šฉ๋“ค์„ master ๋ธŒ๋žœ์น˜์— ํ•ฉ์นฉ๋‹ˆ๋‹ค. ๋˜ ์ƒˆ๋กœ์šด ์ž‘์—…์„ ํ•˜๋ ค๊ณ  ํ•˜๋ฉด master ๋ธŒ๋žœ์น˜์—์„œ ๋‹ค์‹œ ์ƒˆ๋กœ์šด feature๋ธŒ๋žœ์น˜๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ž‘์—…์„ ํ•ฉ๋‹ˆ๋‹ค.
notion imagenotion image
notion imagenotion image
ํŠน์ • ๊ธฐ๋Šฅ์„ ์ž‘์—…ํ•˜๊ณ  ์žˆ๋Š”๋ฐ ๊ธ‰ํ•˜๊ฒŒ ๋ฒ„๊ทธ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ํ˜„์žฌ์˜ ์ž‘์—…์ƒํƒœ๋ฅผ ์ž„์‹œ๋กœ ์ปค๋ฐ‹ํ•ด๋‘๊ณ  ์ด์ „ ๊ธฐ๋Šฅ ์ž‘์—…์ค‘์ธ ์ƒํƒœ๋กœ ๋ธŒ๋žœ์น˜(์ž‘์—…ํด๋”)๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋ฒ„๊ทธ ํŒจ์น˜์šฉ ๋ธŒ๋žœ์น˜๋ฅผ ๋งŒ๋“ค๊ณ  ์ˆ˜์ • ์ž‘์—…์„ ํ•ฉ๋‹ˆ๋‹ค. ์ดํ›„ ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด ๋ฒ„๊ทธ ํŒจ์น˜์šฉ ๋ธŒ๋žœ์น˜์˜ ์ถ”๊ฐ€๋œ ๋ณ€๊ฒฝ ๋‚ด์—ญ์„ ๋งˆ์Šคํ„ฐ ๋ธŒ๋žœ์น˜์— ํ•ฉ์น  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ด๋Ÿฐ ๊ณผ์ •์„ ๊ฑฐ์น˜๋ฉด master๋ธŒ๋žœ์น˜๋Š” ๊ฐ€์žฅ ์ตœ๊ทผ์— ๋ฐฐํฌํ•œ ์†Œ์Šค์ฝ”๋“œ์˜ ์ปค๋ฐ‹์„ ๊ฐ€๋ฅดํ‚ค๊ณ  ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
ย 
์ง€๊ธˆ๋ถ€ํ„ฐ ๋ธŒ๋žœ์น˜ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
  • ํ˜„์žฌ ๋ธŒ๋žœ์น˜ ๋ชฉ๋ก๊ณผ ํ˜„์žฌ ๋ธŒ๋žœ์น˜ ํ™•์ธ
    • $ git branch
      ย 
      notion imagenotion image
ย 
  • branch ๋งŒ๋“ค๊ธฐ ๋ธŒ๋žœ์น˜๋ฅผ ํ˜„์žฌ ์‹œ์ ์— ๋งŒ๋“ค์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Gary ๋ธŒ๋žœ์น˜๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์ž.
    • $ git branch Gary
      ย 
      notion imagenotion image
ย 

2. git checkout

2-1. checkout

checkout์€ ๋ธŒ๋žœ์น˜ ๋ณ€๊ฒฝ ๋˜๋Š” ์ž‘์—… ํŠธ๋ฆฌ ํŒŒ์ผ ๋ณต์›์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ Git 2.23์—์„œ checkout ์„ ๋Œ€์‹ ํ•ด switch์™€ restore๊ฐ€ ๋„์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋Œ€์ฒด๋œ ์ด์œ ๋Š” ๊ธฐ์กด์˜ checkout์ด ๊ฐ€์ง„ ๊ธฐ๋Šฅ์ด ๋งŽ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
ย 
๋ธŒ๋žœ์น˜๋ฅผ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Gary ๋ธŒ๋žœ์น˜๋กœ ์ด๋™ํ•ด๋ณด์ž.
$ git checkout Gary
ย 
notion imagenotion image
ย 
์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์ปค๋ฐ‹ํ•ด๋ด…์‹œ๋‹ค.
$ echo 'hello branch' >> branch.txt $ git status $ git add branch.txt $ git commit -m "๊ฐœ๋ฆฌ 5"
ย 
notion imagenotion image
ย 
์ƒ๋Œ€๋ฐฉ์ด ์›๊ฒฉ ์ €์žฅ์†Œ์—์„œ pull์„ ๋ฐ›์Šต๋‹ˆ๋‹ค.
$ git pull
ย 
notion imagenotion image
ย 
์ƒ๋Œ€๋ฐฉ์ด ๋ธŒ๋žœ์น˜๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ปค๋ฐ‹์„ ํ•œ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.
$ git checkout master $ git branch binky $ git checkout binky $ git commit -m "๋น™ํ‚ค 1"
ย 
notion imagenotion image
ย 

2-2. switch

ย 
๋ธŒ๋žœ์น˜ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.
$ git switch Gary
notion imagenotion image
notion imagenotion image
ย 
๋ธŒ๋žœ์น˜๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์„œ ๋ณ€๊ฒฝํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
$ git switch -c Gary
ย 
notion imagenotion image
notion imagenotion image
ย 

2-3. restore

ํŒŒ์ผ ์ˆ˜์ • ๋‚ด์šฉ ๋ณต์›๊ณผ add๋ฅผ ํ†ตํ•ด ์Šคํ…Œ์ด์ง€์— ์˜ฌ๋ฆฐ ๊ฒƒ์„ ๋บ„ ๋•Œ์—๋„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
ย 
์šฐ์„  ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ ํ›„ ์ปค๋ฐ‹์„ ํ•ฉ๋‹ˆ๋‹ค.
$ touch README.md $ vi README.md //i->์ž‘์„ฑ ํ›„->Esc->:wq $ git add . $ git commit -m "add file" $ git status
ย 
notion imagenotion image
ย 
์ดํ›„ ํ•ด๋‹น ํŒŒ์ผ์„ ์ˆ˜์ •ํ•œ ํ›„์— ์ €์žฅ์„ ํ•˜๊ณ  ์ƒํƒœ๋ฅผ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
$ vi README.md //i->์ˆ˜์ • ํ›„->Esc->:wq
ย 
modified ํŒŒ์ผ์ด ์žˆ๋‹ค๊ณ  ๋‚˜์˜จ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
notion imagenotion image
ย 
์ด๋•Œ, ํŒŒ์ผ ๋ณ€๊ฒฝ๋‚ด์—ญ์„ modified ํŒŒ์ผ์—์„œ unmodified ํŒŒ์ผ๋กœ ๋˜๋Œ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์€ ๋‘๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
1) restore์„ ์ด์šฉํ•˜์—ฌ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๋˜๋Œ๋ ค ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
$ git restore README.md
ย 
2) checkout์„ ์ด์šฉํ•˜์—ฌ ํŒŒ์ผ ๋ณ€๊ฒฝ๋‚ด์—ญ์„ ๋˜๋Œ๋ฆฝ๋‹ˆ๋‹ค. ์ด๋•Œ, checkout์œผ๋กœ ์ง€์›Œ์ง„ ๋‚ด์šฉ์€ commit์„ ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ ๋ณต๊ตฌํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
$ git checkout -- README.md
ย 
modified ํŒŒ์ผ์ด ์—†๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
notion imagenotion image
ย 
ํŒŒ์ผ์„ ์—ด์–ด ํ™•์ธํ•œ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค.
hello world Byebye
checkout โ€” filename ์ „
hello world
checkout โ€” filename ํ›„
ย 
์Šคํ…Œ์ด์ง€ ์˜ฌ๋ฆฐ ๊ฒƒ์„ ๋นผ๋Š” ๋ฐฉ๋ฒ•์€ ๋‘๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.
$ git add . $ git status
ย 
notion imagenotion image
ย 
1) restore ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ ๋นผ๊ธฐ
$ git restore --staged README.md
ย 
1) reset ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ ๋นผ๊ธฐ
$ git reset HEAD README.md
notion imagenotion image
ย 
์ƒํƒœ๋ฅผ ํ™•์ธํ•ด ๋ณด๋ฉด ์Šคํ…Œ์ด์ง€์— ์˜ฌ๋ผ๊ฐ€ ์žˆ์ง€ ์•Š์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
notion imagenotion image
ย 

3. merge(ํ•ฉ๋ณ‘)

notion imagenotion image
ย 
๋‘ branch๋กœ ๋‚˜๋ˆ„์–ด ์ž‘์—…ํ–ˆ๋˜ ๊ฒƒ์„ ๋ณ‘ํ•ฉํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค.
$ git checkout master $ git log $ git merge binky $ git log
ย 
merge๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์œผ๋ฉด push ๊นŒ์ง€ ํ•ด์ฃผ์…”์•ผ ์›๊ฒฉ ์ €์žฅ์†Œ์— ๋ฐ˜์˜์ด ๋ฉ๋‹ˆ๋‹ค.
$ git push origin master
ย 
notion imagenotion image
ย 
์œ„์— binky ๋ธŒ๋žœ์น˜๋ฅผ mergeํ•œ ํ›„์— gary์˜ ๋ธŒ๋žœ์น˜๋ฅผ mergeํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋‘ ๋ธŒ๋žœ์น˜๊ฐ€ ๊ฐ™์€ ๊ณณ์„ ์ˆ˜์ •ํ–ˆ๋‹ค๋ฉด ์ถฉ๋Œ์ด ์ผ์–ด๋‚˜์ง€๋งŒ ์ง€๊ธˆ์€ ๋‹ค๋ฅธ ๊ณณ์„ ์ˆ˜์ •ํ–ˆ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ์ง„ํ–‰ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
$ git merge Gary $ git push origin master
ย 
notion imagenotion image
ย 

4. conflict(์ถฉ๋Œ)

๊ฐœ๋ฆฌ ๋ธŒ๋žœ์น˜์™€ master ๋ธŒ๋žœ์น˜ ๋‘˜ ๋‹ค README.md์—์„œ ์ž‘์—…์„ ํ•˜๋Š”๋ฐ ์„œ๋กœ ๋ณ€๊ฒฝ ๋‚ด์—ญ์ด ๋‹ค๋ฅด๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
์ด๋•Œ, ๊ฐœ๋ฆฌ ๋ธŒ๋žœ์น˜์™€ master ๋ธŒ๋žœ์น˜์˜ ์ž‘์—…๋‚ด์—ญ์ž…๋‹ˆ๋‹ค.
hello world hello Gary
Gary๋ธŒ๋žœ์น˜
hello world hello binky
master๋ธŒ๋žœ์น˜
mergeํ•  ๋•Œ ๋‘ ๋ธŒ๋žœ์น˜๊ฐ€ ๊ฐ™์€ ๊ณณ์„ ์ˆ˜์ •ํ–ˆ๋‹ค๋ฉด ์ถฉ๋Œ์ด ์ผ์–ด๋‚ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ถฉ๋Œ๋‚œ ์‹œ์ ์„ ์ฐพ์•„ ์ˆ˜๋™์œผ๋กœ ๊ณ ์ณ์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค.
<<<<<<< HEAD (Current Change) hello world hello binky ======= >>>>>>> branchName (Incoming Change) hello world hello Gary
ย 
conflict๋Š” ๋‘๊ฐ€์ง€ ๋ณ€๊ฒฝ ๋‚ด์—ญ์„ ๋น„๊ตํ•œ ๋‹ค์Œ์— ์›ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋‚จ๊ธฐ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
hello world hello binky Gary
ย 
์ดํ›„ commit > push ๋ฅผ ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.