독립적으로 어떤 작업을 진행하기 위한 개념
각각의 Branch는 다른 Branch의 영향을 받지 않음
메인 Branch: 배포할 수 있는 수준의 안정적인 Branch
토픽 Branch: 기능 추가나 버그 수정과 같은 단위 작업을 위한 Branch
$ git branch like_feature
현재의 Branch는 아래의 명령어를 통해 확인할 수 있습니다
$ git branch
like_feature
* master
Branch 전환은 아래의 명령어를 통해 할 수 있습니다.
$ git checkout like_feture
Switched to branch 'like_feture'
checkout은 branch를 전환하는데 사용할 수 도 있고
아래와 같이 git log
로 확인한 snapshot을 넘나들때도 사용이 가능합니다
git checkout <snapshot hash>
$ git log --pretty=oneline
e4abb6f... (HEAD -> master) this is master
d97d387... another snapshot
$ git checkout d97d38
...
HEAD is now at d97d38 another snapshot
$ git log --pretty=oneline
e4abb6f... (master) this is master
d97d387... (HEAD) another snapshot
이렇게 snapshot의 hash값을 이용하여 과거의 파일 내용을 확인 할 수 있습니다.
like_feature
Branch의 working directory에서 새로운 정보를 넣어 commit해보겠습니다
먼저 like_feature
로 위치를 이동시켜볼까요?
git checkout like_feature
like_feature
Branch에서의 작업을 끝마치고, master
Branch로 통합합니다
먼저 master
Branch로 이동하여 like_feature
Branch를 병합합니다
$ git checkout master
Switched to branch 'master'
$ git merge like_feature
Updating d78ade4..a63hec2
Fast-forward
comment.js | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
like_feature
Branch의 내용이 masteer
Branch에서 업데이트 된 내용이기 때문에 곧바로 merge가 되는 것을 확인 할 수 있습니다
이렇게 merge가 이루어지는 것을 fast-forward라고 부릅니다
이번에는 각각의 Branch의 working directory에서 같은 파일의 내용을 다르게 수정해보겠습니다
명심하세요! 각각의 Branch는 다른 Branch의 영향을 받지 않기 때문에, 여러 작업을 동시에 진행 할 수 있습니다.
git log --graph --all
을 사용하면 commit graph를 확인할 수 있습니다.
추가로 옵션을 주어서 더 깔끔하게 볼 수도 있겠죠?
$ git log --pretty=online --grpah --all
* c3360a... (HEAD->master) add master
| *782d90...(like_feture) add like_feature
| /
* 1c7881... init git
git checkout master
을 이용하여 master로 checkout한 후 git merge like_feature
로 merge 해보겠습니다
$ git checkout master
switched to branch 'master'
$ git merge like_feature
[main 2019-07-31T08:58:15.648Z] update#setState idle
Merge made by the 'recursive' strategy.
checkout.txt | 2+-
1 file changed, 1 insertion(+), 1 deletion(-)
아래의 명령어는 Merge된 Branch를 볼 수 있습니다
$ git branch --merged
like_feature
* master
사용을 마친 Branch는 git branch -d <branch name>
을 이용하여 삭제할 수 있습니다
$ git branch -d like_feature
Delect branch like_feature (was 782d900).
$ git log --grqph --pretty=oneline --all
* 3bf1a8.. merging(HEAD -> master)
|\
| * 782d90... add like_feature
* | c3360a... add master
|/
* 1c7881 init git
Merge한 두 Branch에서 같은 파일을 변경했을 때 충돌이 발생합니다
...
$(".comment-good").text($(".comment-good").val()-1);
...
...
$(".comment-good").text($(".comment-good").val()-1 < 0 ? 0 :
$(".comment-good").val()-1);
...
git merge like_features
명령을 수행했을 때 아래와 같이 충돌이 발생했습니다.
$ git merge like_feature
Auto-merging comment.js
CONFLICT (content): Merge conflict in comment.js
Automatic merge failed; fix conflicts and then commit the result.
git status
명령어로 어느 파일에서 충돌이 발생했는지 확인합니다.
$ git status
On bransh master
You have unmerged paths.
(fix conflicts and run "git commit")
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: comment.js
no changes added to commit (use "git add" and/or "git commit -a")
충돌이 일어난 comment.js
파일을 열어봅니다.
<<<<<<< HEAD
$(".comment-good").text($(".comment-good").val()-1);
=======
$(".comment-good").text($(".comment-good").val()-1 < 0 ? 0 :
$(".comment-good").val()-1);
>>>>>>> like_feature
수정 완료 후, '<<<<<<<','=======','>>>>>>>'가 포함된 행을 삭제해줍니다.
$(".comment-good").text($(".comment-good").val()-1 < 0 ? 0 :
$(".comment-good").val()-1);
수정 완료 후, git add
, git commit
과정을 거쳐 다시 Merge 해줍니다.
$(".comment-good").text($(".comment-good").val()-1 < 0 ? 0 :
$(".comment-good").val()-1);
$ git merge like_feature
[like_feture a63hec2] Merge branch 'master' into like_feature
master
Branch의 변화를 지속적으로 가져와서 충돌이 발생하는 부분을 제거