# merge
- 합치다. 병합하다의 뜻.
- 서로다른 브랜치의 커밋을 합치는 것을 의미
# 병합하기 (case 1. 파일명이 다를 때)
82109@DESKTOP-OO924JS MINGW64 ~
$ pwd
/c/Users/82109
82109@DESKTOP-OO924JS MINGW64 ~
$ cd git-test
82109@DESKTOP-OO924JS MINGW64 ~/git-test
$ mkdir git-merge
82109@DESKTOP-OO924JS MINGW64 ~/git-test
$ cd git-merge
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge
$ git init
Initialized empty Git repository in C:/Users/82109/git-test/git-merge/.git/
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (master)
$ nano work.txt
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (master)
$ git add work.txt
warning: LF will be replaced by CRLF in work.txt.
The file will have its original line endings in your working directory
g
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (master)
$ git commit -m "work 1"
[master (root-commit) 30c898b] work 1
1 file changed, 1 insertion(+)
create mode 100644 work.txt
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (master)
$ git log --all --graph --oneline
* 30c898b (HEAD -> master) work 1
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (master)
$ git branch o2
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (master)
$ git log --all --graph --oneline
* 30c898b (HEAD -> master, o2) work 1
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (master)
$ nano master.txt
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (master)
$ git add master.txt
warning: LF will be replaced by CRLF in master.txt.
The file will have its original line endings in your working directory
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (master)
$ git commit -m "work 2"
[master 3971251] work 2
1 file changed, 1 insertion(+)
create mode 100644 master.txt
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (master)
$ git commit --amend
[master 58f905c] master work 2
Date: Sun Dec 12 15:32:51 2021 +0900
1 file changed, 1 insertion(+)
create mode 100644 master.txt
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (master)
$ git checkout o2
Switched to branch 'o2'
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (o2)
$ nano o2.txt
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (o2)
$ git add o2.txt
warning: LF will be replaced by CRLF in o2.txt.
The file will have its original line endings in your working directory
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (o2)
$ git commit -m "o2 2"
[o2 e95b85f] o2 2
1 file changed, 1 insertion(+)
create mode 100644 o2.txt
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (o2)
$ git log --all --graph --oneline
* e95b85f (HEAD -> o2) o2 2
| * 58f905c (master) master work 2
|/
* 30c898b work 1
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (o2)
$ git checkout master
Switched to branch 'master'
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (master)
$ git merge o2
Merge made by the 'ort' strategy.
o2.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 o2.txt
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (master)
$ git log --all --graph --oneline
* 55fdd14 (HEAD -> master) Merge branch 'o2'
|\
| * e95b85f (o2) o2 2
* | 58f905c master work 2
|/
* 30c898b work 1
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (master)
$ git reset --hard e95b85f
HEAD is now at e95b85f o2 2
82109@DESKTOP-OO924JS MINGW64 ~/git-test/git-merge (master)
$ git log --all --graph --oneline
* e95b85f (HEAD -> master, o2) o2 2
* 30c898b work 1
1) git commit --amend
- --amend : 방금전의 커밋 메세지를 수정 할 수있다.
2) git checkout o2
- "o2"브랜치로 이동
3) 머지 하는 방법
상황 ) master 에 o2브랜치를 병합하고싶음.
1) master브랜치 상태가 되어야함
git checkout master
2) 현재 브랜치로 병합하고싶은 브랜치를 땡겨옴
git merge o2
4) 머지 이전으로 돌리고 싶다면? - reset 이용
- git reset --hard + 버전 커밋 번호
- 해당 커밋시점으로 리셋
# 병합하기 (case 2. 파일명이 같은 파일 수정 / 수정한 영역이 다를 때)
82109@DESKTOP-OO924JS MINGW64 ~
$ cd git-test
82109@DESKTOP-OO924JS MINGW64 ~/git-test
$ mkdir manual-merge
82109@DESKTOP-OO924JS MINGW64 ~/git-test
$ cd manual-merge
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge
$ git init
Initialized empty Git repository in C:/Users/82109/git-test/manual-merge/.git/
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ nano work.txt
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git add work.txt
warning: LF will be replaced by CRLF in work.txt.
The file will have its original line endings in your working directory
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git commit -m "work 1"
[master (root-commit) 7fc81ba] work 1
1 file changed, 5 insertions(+)
create mode 100644 work.txt
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git log --all --graph --oneline
* 7fc81ba (HEAD -> master) work 1
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git branck o2
git: 'branck' is not a git command. See 'git --help'.
The most similar command is
branch
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git log --all --graph --oneline
* 7fc81ba (HEAD -> master) work 1
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git branch o2
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git log --all --graph --oneline
* 7fc81ba (HEAD -> master, o2) work 1
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ nano work.txt
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git commit -am "master work 2"
warning: LF will be replaced by CRLF in work.txt.
The file will have its original line endings in your working directory
[master 7f83f73] master work 2
1 file changed, 1 insertion(+), 1 deletion(-)
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git checkout o2
Switched to branch 'o2'
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (o2)
$ nano work.txt
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (o2)
$ git log --all --graph --oneline
* 7f83f73 (master) master work 2
* 7fc81ba (HEAD -> o2) work 1
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (o2)
$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
work.txt
Please commit your changes or stash them before you switch branches.
Aborting
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (o2)
$ git commit -am "o2 work 2"
[o2 bbe2604] o2 work 2
1 file changed, 1 insertion(+), 1 deletion(-)
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (o2)
$ git log --all --graph --oneline
* bbe2604 (HEAD -> o2) o2 work 2
| * 7f83f73 (master) master work 2
|/
* 7fc81ba work 1
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (o2)
$ git checkout master
Switched to branch 'master'
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git merge o2
Auto-merging work.txt
Merge made by the 'ort' strategy.
work.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git log --all --graph --oneline
* 608ff77 (HEAD -> master) Merge branch 'o2'
|\
| * bbe2604 (o2) o2 work 2
* | 7f83f73 master work 2
|/
* 7fc81ba work 1
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git log -p
commit 608ff777e3657c7bbea997e505d9c7636dbedb20 (HEAD -> master)
Merge: 7f83f73 bbe2604
Author: Jang.hyunjeong <jang.hyunjeong01@gmail.com>
Date: Sun Dec 12 15:56:55 2021 +0900
Merge branch 'o2'
commit bbe2604031ae4ebd164164c3fe06fed0c6f14ae3 (o2)
Author: Jang.hyunjeong <jang.hyunjeong01@gmail.com>
Date: Sun Dec 12 15:56:18 2021 +0900
o2 work 2
diff --git a/work.txt b/work.txt
index b05b90b..af69029 100644
--- a/work.txt
+++ b/work.txt
@@ -2,4 +2,4 @@
content
# title 2
-content
+o2 content
- 결론 : 파일이 같아도, 수정된 영역 다르다면 git은 알아서 수정해준다.
# 병합하기 (case 3. 파일명이 같은 파일 수정 / 수정한 영역이 같을 때)
82109@DESKTOP-OO924JS MINGW64 ~
$ cd git-test
82109@DESKTOP-OO924JS MINGW64 ~/git-test
$ mkdir manual-merge
82109@DESKTOP-OO924JS MINGW64 ~/git-test
$ cd manual-merge
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge
$ git init
Initialized empty Git repository in C:/Users/82109/git-test/manual-merge/.git/
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ nano work.txt
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git add "work.txt"
warning: LF will be replaced by CRLF in work.txt.
The file will have its original line endings in your working directory
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git commit -m "work 1"
[master (root-commit) 286c0c3] work 1
1 file changed, 6 insertions(+)
create mode 100644 work.txt
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git log -p
commit 286c0c3e462f694241f786053c234aee74787917 (HEAD -> master)
Author: Jang.hyunjeong <jang.hyunjeong01@gmail.com>
Date: Sun Dec 12 16:20:34 2021 +0900
work 1
diff --git a/work.txt b/work.txt
new file mode 100644
index 0000000..0e9ce07
--- /dev/null
+++ b/work.txt
@@ -0,0 +1,6 @@
+#title
+content
+
+#title
+content
+
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git status
On branch master
nothing to commit, working tree clean
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git branch o2
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git log --all --graph --oneline
* 286c0c3 (HEAD -> master, o2) work 1
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ nano work.txt
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git commit -am "master work 2"
warning: LF will be replaced by CRLF in work.txt.
The file will have its original line endings in your working directory
[master 8d26def] master work 2
1 file changed, 1 insertion(+), 1 deletion(-)
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git checkout o2
Switched to branch 'o2'
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (o2)
$ nano work.txt
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (o2)
$ git commit -am "o2 work 2"
[o2 cbea912] o2 work 2
1 file changed, 1 insertion(+), 1 deletion(-)
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (o2)
$ git log --all --graph --oneline
* cbea912 (HEAD -> o2) o2 work 2
| * 8d26def (master) master work 2
|/
* 286c0c3 work 1
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (o2)
$ git checkout master
Switched to branch 'master'
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git merge o2
Auto-merging work.txt
CONFLICT (content): Merge conflict in work.txt
Automatic merge failed; fix conflicts and then commit the result.
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master|MERGING)
$ nano work.txt
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master|MERGING)
$ git commit
error: Committing is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
U work.txt
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master|MERGING)
$ git add work.txt
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master|MERGING)
$ git commit
[master fa161dc] Merge branch 'o2'
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git status
On branch master
nothing to commit, working tree clean
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git log --all --graph --oneline
* fa161dc (HEAD -> master) Merge branch 'o2'
|\
| * cbea912 (o2) o2 work 2
* | 8d26def master work 2
|/
* 286c0c3 work 1
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
1) 같은 파일에 같은 영역을 수정한 뒤 머지를 하면 깃을 충돌 된 내역을 알려준다.
82109@DESKTOP-OO924JS MINGW64 ~/git-test/manual-merge (master)
$ git merge o2
Auto-merging work.txt
CONFLICT (content): Merge conflict in work.txt
Automatic merge failed; fix conflicts and then commit the result.
--> 이를 해결하기위해서는 해당 파일로 들어가, 겹치는 영역을 수정해주어야한다.
** 충돌된 내역은 ==== 을 구분선으로 표시해준다.
#title
content
<<<<<<< HEAD
master
=======
o2
>>>>>>> o2
#title
content
** 필요한 내용만 남기고, 아래와 같이 수정
#title
content
master , o2
#title
content
2) 충돌된 파일을 해결했다면, git commit ! 혹시 add하라고 뜬다면 수정된 파일을 git add 한 뒤 커밋하면 된다!
그럼 잘 머지 되었고, 일전의 충돌도 해결되었다고 나온다.
'Note > 생활코딩 GIT' 카테고리의 다른 글
[생활코딩_GIT] 04. CLI Backup - #1. backup , git hosting (0) | 2021.12.15 |
---|---|
[생활코딩_GIT] 03. CLI Branch & conflict - #3 3way merge (0) | 2021.12.14 |
[생활코딩_GIT] 03. CLI Branch & conflict - #1 branch 만들기 / branch 간의 이동 (0) | 2021.12.12 |
생활코딩_GIT_02. CLI 버전관리 - #4 git 버전 이동/ 버전 삭제 / 버전 되돌리기 (0) | 2021.12.10 |
생활코딩_GIT_02. CLI 버전관리 - #3 버전만들기 (0) | 2021.12.10 |