Git 에서 Branch 사용하기

황상일·2021년 1월 2일
0

github 알아보기

목록 보기
3/12

이전 게시글에서 Git의 기본기능인 Commit을 배웠으니, 여럿이서 협업을 할 때 매우 중요한 Branch를 배워보자

브랜치(Branch)

브랜치 '나무가지'란 뜻으로, 레포지터리의 복사본을 만들어 작업을 하는데 사용된다.
master에서 여러 기능을 동시에 작업을 하면, 작업중의 다른 협업자들의 커밋에 의해 소스코드가 충돌이 날 수 있다.
그렇기 때문에 보통 작업을 분배하고, 각자 브렌치를 만들어 작업 후 검토를 하여 Merge(master로 다시 합치는 작업)를 한다.
이렇게 되면 커밋 로그 관리도 보기 쉽게 되며, 의도치 않은 코드 충돌을 피할 수 있다.

브랜치 사용해 보기

1.브랜치 생성하기

git branch
* master

git branch newbranch

>git branch
* master
  newbranch

git branch <branch name>을 사용하면 위와같이 브랜치를 생성할 수 있다.

2.브랜치 전환하기

git branch
* master
  newbranch

git checkout newbranch
M       MyFirstCommit.txt
Switched to branch 'newbranch'

git branch
  master
* newbranch

git checkout <branch name>을 사용하면 위와같이 작업 브랜치를 전환할 수 있다.

3.브랜치 병합하기

git add MyFirstCommit.txt -p
diff --git a/MyFirstCommit.txt b/MyFirstCommit.txt
index a701303..80638aa 100644
--- a/MyFirstCommit.txt
+++ b/MyFirstCommit.txt
@@ -1 +1,2 @@
-Hello Git ~~
\ No newline at end of file
+Hello Git ~~!!
+branch test
\ No newline at end of file
Stage this hunk [y,n,q,a,d,/,e,?]? y

git commit -m "branch test"
[newbranch 969e8c1] branch test
 1 file changed, 2 insertions(+), 1 deletion(-)

git checkout master
Switched to branch 'master'

git merge newbranch
Updating fdbc838..969e8c1
Fast-forward
 MyFirstCommit.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

commit을 한 후 다시 master브렌치로 체크아웃 한뒤
git merge <branch name>를 하면 위와같이 작업내용을 master에 merge할 수 있다.

4.브랜치 삭제하기

git branch
* master
 newbranch

git branch -d newbranch
Deleted branch newbranch (was 969e8c1).

git branch
* master

merge한 후 branch가 필요없어졌으니 지울땐 git branch -d <branch Name>을 하면 된다.

정리하기

브랜치 생성
git branch <branch name>
브랜치 전환
git checkout <branch name>
브랜치 병합
git merge <branch name>
브랜치 삭제
git branch -d <branch Name>

profile
받은만큼 나눠주자

0개의 댓글