Git - 6. 브랜치와 충돌

BANG·2025년 9월 4일

git

목록 보기
6/7

초기 테스트 환경

$ git init
Initialized empty Git repository in C:/Work/gittest/manual/.git/

$ vi work.txt

$ git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        work.txt

nothing added to commit but untracked files present (use "git add" to track)


$ git add work.txt
warning: in the working copy of 'work.txt', LF will be replaced by CRLF the next time Git touches it


$ git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   work.txt


$ git commit -m "work 1"
[main (root-commit) fc0ce39] work 1
 Committer: unknown <1111@~~~~~.com>
 1 file changed, 1 insertion(+)
 create mode 100644 work.txt


$ git status
On branch main
nothing to commit, working tree clean


$ git log
commit fc0ce3920568da5dbe48e739a93e39f76ed6b721 (HEAD -> main)
Author: unknown <1111@~~~~~.com>
Date:   Thu Sep 4 13:43:22 2025 +0900

    work 1
    
$ vi work.txt
$ git commit -am "work 2"

$ vi work.txt
$ git commit -am "work 3"


$ git log
commit 8fffef07757c13e9b6162c4db95cf351e31abd78 (HEAD -> main)
Author: unknown <1111@~~~~~.com>
Date:   Thu Sep 4 13:46:07 2025 +0900

    work 3

commit f979690f1fc70b636b2a7818ccb54bce90e90347
Author: unknown <1111@~~~~~.com>
Date:   Thu Sep 4 13:45:51 2025 +0900

    work 2

commit fc0ce3920568da5dbe48e739a93e39f76ed6b721
Author: unknown <1111@~~~~~.com>
Date:   Thu Sep 4 13:43:22 2025 +0900

    work 1

브랜치

git branch

  • 브랜치의 목록 출력

git branch 생성할_브랜치명

  • 브랜치를 생성함
  • 마지막의 HEAD가 main을 가리키고 있음
    - 현재 작업환경이 main에 속해 있음
$ git branch
* main

$ git log --all --graph --oneline
* 8fffef0 (HEAD -> main) work 3
* f979690 work 2
* fc0ce39 work 1

$ git branch apple

$ git branch
  apple
* main

$ git log --all --graph --oneline
* 8fffef0 (HEAD -> main, apple) work 3
* f979690 work 2
* fc0ce39 work 1

$ git branch google
$ git branch ms

$ git log --all --graph --oneline
* 8fffef0 (HEAD -> main, ms, google, apple) work 3
* f979690 work 2
* fc0ce39 work 1

브랜치가 분리된 후 작업

  • 현재 위치한 브랜치에만 작업한 내용과 버전이 남겨짐
$ git branch
  apple
  google
* main
  ms

$ vi work.txt

$ git commit -am "main work4"
warning: in the working copy of 'work.txt', LF will be replaced by CRLF the next time Git touches it
[main ea81648] main work4
 Committer: unknown <1111@~~~~~.com>
 1 file changed, 1 insertion(+)


$ git log --all --graph --oneline
* ea81648 (HEAD -> main) main work4
* 8fffef0 (ms, google, apple) work 3
* f979690 work 2
* fc0ce39 work 1
  • 이미 분리된 브랜치에는 작업한 내용이 반영 안됨!!!
$ git checkout apple
Switched to branch 'apple'


$ git log --all --graph --oneline
* ea81648 (main) main work4
* 8fffef0 (HEAD -> apple, ms, google) work 3
* f979690 work 2
* fc0ce39 work 1
  • 다른 브랜치에서 작업
$ vi work.txt
$ vi apple.txt

$ git status
On branch apple
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   work.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        apple.txt

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


$ git add .
warning: in the working copy of 'apple.txt', LF will be replaced by CRLF the next time Git touches it


$ git commit -m "apple 4"
[apple 3255ae4] apple 4
 Committer: unknown <1111@~~~~~.com>
 2 files changed, 2 insertions(+)
 create mode 100644 apple.txt


$ git log --all --graph --oneline
* 3255ae4 (HEAD -> apple) apple 4
| * ea81648 (main) main work4
|/
* 8fffef0 (ms, google) work 3
* f979690 work 2
* fc0ce39 work 1
  • apple 4와 main work4의 부모는 work 3
$ git checkout google
Switched to branch 'google'

$ vi work.txt
$ vi google.txt

$ git status
On branch google
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   work.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        google.txt

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


$ git add .
warning: in the working copy of 'google.txt', LF will be replaced by CRLF the next time Git touches it


$ git commit -m "google 4"
[google bd79425] google 4
 Committer: unknown <1111@~~~~~.com>
 2 files changed, 2 insertions(+)
 create mode 100644 google.txt

$ git log --all --graph --oneline
* bd79425 (HEAD -> google) google 4
| * 3255ae4 (apple) apple 4
|/
| * ea81648 (main) main work4
|/
* 8fffef0 (ms) work 3
* f979690 work 2
* fc0ce39 work 1



$ git checkout ms
Switched to branch 'ms'

$ vi work.txt
$ vi ms.txt

$ git add .
warning: in the working copy of 'ms.txt', LF will be replaced by CRLF the next time Git touches it

$ git commit -m "ms 4"
[ms bc5c1d8] ms 4
 Committer: unknown <1111@~~~~~.com>
 2 files changed, 2 insertions(+)
 create mode 100644 ms.txt



$ git log --all --graph --oneline
* bc5c1d8 (HEAD -> ms) ms 4
| * bd79425 (google) google 4
|/
| * 3255ae4 (apple) apple 4
|/
| * ea81648 (main) main work4
|/
* 8fffef0 work 3
* f979690 work 2
* fc0ce39 work 1
  • apple 4와 google 4, ms4, main work4의 부모는 work 3

병합(merge)

  • apple4 와 main work4 의 부모는 work3
  • work3을 base라고 부름

테스트 환경 작업

$ git init
$ vi work.txt
$ git add work.txt
$ git commit -m "work 1"

$ git branch o2

$ vi main.txt
$ git add main.txt
$ git commit -m "work 2"

$ git comit --amend # 커밋 메시지 변경

$ git log --all --graph --oneline
* 02db800 (HEAD -> main) main work 2
* 29f16f6 (o2) work 1



$ git checkout o2
Switched to branch 'o2'

$ vi o2.txt

$ git add o2.txt
warning: in the working copy of 'o2.txt', LF will be replaced by CRLF the next time Git touches it

$ git commit -m "o2 work 2"
[o2 ed63cf9] o2 work 2
 Committer: unknown <1111@~~~~~.com>
 1 file changed, 1 insertion(+)
 create mode 100644 o2.txt


$ git log --all --graph --oneline
* ed63cf9 (HEAD -> o2) o2 work 2
| * 02db800 (main) main work 2
|/
* 29f16f6 work 1

서로 다른 파일 병합

git merge

  • main 브랜치에 o2 브랜치를 병합
  • o2의 내용을 main로 합치기
  • 근본은 main <- main 브랜치에서 병합 실행해야 함!!!!!
  • o2의 최신 커밋과 main의 이전 커밋을 공통의 조상으로 하는 커밋 생성
$ git checkout main
Switched to branch 'main'


$ git merge o2
Merge made by the 'ort' strategy.
 o2.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 o2.txt


$ git log --all --graph --oneline
*   d15c909 (HEAD -> main) Merge branch 'o2'
|\
| * ed63cf9 (o2) o2 work 2
* | 02db800 main work 2
|/
  • 원상복구하고 다시 해보기
$ git reset --hard 02db800
HEAD is now at 02db800 main work 2


$ git log --all --graph --oneline
* ed63cf9 (o2) o2 work 2
| * 02db800 (HEAD -> main) main work 2
|/
* 29f16f6 work 1

같은 파일, 다른 부분 병합

$ git init
Initialized empty Git repository in C:/Work/gittest/manual-merge/.git/

$ vi work.txt

$ git add work.txt
warning: in the working copy of 'work.txt', LF will be replaced by CRLF the next time Git touches it

$ git commit -m "1"
[main (root-commit) 9236c91] 1
 Committer: unknown <1111@~~~~~.com>
 1 file changed, 6 insertions(+)
 create mode 100644 work.txt

$ git branch o2

$ vi work.txt
$ git commit -am "master work2"
[main e777cec] master work2
 Committer: unknown <1111@~~~~~.com>
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git checkout o2
Switched to branch 'o2'

$ vi work.txt

$ git commit -am "o2 work2"
[o2 ea8f0fe] o2 work2
 Committer: unknown <1111@~~~~~.com>
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git log --all --graph --oneline
* ea8f0fe (HEAD -> o2) o2 work2
| * e777cec (main) master work2
|/
* 9236c91 1




$ git checkout main
Switched to branch 'main'


$ git merge o2
Auto-merging work.txt
Merge made by the 'ort' strategy.
 work.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)


$ git log --all --graph --oneline
*   3b41786 (HEAD -> main) Merge branch 'o2'
|\
| * ea8f0fe (o2) o2 work2
* | e777cec master work2
|/
* 9236c91 1

같은 파일, 같은 부분 병합

$ git init
Initialized empty Git repository in C:/Work/gittest/manual-merge/.git/

$ vi work.txt

$ git add work.txt
warning: in the working copy of 'work.txt', LF will be replaced by CRLF the next time Git touches it

$ git commit -m "1"
[main (root-commit) 9236c91] 1
 Committer: unknown <1111@~~~~~.com>
 1 file changed, 6 insertions(+)
 create mode 100644 work.txt

$ git branch o2

$ vi work.txt
$ git commit -am "master work2"
[main e777cec] master work2
 Committer: unknown <1111@~~~~~.com>
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git checkout o2
Switched to branch 'o2'

$ vi work.txt

$ git commit -am "o2 work2"
[o2 ea8f0fe] o2 work2
 Committer: unknown <1111@~~~~~.com>
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git log --all --graph --oneline
* ea8f0fe (HEAD -> o2) o2 work2
| * e777cec (main) master work2
|/
* 9236c91 1




$ git checkout main
Switched to branch 'main'

$ git merge o2
Auto-merging work.txt
CONFLICT (content): Merge conflict in work.txt
Automatic merge failed; fix conflicts and then commit the result.

$ git status
On branch main
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:   work.txt

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


$ vi work.txt
#title
content
<<<<<<< HEAD		# <---- 현재 브랜치(HEAD)의 내용이란 표시
main
=======				# <---- 구분자
o2
>>>>>>> o2			# <---- o2 브랜치의 내용이라는 표시
#title
content

# 아래 처럼 변경 (구분자들도 같이 삭제 필요!!!!!!!)

#title
content
main - o2
#title
content



$ git add work.txt	# 충돌 해결했다~!


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

Changes to be committed:
        modified:   work.txt


$ git commit
[main c52b052] Merge branch 'o2'
 Committer: unknown <1111@~~~~~.com>
 
 
$ git log --all --graph --oneline
*   c52b052 (HEAD -> main) Merge branch 'o2'
|\
| * 773c5fb (o2) o2 2
* | 9d19893 main 2
|/
* c3c11ba 1

cherry-pick

  • merge는 현재 브랜치와 merge할 브랜치의 최신 브랜치를 병합하는 것
  • cherry-pick은 현재 브랜치와 merge할 브랜치의 특정 버전(커밋)을 병합하는 것
profile
Record Everything!!

0개의 댓글