๊ด€๋ฆฌ ๋ฉ”๋‰ด

Daehyunii's Dev-blog

06. Branch ํ•ฉ์น˜๊ธฐ ๋ณธ๋ฌธ

๐Ÿ“š Language & CS knowledge/Git & GitHub

06. Branch ํ•ฉ์น˜๊ธฐ

Daehyunii 2022. 10. 8. 02:37

01. branch ํ•ฉ์น˜๋Š” ๋ฐฉ๋ฒ•

 

  ์•ž์„œ branch๋ฅผ ์ƒ์„ฑํ•ด์„œ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ๋“ค์„ ํ…Œ์ŠคํŠธํ•ด๋ณด๊ณ  ์ด์ œ ํ”„๋กœ๊ทธ๋žจ์— ์ ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ? ๋ฐ”๋กœ branch๋ฅผ ํ•ฉ์น˜๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰ ๊ธฐ์กด ํ”„๋กœ๊ทธ๋žจ์— ๋‹ค๋ฅธ ์ฐจ์›์—์„œ ์ž‘์„ฑํ•œ ํŒŒ์ผ๋“ค์„ ๊ธฐ์กด ํ”„๋กœ๊ทธ๋žจ์— ํ•ฉ์น˜๋Š” ๊ฒƒ์ด๋‹ค. ๋‘ ๊ฐœ์˜ ์ฐจ์›์„ ํ•˜๋‚˜์˜ ์ฐจ์›์œผ๋กœ ๋‚ด์šฉ์„ ํ•ฉ์ณ ๋งŒ๋“ค์–ด ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

 

branch ํ•ฉ์น˜๊ธฐ

  branch๋ฅผ ํ•ฉ์น˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. merge ๋ฐฉ์‹๊ณผ rebase ๋ฐฉ์‹์ด๋‹ค. merge ๋ฐฉ์‹์€ ๋‘ branch๋ฅผ ํ•˜๋‚˜์˜ commit์œผ๋กœ ์ด์–ด ๋ถ™์ด๋Š” ๊ฒƒ์ด๋‹ค. ์œ„ ์‚ฌ์ง„์˜ ๋ฐฉ์‹์ด merge๋ฐฉ์‹์ด๋‹ค. merge ๋ฐฉ์‹์€ branch ์‚ฌ์šฉ ๋‚ด์—ญ์„ ๋‚จ๊ธธ ํ•„์š”๊ฐ€ ์žˆ์„๋•Œ ํ™œ์šฉํ•œ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด main branch๊ฐ€ ์žˆ๊ณ  test branch๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด์ž. test branch๋ฅผ main branch์— merge ๋ฐฉ์‹์œผ๋กœ ํ•ฉ์นœ๋‹ค๋ฉด test branch์˜ ๊ฐ commit๋œ ๊ฒƒ๋“ค์„ ๋‹ค ํ•ฉ์ณ main branch์— ์ƒˆ๋กœ์šด commit์œผ๋กœ ์ž๋™ ์ƒ์„ฑ๋œ๋‹ค. rebase ๋ฐฉ์‹์€ ํ•ฉ์น˜๋Š” ํ”์ ์ด ๋‚จ์ง€ ์•Š๋Š” ๋ฐฉ์‹์ธ๋ฐ, test branch์˜ commit๋“ค ํ•˜๋‚˜ ํ•˜๋‚˜๋ฅผ ์ „๋ถ€ main branch์˜ ๋’ค์— ํ•˜๋‚˜ ํ•˜๋‚˜์˜ commit๋“ค๋กœ ์ด์–ด๋ถ™์ด๋Š” ๋ฐฉ์‹์ด๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋•Œ๋ฌธ์— branch๋ฅผ ๋งŒ๋“  ํ”์ ์ด ๋ณด์ด์ง€ ์•Š๊ฒŒ ๋œ๋‹ค.

 

merge๋ฐฉ์‹

             test branch   -> @1 -> @2 -> @3
                          /                  \
main branch *1 -> *2 -> *3 -------------------> *4

test branch์˜ @1,@2,@3 ๋‚ด์šฉ๋“ค์ด main branch์˜ *4 commit์— ๋‹ค ๋“ค์–ด๊ฐ€๊ฒŒ ๋จ
๊ทธ๋ฆฌ๊ณ  test branch๋Š” ํ•ฉ์ณ์กŒ๊ธฐ ๋•Œ๋ฌธ์— ๋” ์ด์ƒ ๊ฐ€์ง€๋ฅผ ๋ป—์–ด๋‚˜๊ฐ€์ง€ ์•Š์Œ(๊ทธ๋ ‡๊ฒŒ ๋•Œ๋ฌธ์— branch ์‚ญ์ œ๋ฅผ ํ•ด์ฃผ์–ด
ํ—ท๊ฐˆ๋ฆฌ์ง€ ์•Š๊ฒŒ ํ•ด์ค˜์•ผํ•จ)		


rebase๋ฐฉ์‹

<ํ•ฉ๋ณ‘ ์ „>
              test branch   -> @1 -> @2 -> @3
                          /                 
main branch * -> *2 -> *3 

<ํ•ฉ๋ณ‘ ํ›„>                               
main branch * -> *2 -> *3 -------------------> @1 -> @2 -> @3

test branch ๊ฐ๊ฐ์˜ commit๋“ค์ด main branch์— ํ•˜๋‚˜ ํ•˜๋‚˜ ์ƒ์„ฑ๋˜์„œ ์ด์–ด ๋ถ™์—ฌ์ง(ํ•ฉ๋ณ‘ ํ›„์—๋Š” test branch
๋ฅผ ์‚ญ์ œํ•ด ํ—ท๊ฐˆ๋ฆฌ์ง€ ์•Š๋„๋ก ํ•ด์ค˜์•ผํ•จ)

 

02. merge 

 

  merge ๋ฐฉ์‹์œผ๋กœ branch๋ฅผ ํ•ฉ์น˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

1. ํ•ฉ์น˜๊ฒŒ ๋  branch๋กœ switch๋ฅผ ํ•œ๋‹ค.(์ฆ‰, ์œ„์˜ ์˜ˆ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ณด๋ฉด main branch๋กœ switch๋ฅผ ํ•œ๋‹ค)

2. merge ๋ช…๋ น์–ด ์ž…๋ ฅ

<๋ช…๋ น์–ด>git merge (๋ณ‘ํ•ฉํ•  branch ์ด๋ฆ„) - ์œ„์˜ ์˜ˆ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ณด๋ฉด test branch ์ด๋ฆ„์„ ๋„ฃ์–ด์•ผ ํ•œ๋‹ค.

3. :wq ํ‚ค์›Œ๋“œ๋กœ ์ €์žฅ ํ›„ ์ข…๋ฃŒ๋ฅผ ํ•ด์ค˜์•ผ ํ•จ

4. ํ•ฉ์ณ์ง„ branch๋Š” ๋” ์ด์ƒ ํ•„์š”ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์‚ญ์ œํ•œ๋‹ค.

<๋ช…๋ น์–ด> git branch -d (ํ•ฉ๋ณ‘๋œ branch ์ด๋ฆ„)

 

* ์ฐธ๊ณ ๋กœ ์ด๋ ‡๊ฒŒ ๋ณ‘ํ•ฉ์„ ํ•œ ๊ฒฝ์šฐ์—๋„ ํ•˜๋‚˜์˜ commit์ด ์ƒ์„ฑ๋œ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— reset์œผ๋กœ merge ์ „์œผ๋กœ ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

 

03. rebase

 

  rebase ๋ฐฉ์‹์œผ๋กœ branch๋ฅผ ํ•ฉ์น˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

1. ์ด์–ด๋ถ™์ผ branch๋กœ switch๋ฅผ ํ•œ๋‹ค.(์œ„์˜ ์˜ˆ๋ฅผ ๊ธฐ์ค€์œผ๋กœ test branch๋กœ switch ํ•œ๋‹ค. โ€ปmerge์™€ ๋ฐ˜๋Œ€์ž„)

2. rebase ๋ช…๋ น์–ด ์ž…๋ ฅ

<๋ช…๋ น์–ด>git rebase (์ด์–ด ๋ถ™์ž„์„ ๋‹นํ•  branch ์ด๋ฆ„) - ์œ„์˜ ์˜ˆ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ณด๋ฉด main branch ์ด๋ฆ„์„ ๋„ฃ์–ด์•ผ ํ•œ๋‹ค.

3. ๋‹ค์‹œ ์ด์–ด ๋ถ™์ž„์„ ๋‹นํ•œ branch๋กœ switch ํ•œ๋‹ค.(์œ„์˜ ์˜ˆ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ณด๋ฉด main branch๋กœ switch ํ•œ๋‹ค)

4. git merge (์ด์–ด ๋ถ™์ธ branch ์ด๋ฆ„) ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

5. ํ•ฉ์ณ์ง„ branch๋Š” ๋” ์ด์ƒ ํ•„์š”ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์‚ญ์ œํ•ด ์ค€๋‹ค.

 

* ์ฐธ๊ณ ๋กœ ์œ„์˜ 3,4๋ฒˆ ์ˆœ์„œ๋ฅผ ํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด main branch๊ฐ€ test branch ๋ณด๋‹ค ๋’ค์ณ์ง€๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— main branch๋ฅผ ๋‹ค์‹œ ๊ฐ€์žฅ ์•ž์œผ๋กœ ๋‹น๊ฒจ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

rebase ๋ฐฉ์‹์œผ๋กœ ๋ณ‘ํ•ฉ ํ•œ ์งํ›„(main์ด ๋’ค์ณ์ง€๊ฒŒ ๋จ)
merge๋ฅผ ํ†ตํ•ด main์„ ๊ฐ€์žฅ ์•ž์œผ๋กœ ๋‹ค์‹œ ์˜ฎ๊น€(์ด์ œ test๋ฅผ ์‚ญ์ œํ•ด์ฃผ๋ฉด ๋จ)
main์„ ์•ž์œผ๋กœ ์˜ฎ๊ธด ํ›„ test branch๋ฅผ ์‚ญ์ œํ•˜๋ฉด ํ”์ ์ด ๋‚จ์ง€ ์•Š์Œ