Skip to content

Latest commit

ย 

History

History
161 lines (111 loc) ยท 4.84 KB

Git_Merge_vs_Git_Rebase.md

File metadata and controls

161 lines (111 loc) ยท 4.84 KB

Git Merge vs Git Rebase

Merge, Rebase

๊ณตํ†ต์ 

  • ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ๋ธŒ๋žœ์น˜์˜ ๋‚ด์šฉ์„ ๋ชจ๋‘ ๋ฐ˜์˜

์ฐจ์ด์ 

  • Merge๋Š” ๋‘ ๋ธŒ๋žœ์น˜์˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๋ชจ๋‘ ๋ฐ˜์˜ํ•œ ์ƒˆ๋กœ์šด ์ปค๋ฐ‹์„ ์ƒ์„ฑ (๋จธ์ง€ ์ปค๋ฐ‹)
  • Rebase๋Š” ์ด๋ฅผ ํ˜ธ์ถœํ•œ ๋ธŒ๋žœ์น˜์—์„œ ๋‹ค๋ฅธ ๋ธŒ๋žœ์น˜๋กœ base๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ƒˆ๋กœ์šด ์ปค๋ฐ‹์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ๋ณ€๊ฒฝ ์‚ฌํ•ญ ๋ฐ˜์˜

Merge

git merge {๋จธ์ง€ํ•  ๋ธŒ๋žœ์น˜}
  • ํ˜ธ์ถœํ•œ ๋ธŒ๋žœ์น˜์— ๋จธ์ง€ํ•  ๋ธŒ๋žœ์น˜์˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๋ฐ˜์˜

์žฅ์ 

  • Rebase์—์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์œ„ํ—˜์„ฑ์€ ์—†์Œ

๋‹จ์ 

  • ํ•ญ์ƒ ๋‘ ๋ธŒ๋žœ์น˜์˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๋ชจ๋‘ ๋ฐ˜์˜ํ•œ ์ƒˆ ์ปค๋ฐ‹์ด ์ƒ๊น€
  • ๋”ฐ๋ผ์„œ ์ปค๋ฐ‹ ๋‚ด์—ญ๊ณผ Git Graph๊ฐ€ ์ง€์ €๋ถ„ํ•ด์งˆ ์ˆ˜ ์žˆ์Œ

Rebase

์žฅ์ 

  • ๋ณ„๋„์˜ ์ƒˆ ์ปค๋ฐ‹ ์—†์ด ์–‘ ์ชฝ์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๋ชจ๋‘ ๋ฐ˜์˜ํ•  ์ˆ˜ ์žˆ์Œ
  • ๋‚ด๊ฐ€ ๋งˆ์น˜ ์ƒ๋Œ€๋ฐฉ์ด ์ž‘์—…ํ•œ ์ดํ›„์— ์ž‘์—…ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ํžˆ์Šคํ† ๋ฆฌ๊ฐ€ ๊ธฐ๋ก๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ธŒ๋žœ์น˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ

๋‹จ์ 

  • ๋ฆฌ๋ฒ ์ด์Šค์˜ ๋™์ž‘ ์›๋ฆฌ์— ์˜ํ•œ ์œ„ํ—˜์„ฑ
    • ๋จธ์ง€๋Š” ๊ทธ๋ƒฅ ํ˜„์žฌ ๋ธŒ๋žœ์น˜์—์„œ ๋จธ์ง€ํ•  ๋ธŒ๋žœ์น˜์™€์˜ ๋ณ€๊ฒฝ ๋‚ด์—ญ์„ ๋ฐ˜์˜ํ•œ ์ƒˆ ์ปค๋ฐ‹ ์ฐ๊ณ  ๋
    • ๋ฆฌ๋ฒ ์ด์Šค๋Š” ํ˜„์žฌ ๋ธŒ๋žœ์น˜์—์„œ ๋ฆฌ๋ฒ ์ด์Šคํ•  ๋ธŒ๋žœ์น˜๋ฅผ patch๋กœ ๋งŒ๋“ค๊ณ  ์—ฌ๊ธฐ์„œ ํ˜„์žฌ ๋ธŒ๋žœ์น˜์˜ ์ž‘์—…๋‚ด์šฉ์„ ๊ทธ๋Œ€๋กœ ๋‹ค์‹œ ์ปค๋ฐ‹ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘
      • ์ฆ‰, base๋ฅผ ๋ฆฌ๋ฒ ์ด์Šคํ•  ๋ธŒ๋žœ์น˜์˜ ๋งˆ์ง€๋ง‰ ์ปค๋ฐ‹์œผ๋กœ ์˜ฎ๊ธด ์ดํ›„ ์‹œ์ ์˜ ๋ชจ๋“  ์ปค๋ฐ‹์ด ์ƒˆ๋กœ ์ž‘์„ฑ๋จ
        • ์ปค๋ฐ‹ ํ•ด์‹œ๊ฐ€ ๋ณ€๊ฒฝ๋˜๊ณ , ๋ฆฌ๋ฒ ์ด์Šค ์ด์ „์˜ ์ปค๋ฐ‹๊ณผ ์™„์ „ํžˆ ๋‹ค๋ฅธ ์ปค๋ฐ‹์œผ๋กœ ๊ฐ„์ฃผ๋จ
    • ๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด๋ฏธ ์›๊ฒฉ ์ €์žฅ์†Œ์— pushํ•œ ์ปค๋ฐ‹์„ rebase ํ•ด์„œ๋Š” ์•ˆ ๋จ
      1. ๋‚ด ์ž‘์—…์— ๋Œ€ํ•œ ์ปค๋ฐ‹ A, B๋ฅผ origin์— push
      2. ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ž‘์—… ๋‚ด์šฉ์„ ๋ฐ˜์˜ํ•˜๊ณ ์ž rebase
        • base๊ฐ€ ์žฌ์„ค์ •๋˜๋ฉฐ ์ดํ›„์˜ ์ž‘์—…๋ฌผ์ธ A, B ์ปค๋ฐ‹์€ Aโ€™, Bโ€™ ์ปค๋ฐ‹์œผ๋กœ ๋ณ€๊ฒฝ๋จ
      3. ์ดํ›„ ๋‹ค๋ฅธ ์ž‘์—… C๋ฅผ ์ˆ˜ํ–‰ํ•ด ์ปค๋ฐ‹ํ•˜๊ณ  origin์— push
      4. ๊ธฐ์กด์˜ A, B์™€ Aโ€™, Bโ€™๋Š” ๊ฐ™์€ ์ž‘์—…์ž„์—๋„ ๋‹ค๋ฅธ ์ปค๋ฐ‹์œผ๋กœ ๊ฐ„์ฃผ๋จ

Rebase ๋ฌธ์ œ์  ์˜ˆ์‹œ

  1. ๋งˆ์Šคํ„ฐ์—์„œ ๋ธŒ๋žœ์น˜ a๋กœ ์ฒดํฌ์•„์›ƒํ•˜๊ณ  ์ƒˆ ์ปค๋ฐ‹์„ ์ƒ์„ฑ

    commit f132497e029781371a1fb6602a27d44947327be7 (HEAD, master, a)
    Author: ~
    Date:   ~
    
        a commit (1)
    
    commit 2a56f8e097407782b80d01b963b51a5da52052eb
    Author: ~
    Date:   ~
    
        Initial commit
  2. ๋งˆ์Šคํ„ฐ์—์„œ ๋ธŒ๋žœ์น˜ b๋กœ ์ฒดํฌ์•„์›ƒํ•˜๊ณ  ๋‘ ๊ฐœ์˜ ์ƒˆ ์ปค๋ฐ‹์„ ์ƒ์„ฑ

    commit 5c966a660743e97751b2cc3a02dbb6af52742c95 (HEAD -> b)
    Author: ~
    Date:   ~
    
        b commit (2)
    
    commit 9d2628054585e816abb2d3c1e1e4805cd2fa0c58
    Author: ~
    Date:   ~
    
        b commit (1)
    
    commit 2a56f8e097407782b80d01b963b51a5da52052eb (master)
    Author: ~
    Date:   ~
    
        Initial commit
  3. ๋งˆ์Šคํ„ฐ๊ฐ€ a๋ฅผ ๋ฆฌ๋ฒ ์ด์Šคํ•˜๊ณ (์ถฉ๋Œ ์—†์Œ), b๋กœ ์ด๋™ํ•ด ๋งˆ์Šคํ„ฐ๋ฅผ ๋ฆฌ๋ฒ ์ด์Šค(a, b๊ฐ„ ์ž‘์—… ๋‚ด์šฉ ์ถฉ๋Œ)

  4. ์ถฉ๋Œ ํ•ด๊ฒฐ ํ›„ git rebase --continue ๋ช…๋ น์„ ํ†ตํ•ด ๋ฆฌ๋ฒ ์ด์Šค๋ฅผ ์ˆ˜ํ–‰

    commit **9d4cf26dcbd273d3ebae0417c105c400f131b333** (HEAD -> b)
    Author: ~
    Date:   ~
    
        b commit (2)
    
    commit **bfa0260647e48ec204a450c459a1164a14f98e94**
    Author: ~
    Date:   ~
    
        b commit (1)
    
    commit f132497e029781371a1fb6602a27d44947327be7 (master, a)
    Author: ~
    Date:   ~
    
        a commit (1)
    
    commit 2a56f8e097407782b80d01b963b51a5da52052eb
    Author: ~
    Date:   ~
    
        Initial commit
    • master์˜ base(์‹œ์ž‘์ )๊ฐ€ b์—์„œ์˜ master์˜ ์œ„์น˜(Initial Commit)์—์„œ ๋ฆฌ๋ฒ ์ด์Šค๋ฅผ ์‹œ๋„ํ•œ master(a commit (1))๋กœ ์ด๋™
    • ๊ทธ ์ดํ›„์˜ b์˜ ์ปค๋ฐ‹์˜ ์ปค๋ฐ‹ ํ•ด์‹œ๊ฐ€ ๋ชจ๋‘ ๋ฐ”๋€œ
      • ๋ณต์‚ฌํ•œ ๋ธŒ๋žœ์น˜์— ์ปค๋ฐ‹์„ ์ƒˆ๋กœ ์“ฐ๊ณ  ์ด๋ฅผ ์ƒˆ b ๋ธŒ๋žœ์น˜๋กœ ๊ฐ„์ฃผํ•˜๋ฏ€๋กœ
    • ๋งŒ์•ฝ ์›๊ฒฉ์— ์•ž์„  b์˜ ์ปค๋ฐ‹ ๋‚ด์—ญ์ด ๋ฐ˜์˜๋˜์–ด ์žˆ์—ˆ๋‹ค๋ฉด 4๋ฒˆ์˜ ๊นƒ ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ํ‘ธ์‹œํ•  ์‹œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒ

์™ธ๋ถ€ ์ €์žฅ์†Œ์˜ ์ฝ”๋“œ๋ฅผ pullํ•˜๋Š” ๊ฒฝ์šฐ

fetch?

git fetch {์›๊ฒฉ์ €์žฅ์†Œ ๋ณ„์นญ} {์›๊ฒฉ์ €์žฅ์†Œ ๋ธŒ๋žœ์น˜}
  • FETCH_HEAD๋ผ๋Š” ๋ธŒ๋žœ์น˜์— ํ•ด๋‹น ์›๊ฒฉ ๋ธŒ๋žœ์น˜์˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๋ฐ˜์˜

fetch-merge (default pull)

# 1
git config pull.rebase false  # merge (the default strategy)
git pull origin {branch}

# 2
git fetch origin {branch}
git merge FETCH_HEAD

fetch-rebase

# 1
git config pull.rebase true   # change strategy to rebase
git pull origin {branch}

# 2
git fetch origin {branch}
git rebase FETCH_HEAD

Merge, Rebase ์ „๋žต ๊ฒฐ๋ก 

  • ๊ธฐ๋ณธ ์ „๋žต์€ rebase๋กœ ํ•˜๋˜ ์™ธ๋ถ€์— PR์„ ์˜ฌ๋ ค๋†“๊ณ  ์ถ”๊ฐ€ ์ž‘์—… ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ง์ ‘ fetch-merge ์ˆ˜ํ–‰
  • ๋งŒ์•ฝ ์‹ค์ˆ˜๋กœ ์™ธ๋ถ€์— PR ์žˆ๋Š”๋ฐ git pull ํ•ด์„œ rebase๋ฅผ ์‹œ๋„ํ•œ ๊ฒฝ์šฐ git rebase --abort๋กœ ๋ฆฌ๋ฒ ์ด์Šค ์ทจ์†Œํ•˜๊ณ  ๋‹ค์‹œ fetch-merge