git rebaseโœŒ๐ŸผโœŒ๐Ÿผ

Jung Hyun Kimยท2020๋…„ 7์›” 9์ผ
0

git

๋ชฉ๋ก ๋ณด๊ธฐ
4/6

git rebase ๐Ÿ’จ

๋‚ด ๊นƒ ์ปค๋ฐ‹์˜ blocker... git rebase ๋ฌด์„œ์šด ๋„ˆ๋ž€๋…€์„.....ํ•˜์ง€๋งŒ ์“ฐ๋Š”๋ฐ๋Š” ๋‹ค ์ด์œ ๊ฐ€ ์žˆ๋‹ค.. ์—ด์‹ฌํžˆ ์•Œ์•„๊ฐ€ ๋ณด์ž! ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ

์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ ์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ2 ์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ3

๊ณต์‹ ๋ฌธ์„œ

๊ทธ๋ž˜์„œ.. git merge ๋‘๊ณ  ์™œ git rebase๋ฅผ ์“ฐ๋‚˜์š”?

  • git rebase ๋‚˜ git merge ๋‚˜ ๊ฒฐ๊ตญ ์šฐ๋ฆฌ์˜ ๋ชฉ์ ์€ ๋กœ์ปฌ ๋ธŒ๋žœ์น˜์—์„œ ์ž‘์—…ํ•˜๋˜ ๊ฒฐ๊ณผ๋ฌผ์„ ๋งˆ์Šคํ„ฐ ๋ธŒ๋žœ์น˜๋กœ ํ•ฉ์น˜๋Š” ๊ฒƒ์ธ๋ฐ, ๊ทธ ๋ฐฉ๋ฒ•์„ ์–ด๋–ป๊ฒŒ ํ•  ๊ฒƒ์ด๋ƒ์˜ ์ฐจ์ด์ด๋‹ค.
  • git merge๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋งŽ์€ ๊ฐœ๋ฐœ์ž๋“ค์ด ํ•œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ž‘์—…ํ•œ๋‹ค๋ฉด ๊ทธ branch๋“ค์€ messy branch history์˜ ๋ํŒ์™•์ด ๋  ๊ฒƒ์ด๋‹ค!
    messy git branch history
  • ์ด๋Ÿฐ ๋ณต์žกํ•œ ๋ธŒ๋žœ์น˜ ๊ตฌ์กฐ๋Š” , ๋‚˜์ค‘์— ๊ฐœ๋ฐœ์ž๋“ค์ด history๋ฅผ ํŒŒ์•…ํ•ด ๋‚˜๊ฐˆ ๋•Œ, ํ•˜๋‚˜ํ•˜๋‚˜ ๋ฌธ์ œ๋ฅผ track down ํ•ด๋‚˜๊ฐ€๊ธฐ๋„ ๋ณต์žกํ•˜๊ธฐ์— ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ํ•  ๋ฐฉ๋ฒ•์ด git rebase๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.


git merge vs git base ๊ตฌ์กฐ ๋น„๊ต

git merge ํŠน์ง•

  • ๋ชจ๋“  commit ๊ณผ history๋ฅผ ๊ธฐ๋กํ•˜๊ณ , branch๋“ค์ด ์ˆ˜์ • ๋˜์ง€ ์•Š์€์ฑ„(non-destructive)๋กœ ์กด์žฌ ํ•œ๋‹ค.
  • merge ๋ฐ ์ตœ์‹  ๋งˆ์Šคํ„ฐ branch๋กœ update๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋„ commit์„ ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์—„์ฒญ๋‚œ ์–‘์˜ commit์ด ์Œ“์ธ๋‹ค.

git rebase ํŠน์ง•

  • ์•„๋ž˜ ๊ทธ๋ฆผ ์ฒ˜๋Ÿผ, ์ „์ฒด feature branch๋ฅผ master ๋กœ ์˜ฎ๊ธฐ๋Š” ํ˜•ํƒœ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.
    -merge commit์ด ์•„๋‹ˆ๋ผ master๋ฅผ rebase ์‹œ์ž‘์ ์„ ๋ฐ”๊พผ๋‹ค ๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.
  • git rebase๋ฅผ ํ†ตํ•ด ํ›จ์”ฌ ๊น”๋”ํ•œ project history ๊ด€๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ณ , ๋ถˆํ•„์š”ํ•œ merge commit์„ ์—†์•จ ์ˆ˜ ์žˆ๋‹ค.
  • git flow๊ฐ€ timeline ์ฒ˜๋Ÿผ ํ•œ ์ค„๋กœ ๊ด€๋ฆฌ๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— history trace๊ฐ€ ํŽธํ•˜๋‹ค.

git rebase ๋ช…๋ น์–ด ์ตํžˆ๊ธฐโœ๐Ÿผ

git base ๊นŒ์ง€์˜ workflow

  1. git clone 'ํด๋ก  ๋ฐ›์„ ๋ ˆํฌ์˜ url'
  2. git branch 'feature/main'
  3. git checkout 'feature/main'
  4. ์—ด์‹ฌํžˆ ์ž‘์—… ๋ฐ ์ˆ˜์ •
  5. git add .
  6. git commit -m"complete main"
  7. git checkout master
  8. git pull origin master
  9. git checkout feature/main

์—ฌ๊ธฐ๊นŒ์ง€๋Š” merge๋‚˜ rebase๋‚˜ ๊ณผ์ •์€ ๋˜‘๊ฐ™๋‹ค ์ด ์ดํ›„๋ถ€ํ„ฐ๊ฐ€ ์ค‘.์š”!

  1. git rebase โ€“i master feature/main

-i โ€”vim์„ ์ผœ์„œ ์‹คํ–‰ํ•œ๋‹ค๋Š” ๋œป

commit ๋ชฉ๋ก ์ค‘ ์ œ์ผ ์ƒ๋‹จ commit, ์„ ํƒํ•˜๋ ค๋Š” commit์„ p(pick) ๋‚˜๋จธ์ง€๋ฅผ s(squash)๋กœ ์ง€์ •

11.1 successfully rebased and updated ๊ฐ€ ๋œจ๋ฉด git push origin feature/main


11.2 conflict๊ฐ€ ๋œจ๋ฉด conflict ์ˆ˜์ •ํ›„

git add .
git rebase --continue

ํ•ด๊ฒฐ ๋ ๋•Œ ๊นŒ์ง€ ๋ฌดํ•œ ๋ฐ˜๋ณต git add . git rebase --continue

  1. ์ˆ˜์ •ํ•˜๊ณ  successfully rebased and updated ๊ฐ€ ๋œจ๋ฉด git push origin feature/main

  2. ๋งŒ์•ฝ ํ˜„์žฌ master๊ฐ€ ๊ทธ์‚ฌ์ด ์—…๋ฐ์ดํŠธ ๋˜์—ˆ๋‹ค๋ฉด, 7๋ฒˆ ๋ถ€ํ„ฐ ๋‹ค์‹œ ์‹คํ–‰ ํ•ด์•ผ ํ•˜๋Š”๋ฐ,์ด ๋•Œ ๋‹ค์‹œ ์ง„ํ–‰ํ•˜๋ฉด rejected ๊ฐ€ ๋˜๋ฉด git push origin feature/main -f force push ํ•œ๋‹ค

  3. ๊ทธ ํ›„ github์— ๊ฐ€์„œ Pull Request ์ž‘์„ฑ

(ํ˜น์‹œ๋‚˜ ์ž˜๋ชป๋œ ์ •๋ณด๊ฐ€ ์žˆ์œผ๋ฉด ๋ง์”€ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค (๊นƒ์ƒ์•„ ใ…œใ…œ))

์ ์žฌ์ ์†Œ์— ์‚ฌ์šฉํ•˜๋Š” rebase์™€ merge

  • ๊ฒฐ๊ตญ ๋‘˜์˜ ์ฐจ์ด๋Š” ๋‚ด๊ฐ€ ์ค‘ ์ ์„ ์–ด๋””์— ๋‘˜ ๊ฒƒ์ด๋ƒ ์ด๋‹ค.
  • "๋‚˜๋Š” ๋ชจ๋“  commit์ด ํ•ด๋‹น ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ history์˜€๊ธฐ ๋•Œ๋ฌธ์— history๋ฅผ reviseํ•˜๋Š” ๊ฒƒ์€ ํ—ˆ์šฉ ๋ชปํ•ด!" ๊ทธ branch history๊ฐ€ messy ํ•œ ๊ฒƒ ๋˜ํ•œ It is what it is! ๋ผ๋ฉด git merge
  • "์•„๋‹ˆ! ๋‚ด๊ฐ€ ํ”„๋กœ์ ํŠธ ๊ฒฐ๊ณผ๋ฌผ์— ํ•˜๋‚˜ ํ•˜๋‚˜ draft์˜ ๋‚ด์šฉ๋“ค์„ ์–ด๋–ป๊ฒŒ ์ˆ˜์ •ํ•œ ๊ฒƒ ๊นŒ์ง€ ๋‚จ๊ฒจ์•ผํ•ด? ๋‚œ ์ •๋ ฌ ๋œ ๊น”๋”ํ•œ ๊ณผ์ •์„ ๋‚จ๊ธฐ๊ณ  ์‹ถ์–ด!" ๋ผ๋ฉด git rebase
profile
์ฝ”๋ฆฐ์ด ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž๐Ÿ’ป๐Ÿ’›๐Ÿค™๐Ÿผ

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