티스토리 뷰

branch

기본 명령어

  • 브랜치 목록

    $ git branch
  • 브랜치 생성

$ git branch {브랜치이름}
  • 브랜치 이동
$ git checkout {브랜치 이동}
# 브랜치 생성 및 이동
$ git checkout -b {브랜치 이름}
  • 브랜치 병합
(master)$ git merge {브랜치이름}

{브랜치이름}을 (master)로 병합

  • 브랜치 삭제
$ git branch -d {브랜치 이름}


![git](markdown-images/git.png)

Branch 상황 3가지 작업

# 어쨌든 시작은, root-commit이 있어야 작업된다.
$ touch README.md

$ git commit -m "Init"
[master (root-commit) bce28b7] Init
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md

$ git branch
* master
* 
$ git branch hellobranch

# master 브랜치 생성 
$ git branch
  hellobranch
* master

상황 1. fast-foward

fast-foward는 feature 브랜치 생성된 이후 master 브랜치에 변경 사항이 없는 상황

  1. feature/blog branch 생성 및 이동
(master)$ git checkout -b feature/blog
Switched to a new branch 'feature/blog'

 

  1. 작업 완료 후 commit
(feature/blog)$ touch blog.html
(feature/blog)$ git add .

(feature/blog)$ git commit -m 'Complete blog app'
[feature/blog b0bb38a] Complete blog app
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 blog.html

(feature/blog)$ git log --oneline
# HEAD는 현재 branch를 linked list로 가르키고 있다.
b0bb38a (HEAD -> feature/blog) Complete blog app
5ad9d95 (master) Hellobranch
bce28b7 Init

 

  1. master 이동
(feature/blog)$ git checkout master
Switched to branch 'master'

(master)$ git log --oneline
5ad9d95 (HEAD -> master) Hellobranch
bce28b7 Init

 

  1. master에 병합
    (master)$ git merge feature/blog
    Updating 5ad9d95..b0bb38a
    Fast-forward
    blog.html | 0
    1 file changed, 0 insertions(+), 0 deletions(-)
    create mode 100644 blog.html

 

  1. 결과 -> fast-foward (단순히 HEAD를 이동)

    HEAD가 두가지 brance (master, feat)를 가르킨다.

(master)$ git log --oneline
# HEAD가 두가지 brance (master, feat)를 가르킨다.  => Fast Forward
b0bb38a (HEAD -> master, feature/blog) Complete blog app
5ad9d95 Hellobranch
bce28b7 Init

 

  1. branch 삭제
(master)$ git branch -d feature/blog
Deleted branch feature/blog (was b0bb38a).

 

상황 2. merge commit

서로 다른 이력(commit)을 병합(merge)하는 과정에서 다른 파일이 수정되어 있는 상황

git이 auto merging을 진행하고, commit이 발생된다.

 

  1. feature/poll branch 생성 및 이동
(master)
$ git checkout -b feature/poll
Switched to a new branch 'feature/poll'
(feature/poll) $
  1. 작업 완료 후 commit
    $ touch poll.html
    $ git add poll.html
    $ git commit -m 'Complete poll app'
    [feature/poll 7e2990c] Complete poll app
    1 file changed, 0 insertions(+), 0 deletions(-)
    create mode 100644 poll.html
    

$ git log --oneline
7e2990c (HEAD -> feature/poll) Complete poll app
1aa1e05 Complete blog app
5315196 Hellobranch
7bcaf91 Init


3. master 이동
```bash
(feature/poll)$ git checkout master
Switched to branch 'master'
  1. master에 추가 commit 이 발생시키기!!

    • 다른 파일을 수정 혹은 생성하세요!
    • hotfix.css 파일을 생성하고, README.md 파일을 수정해본다.
(master)$ touch hotfix.css
(master)$ git add .
(master)$ git commit -m 'hotfix in master'
[master ba31f14] hotfix in master
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 hotfix.css
deletions(-)
create mode 100644 hotfix.css

(master)$ git log --oneline
ba31f14 (HEAD -> master) hotfix in master
a85dc82 Complete blog app
b344efa Init
  1. master에 병합

    • Merge branch 'feature/poll' into master 어쩌고 vim 화면 나오면 캡쳐해서 넣기~!
      • => 까먹고 안했으니 집가서 할 때 수정하기 (는 집가서 안함..)
(master)$ git merge feature/poll
Merge made by the 'recursive' strategy.
poll.html | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 poll.html
  1. 결과 -> 자동으로 merge commit 발생

    • vim 편집기 화면이 나타납니다.
    • 자동으로 작성된 커밋 메시지를 확인하고, esc를 누른 후 :wq를 입력하여 저장 및 종료를 합니다.
      • w : write
      • q : quit
    • 커밋을 확인 해봅시다.
  1. 그래프 확인하기
(master)$ git log --oneline --graph
*   2e6fd8b (HEAD -> master) Merge branch 'feature/poll' into master
|\
| * f1c0950 (feature/poll) Complete poll app
* | ba31f14 hotfix in master
|/
* a85dc82 Complete blog app
* b344efa Init
  1. branch 삭제
(master)$ git branch -d feature/poll
Deleted branch feature/poll (was f1c0950).

상황 3. merge commit 충돌

서로 다른 이력(commit)을 병합(merge)하는 과정에서 동일 파일이 수정되어 있는 상황

git이 auto merging을 하지 못하고, 해당 파일의 위치에 라벨링을 해준다.

원하는 형태의 코드로 직접 수정을 하고 merge commit을 발생 시켜야 한다.

  1. feature/board branch 생성 및 이동
(master)$  git checkout -b feature/board
  1. 작업 완료 후 commit
  • (feature/board)에서 README 수정해서 충돌 발생시키기
(feature/board)$ touch board.html


(feature/board)$ git status
On branch feature/board
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
          # 추후에 README.md를 수정해서 충돌을 발생시킨다.
        modified:   README.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        board.html

no changes added to commit (use "git add" and/or "git commit -a")


(feature/board)$ git add .
(feature/board)$ git commit -m "Complete board and update README.ME"
[feature/board 5c87491] Complete board and update README.ME
 2 files changed, 2 insertions(+)
 create mode 100644 board.html


(feature/board)$ git log --oneline
5c87491 (HEAD -> feature/board) Complete board and update README.ME
2e6fd8b (master) Merge branch 'feature/poll' into master
ba31f14 hotfix in master
f1c0950 Complete poll app
a85dc82 Complete blog app
b344efa Init
  1. master 이동
    (feature/board)$ git checkout master
    Switched to branch 'master'
  • master에서 README.ME를 수정한다.
  1. master에 추가 commit 이 발생시키기!!
  • 동일 파일을 수정 혹은 생성하세요!
    # README 수정 후 commit
     $ git add README.md
     $ git commit -m 'Update README'
     $ git log --oneline
     a6530b3 (HEAD -> master) Update README
     fb9c33d Merge branch 'feature/poll' into master
     b25f8ab hofix in master
     7e2990c Complete poll app
     1aa1e05 Complete blog app
     5315196 Hellobranch
     7bcaf91 Init
  1. master에 병합
(master)$ git merge feature/board
Auto-merging README.md
# 내용 충돌 
# README.md에서 충돌
CONFLICT (content): Merge conflict in README.md
# 자동 병합 실패;
# 충돌을 고치고 다시 커밋해라!
Automatic merge failed; fix conflicts and then commit the result.

(master|MERGING) $ git status
On branch master
You have unmerged paths.
# 충돌 고치고 commit!
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)
# 커밋될 변경사항
Changes to be committed:
        new file:   board.html

# 병합 되지 않은 파일들
Unmerged paths:
# 해결하고 add해!
  (use "git add <file>..." to mark resolution)
        both modified:   README.md
  1. 결과 -> *merge conflict발생
  1. 충돌 확인 및 해결
    # master에서 수정함만 남겨두던지 feauter/board만 남겨두던지
    <<<<<<< HEAD
    def index(request):
     return 
    =======
    def create(request):
     return 
    >>>>>>> feature/board
    

master에서 수정함


* 수정 후에는 반드시 해당 파일 add

$ git add .


8. merge commit 진행

```bash
$ git commit
  • vim 편집기 화면이 나타납니다.
  • 자동으로 작성된 커밋 메시지를 확인하고, esc를 누른 후 :wq를 입력하여 저장 및 종료를 합니다.
    • w : write
    • q : quit
  • 커밋이 확인 해봅시다.
  1. 그래프 확인하기
    • 중간에 꼬여서 좀 다르게 나왔지만
$ git log --oneline --graph
*   7005a60 (HEAD -> master) Merge branch 'feature/board' into master
|\
| * 29f7ad4 (feature/board) Add poll.html in feature/board
* | 7c6a228 Add poll_master.html in master
* | 0563519 Update README
|/
* 4b92247 Init
  1. branch 삭제
$ git branch -d feature/board
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함