달나라 노트

Github - Branch 관리 (Branch 생성, 이동, 병합 삭제) 본문

Github

Github - Branch 관리 (Branch 생성, 이동, 병합 삭제)

CosmosProject 2021. 5. 12. 01:49
728x90
반응형

 

 

 

Github에서 나의 repo에 branch를 생성하면 마치 원본 파일을 2개로 복사해놓는 효과입니다.

 

Repository A에 있는 기본 branch인 master branch가 있으며,

여기에 추가로 test branch를 생성했다고 가정해봅시다.

 

test branch로 놓고 파일을 수정한 후 master branch로 가서 해당 파일을 보면 test branch의 수정내역이 master branch에선 보이지 않습니다.

test branch를 master branch로 병합하기 전까지는요.

 

 

 


 

상황을 가정해봅시다.

내가 어떤 프로그램을 작성하고있는데, 원본 파일에다가 마구 테스트를 하면 문제가 생길 수 있으니,

brach를 이용하려고 합니다.

 

branch를 생성하여 test branch에서 수정을 한 후 수정사항을 master branch에 merge하는 그런 흐름입니다.

 

그러면 위와같은 branch를 어떻게 관리할 수 있는지

그리고 어떠한 순서로 진행하는지

그리고 이 때 필요한 명령어들은 뭐가 있는지 알아봅시다.

 

 

 

먼저 branch를 생성해봅시다.

git branch test

git branch <생성할 branch 이름>

위 명령어로 test branch를 생성했습니다.

 

 

 

git branch

* master
  test

git branch 명령어로 brach list를 조회해보니 기존에 있던 master branch와 새로 생성한 test branch가 확인됩니다.

 

 

 

 

git checkout test

checkout 명령어를 이용하여 test branch로 이동합니다.

 

 

 

git branch

  master
* test

동일하게 git branch 명령어로 brach list를 조회해보니 현재 test branch로 설정되어있음을 알 수 있습니다.

 

 

 

 

 

vim program.py

test branch에서 program.py를 vim editor로 수정합니다.

(program.py는 master branch에서 이미 있던 파일입니다.)

 

 

 

 

 

git commit program.py

program.py를 test branch에서 commit해줍니다.

 

 

 

 

git checkout master

지금까지 한 후 다시 master branch로 가서 program.py를 보면 

test branch에서 수정한 내용이 master branch에선 확인되지 않습니다.

 

수정사항은 아직 test branch에서만 있기 때문이죠.

 

 

 

 

 

git checkout test

다시 test branch로 돌아가서

 

 

 

 

 

git merge test

merge 명령어를 이용하여 test branch의 수정사항을 master branch로 merge해줍니다.

 

위 merge가 성공하면 이제 master branch에서도 test branch에서 진행했던 수정된 program.py의 내용을 볼 수 있습니다.

 

 

 

 

git branch -d test

git branch -d <branch 이름>

위 명령어를 사용하면 -d 옵션이 branch를 삭제하라는 의미이므로 test branch를 삭제합니다.

 

보통 다 사용한 branch는 삭제하기 마련이니까요.

 

(만약 test branch에 더 수정사항이 있다면 남겨둬도 좋습니다.)

 

 

 

 

 


 

 

 

 

이전 예시에서는 test branch에서만 수정을 했습니다.

이번에는 master branch와 test branch모두에서 파일의 수정과 commit이 있지만 서로 충돌하는 commit이 아닌 경우를 봐봅시다.

 

 

 [Terminal] $ git branch
* main

 [Terminal] $ git branch test
 
 [Terminal] $ git branch
* main
  test

먼저 위처럼 test branch를 생성해줍시다.

 

 

 

 [Terminal] $ git checkout test
Switched to branch 'test'

 [Terminal] $ ls  
test.py		test_file_x.py	test_file_y.py

 [Terminal] $ vim test.py

checkout 명령어를 이용해 test branch로 이동한 후

test.py 파일을 vim editor로 수정해줍시다.

 

 

 [Terminal] $ git commit test.py

test branch에서 수정한 test.py를 commit합시다.

 

 

 

 [Terminal] $ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

 [Terminal] $ vim test_file_x.py
 
 [Terminal] $ git commit test_file_x.py

그리고 main branch로 돌아와서 commit을 발생시켜봅시다.

 

위처럼 test branch의 commit과 충돌하지 않도록

test branch에서 수정이 있던 test.py가 아닌 아예 다른 파일(test_file_x.py)을 수정하고 commit 하였습니다.

 

 

 

 

 [Terminal] $ git merge test

이제 git merge를 이용해 test branch를 main branch에 merge 해줍시다.

 

 

 

 

Merge branch 'test' into main
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.

git merge를 하면 위와같은 메세지가 뜹니다.

이건 commit merge 메세지입니다.

각 branch에 있는 서로 다른 commit을 main branch에 합치겠다는 뜻입니다.

 

서로 다른 commit이 main branch와 test branch에 있습니다.

main branch에선 test_file_x.py를 수정한 commit이고,

test branch에선 test.py를 수정한 commit이었죠.

 

이 두 commit은 서로 다른 파일에 대한 것이니 전혀 충돌할 가능성이 없겠죠?

 

따라서 git은 자동으로 두 branch에존재하는 commit을 합쳐줍니다.

 

위 화면에서 commit merge message를 확인하고 :wq를 입력하여 저장 후 나와줍니다.

 

 

 

 

 

 [Terminal] $ git merge test
Merge made by the 'recursive' strategy.
 test.py | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 test.py

그러면 위처럼 merge가 완료된 것을 볼 수 있습니다.

 

 

 

 

 

 [Terminal] $ git log --oneline --graph
*   9e5c798 (HEAD -> main) Merge branch 'test' into main
|\  
| * cb0d131 (test) edit in test branch
* | 66f8e28 edit in main branch
|/  
* 15177dd (origin/main, origin/HEAD) modified
* 05a1018 Create test_file_y.py
* b0ce84d Create test_file_x.py

git log 명령어를 통해 한번 어떤식으로 변화가된건지 그래프로 봐봅시다.

 

가장 왼쪽에 있는 일직선이 main branch이며 중간에 오른쪽으로 튀어나온게 test branch를 나타냅니다.

 

그리고 asterisk(*) 표시가 있는 곳이 각 commit이 발생한 단계라고 보시면 됩니다.

 

 

 

 [Terminal] $ git log --oneline --graph
*   9e5c798 (HEAD -> main) Merge branch 'test' into main
|\  
| * cb0d131 (test) edit in test branch
* | 66f8e28 edit in main branch

중요하게 보실건 위 부분입니다.

 

66f8e28이라는 commit은 commit message(edit in main branch)를 보면 main branch에서 test_file_x.py를 수정했던 commit이고,

cb0d131이라는 commit은 test branch에서 test.py 파일을 수정했던 commit입니다.

 

그리고 이것이 9e5c798에서 보이는 것과 같이 test branch를 main으로 merge했다는 메세지와 함께 하나의 길로 합쳐진걸 볼 수 있죠.

 

 

 

 

 

 

 


 

 

 

 

이번에도 main branch와 test branch에서 모두 commit을 발생시킬건데

한번 두 개의 commit이 충돌하도록 해봅시다.

 

 

 [Terminal] $ git branch
* main

 [Terminal] $ git branch test

 [Terminal] $ git checkout test
Switched to branch 'test'

 [Terminal] $ git branch
  main
* test

먼저 위처럼 test branch를 생성하고 test branch로 이동합시다.

 

 

 

 [Terminal] $ vim test.py
 
 [Terminal] $ git commit test.py

test branch에서 test.py 파일을 수정한 후 commit하였습니다.

 

 

 

 

 [Terminal] $ git checkout main

 [Terminal] $ git branch 
* main
  test
 
 [Terminal] $ vim test.py
 
 [Terminal] $ git commit test.py

이번엔 main branch로 돌아와서

test branch에서 수정했던 test.py를 수정한 후 commit합시다.

 

 

 

 [Terminal] $ git merge test
Auto-merging test.py
CONFLICT (content): Merge conflict in test.py
Automatic merge failed; fix conflicts and then commit the result.

이 상태에서 test branch를 main branch로 merge하려고하면 위와같이 conflict error가 발생합니다.

 

Merge conflict in test.py 부분을 보면 test.py 파일에서 충돌이 일어났음을 볼 수 있죠.

 

 

 

 [Terminal] $ vim test.py
 
 # This is test (editted)
<<<<<<< HEAD
edit in main branch
=======
# edit in test branch
>>>>>>> test

충돌이 있었던 test.py을 보면 파일 내용이 위처럼 내용이 바뀌어있습니다.

 

저는 test.py 파일에서

첫 번째 줄(# This is test (editted))은 건드리지 않고

두 번째 줄을 각각의 branch에서 서로 다르게 수정한 후 commit했었습니다.

즉, test.py의 두 번째 줄에서 main <-> test branch간의 충돌이 일어나겠죠.

 

따라서 위 내용을 보면

HEAD (main branch)에선 test.py파일의 두 번째 줄에 edit in main branch 라는 글자가 적혀있고,

test (test branch)에선 test.py파일의 두 번째 줄에 edit in test branch 라는 글자가 적혀있다는 뜻입니다.

(이렇게 git은 충돌이 일어난 파일에 자동으로 어떻게 충돌이 일어났는지에 대한 내용을 적어줍니다.)

 

 

 

 

 [Terminal] $ git status
On branch main
Your branch is ahead of 'origin/main' by 4 commits.
  (use "git push" to publish your local commits)

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   test.py

이 상태에서 git status를 보면 Unmerged paths라는 부분에 test.py가 적혀있죠.

 

여기서 중요한건 위처럼 충돌이 일어난 경우에는 이미 add를 해서 tracking 중인 파일이라고 해도 새로 add를 해줘야합니다.

 

 

 [Terminal] $ git add test.py
 
 [Terminal] $ git status
On branch main
Your branch is ahead of 'origin/main' by 4 commits.
  (use "git push" to publish your local commits)

All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:
	modified:   test.py

따라서 위처럼 test.py를 add해주면

Changed to be comitted 부분에 test.py가 modified로 나타난걸 볼 수 있죠.

 

 

 

 

 [Terminal] $ git commit

그리고 git commit을 입력하여 commit합시다.

 

Merge branch 'test' into main

# Conflicts:
#       test.py
#
# It looks like you may be committing a merge.
# If this is not correct, please remove the file
#       .git/MERGE_HEAD
# and try again.


# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Committer: 박세호 <robin@Robin-MacBook-Pro.local>
#
# On branch main
# Your branch is ahead of 'origin/main' by 4 commits.
#   (use "git push" to publish your local commits)
#
# All conflicts fixed but you are still merging.
#
# Changes to be committed:

그러면 위같이 자동으로 생성된 commit message가 뜨는데 :wq를 입력하고 저장 후 나와줍시다.

 

 

 

 

 

 [Terminal] $ git status
On branch main
Your branch is ahead of 'origin/main' by 6 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

git status로 확인해보면 더 이상 충돌이 난 merge라던가 unmerged paths 등이 안보이는 것을 알 수 있고,

merge가 잘 되었다고 볼 수 있죠.

 

 

 

 [Terminal] $ git log --oneline --graph
*   7d4a5d1 (HEAD -> main) Merge branch 'test' into main
|\  
| * 8a533f2 (test) commit in test branch
* | 2e762b0 edit in main branch
|/  
*   9e5c798 Merge branch 'test' into main
|\  
| * cb0d131 edit in test branch
* | 66f8e28 edit in main branch
|/  
* 15177dd (origin/main, origin/HEAD) modified
* 05a1018 Create test_file_y.py
* b0ce84d Create test_file_x.py

git log graph를 확인해봅시다.

 

 

 

 

 [Terminal] $ git log --oneline --graph
*   7d4a5d1 (HEAD -> main) Merge branch 'test' into main
|\  
| * 8a533f2 (test) commit in test branch
* | 2e762b0 edit in main branch

중요한건 위 부분입니다.

main branch에서 있었던 2e762b0 commit과

test branch에서 있었던 8a533f2 commit이

7d4a5d1로 merge된 것을 볼 수 있죠.

 

 

 

 [Terminal] $ vim test.py
 
 # This is test (editted)
<<<<<<< HEAD
edit in main branch
=======
# edit in test branch
>>>>>>> test

vim editor로 test.py를 보면 여전히 위처럼 git이 자동으로 생성해준

어디에서 충돌이 일어났는지에 대한 내용이 적혀있습니다.

 

이제 우리는 이 충돌이 일어난 내용을 보고 test.py를 직접 원하는 대로 수정해주면 됩니다.

 

이렇게 git merge에서 발생하는 충돌은 merge를 한 후에 직접 확인하여 파일을 수정해줘야 합니다.

 

 

 

 

 

 

 

 

 

 

 

 

728x90
반응형
Comments