Git Branch

1. branch κ΄€λ ¨ λͺ…λ Ήμ–΄

Git 브랜치λ₯Ό μœ„ν•΄ root-commit을 λ°œμƒμ‹œν‚€κ³  μ§„ν–‰ν•˜μ„Έμš”.

  1. 브랜치 생성

    (master) $ git branch {브랜치λͺ…}
  2. 브랜치 이동

    (master) $ git checkout {브랜치λͺ…}
  3. 브랜치 생성 및 이동

    (master) $ git checkout -b {브랜치λͺ…}
  4. 브랜치 μ‚­μ œ

    (master) $ git branch -d {브랜치λͺ…}
  5. 브랜치 λͺ©λ‘

    (master) $ git branch
  6. 브랜치 병합

    (master) $ git merge {브랜치λͺ…}
  • master λΈŒλžœμΉ˜μ—μ„œ {브랜치λͺ…}을 병합

2. branch 병합 μ‹œλ‚˜λ¦¬μ˜€

branch κ΄€λ ¨λœ λͺ…λ Ήμ–΄λŠ” κ°„λ‹¨ν•˜λ‹€.

λ‹€μ–‘ν•œ μ‹œλ‚˜λ¦¬μ˜€ μ†μ—μ„œ μ–΄λ–€ 상황인지 νŒŒμ•…ν•˜κ³  자유둭게 ν™œμš©ν•  수 μžˆμ–΄μ•Ό ν•œλ‹€.

상황 1. fast-foward

fast-fowardλŠ” feature 브랜치 μƒμ„±λœ 이후 master λΈŒλžœμΉ˜μ— λ³€κ²½ 사항이 μ—†λŠ” 상황

  1. feature/crud branch 생성 및 이동

    git branch feature/crud
    git checkout feature/crud
    • ν˜Ήμ€

    git checkout -b feature/crud
  2. μž‘μ—… μ™„λ£Œ ν›„ commit

    • μž„μ˜μ˜ νŒŒμΌμ„ λ§Œλ“€κ³ , commit

    • add , commit

  3. master 이동

    (feature/crud) $ git checkout master
    Switched to branch 'master'
    (master) $
  4. master에 병합

    $ git merge feature/crud
    Updating d66ff92..f219c8c
    # Fast-forward!!!
    Fast-forward
     crud.html | 0
     urls.py   | 1 +
     views.py  | 1 +
     3 files changed, 2 insertions(+)
     create mode 100644 crud.html
     create mode 100644 urls.py
     create mode 100644 views.py
    
  5. κ²°κ³Ό -> fast-foward

    $ git log --oneline
    f219c8c (HEAD -> master, feature/crud) Complete CRUD)
    d66ff92 Init
  6. branch μ‚­μ œ

    git branch -d feature/crud

상황 2. merge commit

μ„œλ‘œ λ‹€λ₯Έ 이λ ₯(commit)을 병합(merge)ν•˜λŠ” κ³Όμ •μ—μ„œ λ‹€λ₯Έ 파일이 μˆ˜μ •λ˜μ–΄ μžˆλŠ” 상황

git이 auto merging을 μ§„ν–‰ν•˜κ³ , commit이 λ°œμƒλœλ‹€.

  1. feature/signout branch 생성 및 이동

    (master)
    $ git checkout -b feature/signout
    Switched to a new branch 'feature/signout'
    (feature/signout) $
  2. μž‘μ—… μ™„λ£Œ ν›„ commit

    touch signout.txt
    git add .
    git commit -m 'Complete'
  3. master 이동

    (feature/signout)
    $ git checkout master
    Switched to branch 'master'
    (master) $
  4. master에 μΆ”κ°€ commit 이 λ°œμƒμ‹œν‚€κΈ°!!

    • λ‹€λ₯Έ νŒŒμΌμ„ μˆ˜μ • ν˜Ήμ€ μƒμ„±ν•˜μ„Έμš”!

  5. master에 병합

    $ git merge feature/signout
    Merge made by the 'recursive' strategy.
     signout.txt | 0
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 signout.txt
  6. κ²°κ³Ό -> μžλ™μœΌλ‘œ merge commit λ°œμƒ

  7. κ·Έλž˜ν”„ ν™•μΈν•˜κΈ°

    $ git log --oneline --graph
    *   2125d5b (HEAD -> master) Merge branch 'feature/signout'
    |\
    | * fb74094 (feature/signout) Complete signout
    * | 75f0abd README @ master
    |/
    * f219c8c Complete CRUD)
    * d66ff92 Init
    $ git log -1 --oneline
    2125d5b (HEAD -> master) Merge branch 'feature/signout'
  8. branch μ‚­μ œ

    $ git branch -d feature/signout
    Deleted branch feature/signout (was fb74094).


상황 3. merge commit 좩돌

μ„œλ‘œ λ‹€λ₯Έ 이λ ₯(commit)을 병합(merge)ν•˜λŠ” κ³Όμ •μ—μ„œ 같은 파일의 λ™μΌν•œ 뢀뢄이 μˆ˜μ •λ˜μ–΄ μžˆλŠ” 상황

git이 auto merging을 ν•˜μ§€ λͺ»ν•˜κ³ , 좩돌 λ©”μ‹œμ§€κ°€ λœ¬λ‹€.

ν•΄λ‹Ή 파일의 μœ„μΉ˜μ— ν‘œμ€€ν˜•μ‹μ— 따라 ν‘œμ‹œ ν•΄μ€€λ‹€.

μ›ν•˜λŠ” ν˜•νƒœμ˜ μ½”λ“œλ‘œ 직접 μˆ˜μ •μ„ ν•˜κ³  직접 commit을 λ°œμƒ μ‹œμΌœμ•Ό ν•œλ‹€.

  1. feature/signup branch 생성 및 이동

    (master)
    $ git checkout -b feature/signup
    Switched to a new branch 'feature/signup'
  2. μž‘μ—… μ™„λ£Œ ν›„ commit

    • add, commit

  3. master 이동

    (feature/signup)
    $ git checkout master
    Switched to branch 'master'
  4. master에 μΆ”κ°€ commit 이 λ°œμƒμ‹œν‚€κΈ°!!

    • 동일 νŒŒμΌμ„ μˆ˜μ • ν˜Ήμ€ μƒμ„±ν•˜μ„Έμš”!

   $ git log --oneline
   3c81134 (HEAD -> master) Hotfix signout urls.py
   2125d5b Merge branch 'feature/signout'
   75f0abd README @ master
   fb74094 Complete signout
   f219c8c Complete CRUD)
   d66ff92 Init
  1. master에 병합

    (master)
    $ git merge feature/signup
    Auto-merging urls.py
    CONFLICT (content): Merge conflict in urls.py
    Automatic merge failed; fix conflicts and then commit the result.
    
    (master|MERGING)
  2. κ²°κ³Ό -> merge conflictλ°œμƒ

    git status λͺ…λ Ήμ–΄λ‘œ 좩돌 νŒŒμΌμ„ 확인할 수 있음.

    (master|MERGING)
    $ git status
    On branch master
    You have unmerged paths.
      (fix conflicts and run "git commit")
      (use "git merge --abort" to abort the merge)
    
    Changes to be committed:
            new file:   signup.txt
    
    Unmerged paths:
      (use "git add <file>..." to mark resolution)
            both modified:   urls.py
  3. 좩돌 확인 및 ν•΄κ²°

    <<<<<<< HEAD
    # signout
    =======
    # signup
    >>>>>>> feature/signup
  4. merge commit 진행

    git commit
    • vim νŽΈμ§‘κΈ° 화면이 λ‚˜νƒ€λ‚©λ‹ˆλ‹€.

    • μžλ™μœΌλ‘œ μž‘μ„±λœ 컀밋 λ©”μ‹œμ§€λ₯Ό ν™•μΈν•˜κ³ , escλ₯Ό λˆ„λ₯Έ ν›„ :wqλ₯Ό μž…λ ₯ν•˜μ—¬ μ €μž₯ 및 μ’…λ£Œλ₯Ό ν•©λ‹ˆλ‹€.

      • w : write

      • q : quit

    • 컀밋이 확인 ν•΄λ΄…μ‹œλ‹€.

  5. κ·Έλž˜ν”„ ν™•μΈν•˜κΈ°

    (master)
    $ git log --oneline --graph
    *   4e55ba7 (HEAD -> master) Merge branch 'feature/signup'
    |\
    | * fc7f0d8 (feature/signup) Complete signup
    * | 3c81134 Hotfix signout urls.py
    |/
    *   2125d5b Merge branch 'feature/signout'
    |\
    | * fb74094 Complete signout
    * | 75f0abd README @ master
    |/
    * f219c8c Complete CRUD)
    * d66ff92 Init
    
  6. branch μ‚­μ œ

    git branch -d feature/signup

Last updated

Was this helpful?