git -basic of branch and merge

sihwan_e·2021년 5월 11일
0

git

목록 보기
1/1

링크텍스트

git branch
main

작업 중 문제가 생겨 hotfix 하기위해

git branch hotfix_1
git checkout hotfix_1 

이 때 git checkout -b hotfix_1
를 통해 브랜치를 만들면서 체크아웃까지 한번에 할수 있다.

		master
(c0) <- (c1) <- (c2)
		hotfix_1

그리고 hotfix_1에서 수정을 하고 커밋을하게 되면 hotfix_1 브랜치가 앞으로 나아간다

		master
(c0) <- (c1) <- (c2) <- (c3)
			hotfix_1

이제 다시 master branch 로 돌아가서, hotfix_1을 merge해준다

git checkout master
git merge hotfix_1
Updating 50cc27b..6967b8f
Fast-forward
 SQL_#2.text | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

c3은 c2커밋에 기반한 커밋이기 때문에 브랜치 포인터는 merge 과정 없이 바로 최신 커밋으로 이동한다.
이런 Merge 방식을 “Fast forward” 라고 부른다. 다시 말해 A 브랜치에서 다른 B 브랜치를 Merge 할 때 B 브랜치가 A 브랜치 이후의 커밋을 가리키고 있으면 그저 A 브랜치가 B 브랜치와 동일한 커밋을 가리키도록 이동시킬 뿐이다.

Merge 충돌이 일어났을 때 Git이 어떤 파일을 Merge 할 수 없었는지 살펴보려면 git status 명령을 이용한다.

$ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")

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

    both modified:      파일명

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

충돌이 일어난 파일은 unmerged 상태로 표시된다. Git은 충돌이 난 부분을 표준 형식에 따라 표시해준다. 그러면 개발자는 해당 부분을 수동으로 해결한다. 충돌을 해결하려면 위쪽이나 아래쪽 내용 중에서 고르거나 새로 작성하여 Merge 한다.

git status 명령으로 충돌이 해결된 상태인지 다시 한번 확인해볼 수 있다.

$ git status
On branch master
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)

Changes to be committed:

modified:   파일명

충돌을 해결하고 나서 해당 파일이 Staging Area에 저장됐는지 확인했으면 git commit 명령으로 Merge 한 것을 커밋한다. 충돌을 해결하고 Merge 할 때는 커밋 메시지가 아래와 같다.

Merge branch 'hotfix_1'
Conflicts:
    파일명
#
# 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.
# On branch master
# All conflicts fixed but you are still merging.
#
# Changes to be committed:
#	modified:   파일명
#

어떻게 충돌을 해결했고 좀 더 확인해야 하는 부분은 무엇이고 왜 그렇게 해결했는지에 대해서 자세하게 기록한다.

profile
Sometimes you gotta run before you can walk.

0개의 댓글