Git, 분산 버전 관리 시스템 5장 브랜치 이해하고 활용하기 본문

Programming

Git, 분산 버전 관리 시스템 5장 브랜치 이해하고 활용하기

halatha 2010. 4. 20. 18:35
5.1 브랜치란

/mysite$ git branch -m master mymaster    #    마스터 브랜치명 변경
/mysite$ git branch    #    지역 저장소에 있는 모든 브랜치명 출력
  RB_1.0.1
* mymaster
/mysite$ git branch -m mymaster master
/mysite$ git branch
  RB_1.0.1
* master

5.2 새로운 브랜치 생성

/mysite$ git branch new    #    new branch 생성
/mysite$ git branch
  RB_1.0.1
* master
  new
/mysite$ git checkout new
Switched to branch 'new'
/mysite$ git branch
  RB_1.0.1
  master
* new

/mysite$ git checkout -b alternate master    #    master branch에서 alternate branch를 생성하고 동시에 checkout
Switched to a new branch 'alternate'

5.3 브랜치 간의 변경 사항 합치기

바로 합치기 Straight Merge

/mysite$ touch about.html
/mysite$ git add about.html    #    alternate branch에 about.html 추가
/mysite$ git commit -m "add the skeleton of an about page"
[alternate 9cba759] add the skeleton of an about page
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 about.html

/mysite$ git checkout master
Switched to branch 'master'
/mysite$ git merge alternate    #    master branch에 alternate branch를 merge
Updating a529f56..9cba759
Fast-forward
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 about.html

커밋 합치기 Squashed Commit: 많은 실험을 하는 경우 유용. 실험한 내용을 추적하지 않고 마지막 결과만을 사용하므로

/mysite$ git checkout -b contact master    #    contact branch에서 contact.html 더하고 commit
/mysite$ vi contact.html
/mysite$ git add contact.html
/mysite$ git commit -m "add contact file with email"
/mysite$ git commit -m "add secondary email" -a
[contact 7dbff08] add secondary email
 1 files changed, 1 insertions(+), 0 deletions(-)

/mysite$ git checkout master    #    master branch로 전환
Switched to branch 'master'
/mysite$ git merge --squash contact    #    squash를 지정해 git merge에서 지정한 브랜치의 모든 commit을 하나의 commit으로 넣음
Updating 9cba759..7dbff08
Fast-forward
Squash commit -- not updating HEAD
 contact.html |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 contact.html
/mysite$ git status    #    staging은 되었으나 아직 commit은 되지 않음
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   contact.html
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       mysite-1.0.tar.gz
#       mysite-1.0.zip
/mysite$ git commit -m "add contact page" \
> -m "has primary and secondary email"
[master 8545789] add contact page
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 contact.html

선택하여 합치기 Cherry-picking: 수정된 버그나 브랜치들 사이에 공유해야 하는 클래스를 추가하는 경우

/mysite$ git checkout contact
Switched to branch 'contact'
/mysite$ vi contact.html
/mysite$ git commit -m "add link to twitter" -a
[contact 8e77ee1] add link to twitter
 1 files changed, 1 insertions(+), 0 deletions(-)

/mysite$ git checkout master
Switched to branch 'master'
h7b256:/home1/irteam/work/jun/TEST/mysite$ git cherry-pick 8e77ee1    #    고유한 commit name을 이용해 cherry pick
Finished one cherry-pick.
[master 78851bb] add link to twitter
 1 files changed, 1 insertions(+), 0 deletions(-)

/mysite$ git reset --hard HEAD^    #    바로 이전 commit 제거
HEAD is now at 8545789 add contact page

/mysite$ git cherry-pick -n 8e77ee1    #    -n을 사용하면 Git을 합치기는 하지만 commit은 하지 않음
Finished one cherry-pick.
h7b256:/home1/irteam/work/jun/TEST/mysite$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   contact.html
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       mysite-1.0.tar.gz
#       mysite-1.0.zip

/mysite$ git commit    #    편집기를 이용해 message를 편집하는 commit
".git/COMMIT_EDITMSG" 15L, 406C written
[master eca1a2e] add link to twitter
 1 files changed, 1 insertions(+), 0 deletions(-)

5.4 충돌 다루기

/mysite$ git checkout -b about master
Switched to a new branch 'about'
/mysite$ vi about.html
/mysite$ git add about.html
/mysite$ git commit -m "a list of my favorite programming languages"
[about 003685a] a list of my favorite programming languages
 1 files changed, 13 insertions(+), 0 deletions(-)

/mysite$ git branch about2 about    #    about2 branch를 생성만 하고 아직 전환하지는 않음
/mysite$ vi about.html
/mysite$ git commit -m "add JavaScript to list" -a    #    about branch에는 존재하고 about2 branch에는 존재하지 않는 변경 사항 발생
[about f242be1] add JavaScript to list
 1 files changed, 1 insertions(+), 0 deletions(-)

/mysite$ git checkout about2
Switched to branch 'about2'
/mysite$ vi about.html
/mysite$ git commit -m "add ECMAScript to list" -a
[about2 35f3672] add ECMAScript to list
 1 files changed, 1 insertions(+), 0 deletions(-)

/mysite$ git checkout about
Switched to branch 'about'
/mysite$ git merge about2    #    merge를 하려고 하면 충돌 발생
Auto-merging about.html
CONFLICT (content): Merge conflict in about.html
Automatic merge failed; fix conflicts and then commit the result.

/mysite$ vi about.html    #    <<<<<<<< 먼저 commit한 내용, ======== 구분자, >>>>>>>>> about2 branch에 추가한 내용
 11 <<<<<<< HEAD
 12         <li>java script</li>
 13 =======
 14         <li>ECMA script</li>
 15 >>>>>>> about2

/mysite$ git mergetool
merge tool candidates: tortoisemerge emerge vimdiff
Merging the files: about.html

Normal merge conflict for 'about.html':
  {local}: modified
  {remote}: modified
Hit return to start merge resolution tool (emerge): vimdiff    #    vimdiff를 입력했는데 emacs가 떴음 -_-

/mysite$ git commit
[about a5eb34c] Merge branch 'about2' into about

5.5 브랜치 삭제하기

/mysite$ git branch -d about2
Deleted branch about2 (was 35f3672).

/mysite$ git checkout master
Switched to branch 'master'
h7b256:/home1/irteam/work/jun/TEST/mysite$ git branch -d about    #    브랜치 삭제 명령은 삭제하려는 브랜치가 성공적으로 현재 브랜치에 합쳐졌을 경우에만 동작
error: The branch 'about' is not fully merged.
If you are sure you want to delete it, run 'git branch -D about'.    #    합치지 않고 강제로 삭제하기를 원하면 -D option 사용

5.6 브랜치명 변경하기

/mysite$ git branch -m contact contacts
/mysite$ git branch
  RB_1.0.1
  about
  alternate
  contacts
* master
  new
/mysite$ git branch -m master contacts    #    이미 존재하는 이름을 사용하려는 경우. -M을 사용하면 강제로 덮어쓰므로 주의
fatal: A branch named 'contacts' already exists.

Comments