๊น(GIT) (5)- branch scenario, merge
branch scenario
1. branch ์ฌ์ฉ๋ฒ
๋ธ๋์น๋ฅผ ์กฐ์ํ ๋๋ ๋ฐ๋์
master
๋ธ๋์น์ ์ต์ 1๊ฐ์ ์ปค๋ฐ์ ์์ด์ผํ๋ค!!!
์ค์ต ์ค๋น
- ๋ฐ๋ผํด ๋ณธ๋ค.
# git ์ด๊ธฐํ ๋ฐ commit ๋จ๊ฒจ๋๊ธฐ
$ git init
$ touch a.txt
# a.txt text 1์ด๋ผ๋ ๊ธ์ ์์ฑํ๋ค.
$ git add .
$ git commit -m'text2'
[master 67f01f6] text2
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 b.txt
# 3๊ฐ์ ์ปค๋ฐ์ ๋ ๋จ๊ธด๋ค.
$ git log --oneline
b9aaaf5 (HEAD -> master) test3
67f01f6 text2
acf00f0 Initial commit
HEAD
-
https://stackoverflow.com/questions/2304087/what-is-head-in-git
- ํ์ฌ ์์นํ ๋ธ๋์น์ ์ต์ ์ปค๋ฐ
- HEAD๋ ํ์ฌ ๋ธ๋์น๋ฅผ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ
ex. ์ด์ ์ฐ๋ฆฌ๋ ๋ก๊ทธ์ธ ๊ธฐ๋ฅ์ ๊ฐ๋ฐํด๋ณธ๋ค๊ณ ๊ฐ์
$ git branch feature/login # ๋ธ๋์น ์์ฑ๋ง
$ git branch
feature/login
* master
master ๋ธ๋์น์์ ๋ง์ง๋ง ์ค ์ถ๊ฐ
text 1
text 2
text 3
master์์ ์์ฑํ text 4 # ์ถ๊ฐ
# add, commit
$ touch d.txt # master์์ d.txt๋ฅผ ์์ฑํ๋ค.
$ git add .
$ git commit -m'master text 4'
[master 121a503] master text 4
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 d.txt
# log
$ git log --oneline
121a503 (HEAD -> master) master text 4
b9aaaf5 (feature/login) test3
67f01f6 text2
acf00f0 Initial commit
๋ธ๋์น ์ด๋์ ํ๊ณ a.txt
๋ฅผ ํ์ธํด๋ณด์
$ git checkout feature/login
Switched to branch 'feature/login'
# master text 4๊ฐ ์ฌ๋ผ์ง
# log ํ์ธ
$ git log --oneline
b9aaaf5 (HEAD -> feature/login) test3
67f01f6 text2
acf00f0 Initial commit
# ํ์ฌ ๋ธ๋์น๋ feature/login
text 1
text 2
text 3
login text 4 # ์ถ๊ฐ
๋ธ๋์น๋ฅผ ๋ค๋ฃฐ ๋๋ ํ์ฌ ๋ด๊ฐ โ์ด๋คโ ๋ธ๋์น์ ์๋์ง๋ฅผ ๋ฐ๋์ ์์์ผ ํจ!!!
# login.txt
login text 4 # ์ถ๊ฐ
$ touch login.txt
$ git status
On branch feature/login
Untracked files:
(use "git add <file>..." to include in what will be committed)
login.txt # WD + commit ์ด๋ ฅ ์์(git์ด ์ง๊ธ๊น์ง ํ๋ฒ๋ ์ถ์ ํ์ ์์)
nothing added to commit but untracked files present (use "git add" to track)
# add, commit
$ git add .
$ git commit -m'login text4'
[feature/login f6dda89] login text4
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 login.txt
# ํ์ฌ ๋ธ๋์น๋? feature/login
$ git log --oneline
f6dda89 (HEAD -> feature/login) login text4
b9aaaf5 test3
67f01f6 text2
acf00f0 Initial commit
# ๋ค๋ฅธ ๋ธ๋์น์ ๊ทธ๋ํ๊น์ง ๋ณด๋ ๋ฐฉ๋ฒ
$ git log --all --graph --oneline
* f6dda89 (HEAD -> feature/login) login text4
| * 121a503 (master) master text 4
|/
* b9aaaf5 test3
* 67f01f6 text2
* acf00f0 Initial commit
2. branch merge scenario
-
๋๋ผ์ด๋ธ์ git-merge ํด๋ ์์ฑ ํ ํด๋น ํด๋์์ vs code๋ฅผ ์ด์!
-
ctrl + ๋ฐฑํฑ(1์ผ์ชฝ์ ์๋)
์ ๋๋ฅธ ํGit Bash
ํด๋ฆญ ํ ์์
$ git init
Initialized empty Git repository in F:/git-merge/.git/
$ touch test.txt
- test.txt์ master test 1์ด๋ผ๊ณ ์ฑ์๋ฃ๊ธฐ (์ ์ฅ ๊ผญ!!)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt
nothing added to commit but untracked files present (use "git add" to track)
$ git add .
$ git commit -m'master test 1'
[master (root-commit) 9fe5f83] master test 1
1 file changed, 1 insertion(+)
create mode 100644 test.txt
# log
$ git log
commit 9fe5f839afe5de4e366dc981f62f9692cce859ee (HEAD -> master)
Author: ingu627 <rjsdudans@naver.com>
Date: Sun Nov 21 19:41:06 2021 +0900
master test 1
3๊ฐ์ง ๋ณํฉ ์ํฉ
1. fast-forward
- ์ฃผ์ ์ฌํญ! ๋ฐ๋์ master ๋ธ๋์น์๋ 1๊ฐ ์ด์์ ์ปค๋ฐ์ด ์กด์ฌํด์ผ ํจ!!!
โ๋ค๋ฅธ ๋ธ๋์น๊ฐ ์์ฑ๋ ์ดํ์ master ๋ธ๋์น์ ๋ณ๊ฒฝ ์ฌํญ์ด ์๋ ์ํฉ => ๋จ์ํ๊ฒ master ๋ธ๋์น์ ํฌ์ธํฐ๋ฅผ ์ต์ commit์ผ๋ก ์ด๋ ์ํจ๋ค.โ
master ๋ธ๋์น์์ feature/login
๋ธ๋์น๋ฅผ ๋ณํฉ(merge) ํ ๋ feature/login
๋ธ๋์น๊ฐ master ๋ธ๋์น ์ดํ์ commit์ ๊ฐ๋ฆฌํค๊ณ ์๋ค๋ฉด master ๋ธ๋์น๋ feature/login
๋ธ๋์น๊ฐ ๊ฐ๋ฆฌํค๊ณ ์๋ ์ต์ commit์ผ๋ก ํฌ์ธํฐ(HEAD)๋ฅผ ์ด๋ ์ํค๋ฉด ๋๋ค.
1. feature/login ๋ธ๋์น ์์ฑ ํ ์ด๋
$ git checkout -b feature/login
Switched to a new branch 'feature/login'
2. ํน์ ํ ์์ ์ ์๋ฃํ๊ณ commit ์งํ
$ touch login.txt
$ git add .
$ git commit -m'login test 1'
[feature/login 6302c25] login test 1
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 login.txt
# log
$ git log --oneline
# master ๋ธ๋์น์์ feature/login ๋ธ๋์น ์์ฑ -> feature/login ๋ธ๋์น์์ ์๋ก์ด commit ์์ฑ
6302c25 (HEAD -> feature/login) login test 1
9fe5f83 (master) master test 1
3. master ๋ธ๋์น๋ก ์ด๋
- ๋ธ๋์น๋ฅผ ์ด๋ํ๋ ์๊ฐ login.txt ํ์ผ์ด ์ฌ๋ผ์ง๋ค.
- ์๋ํ๋ฉด login.txt ํ์ผ์ master ๋ธ๋์น๊ฐ ์๋๋ผ feature/login ๋ธ๋์น์์ commit(๋ฒ์ )์ ํ๊ธฐ ๋๋ฌธ์
$ git checkout master
Switched to branch 'master'
4. master ๋ธ๋์น์ feature/login ๋ธ๋์น์์ ์์ ํ ๊ฒฐ๊ณผ๋ฌผ ๋ณํฉ
- ๋ณํฉ ์ดํ์ login.txt ํ์ผ์ด ์๊น
- ๋ณํฉํ๊ณ ์ ํ๋ ๋ธ๋์น๋ก ์ด๋ํ ํ ์์
$ git merge feature/login
Updating 9fe5f83..6302c25
Fast-forward
login.txt | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 login.txt
5. ๋ก๊ทธ ํ์ธ
- HEAD -> master๊ฐ ์๋ก ์ฌ๋ผ์์!
# ๋ณํฉ ์
$ git log --oneline
# master ๋ธ๋์น์์ feature/login ๋ธ๋์น ์์ฑ -> feature/login ๋ธ๋์น์์ ์๋ก์ด commit ์์ฑ
6302c25 (HEAD -> feature/login) login test 1
9fe5f83 (master) master test 1
# ๋ณํฉ ํ
$ git log --oneline
6302c25 (HEAD -> master, feature/login) login test 1
9fe5f83 master test 1
6. ๋ธ๋์น ์ญ์
$ git branch -d feature/login
Deleted branch feature/login (was 6302c25).
# ํ์ธ
$ git branch
* master
$ git log --oneline
6302c25 (HEAD -> master) login test 1
9fe5f83 master test 1
2. Merge commit -> conflict x
๋ค๋ฅธ ๋ธ๋์น(feature/signup)๊ฐ ์์ฑ๋ ์ดํ์ master ๋ธ๋์น์ ๋ณ๊ฒฝ ์ฌํญ์ด ์๊ณ feature/signup ๋ธ๋์น์๋ ๋ณ๊ฒฝ ์ฌํญ์ด ์กด์ฌํ๋ ๊ฒฝ์ฐ -> ์ด๋ ์ถฉ๋ ์ํฉ์ด ๋ฐ์ํ์ง ์๋ ๊ฒฝ์ฐ
-
๋ค๋ฅธ ํ์ผ์ ๋ณ๊ฒฝ ์ฌํญ์ ์์ฑ(์์ ) commitํ ๊ฒฝ์ฐ
Merge commit
์ด ๋ฐ์ํจ- ์ด commit ๋ด์ญ์ ์ฐ๋ฆฌ๊ฐ ์ง์ ์์ฑํ commit์ด ์๋ git์ด ์๋์ผ๋ก ์์ฑํด์ค commit message
- ์ถฉ๋์ด ๋ฐ์ํ์ง ์๋ ์ด์ ๋ ๋ค๋ฅธ ํ์ผ (๊ฐ์ ํ์ผ์ด๋ผ๋ ๋ค๋ฅธ ๋ผ์ธ์ ์์ ํ ๊ฒฝ์ฐ ํฌํจ)์ด๊ธฐ ๋๋ฌธ์ ๋จ์ํ๊ฒ ํฉ์น๊ธฐ๋ง ํ๋ฉด ๋๊ธฐ ๋๋ฌธ!
1. signup ๋ธ๋์น ์์ฑ & ์ด๋
git checkout -b ๋ธ๋์น์ด๋ฆ
: ์ด๋ฆ๋ช ์ผ๋ก ์์ฑํ๊ณ ๊ทธ ๋ธ๋์น๋ก ์ด๋
$ git checkout -b feature/signup
Switched to a new branch 'feature/signup'
2. ํน์ ํ ์์ ์ ์งํํ๊ณ commit
$ touch signup.txt
# add, commit
$ git add .
$ git commit -m'signup test 1'
[feature/signup 586d298] signup test 1
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 signup.txt
# log
$ git log --oneline
586d298 (HEAD -> feature/signup) signup test 1 # feature/signup ๋ธ๋์น์ ์ต์ ์ปค๋ฐ + ํ์ฌ ๋ด๊ฐ ์์นํ ๋ธ๋์น
6302c25 (master) login test 1 # master ๋ธ๋์น์ ์ต์ ์ปค๋ฐ
9fe5f83 master test 1
3. master๋ก ์ด๋
- ์ด๋ํ๋ ์๊ฐ signup.txt ํ์ผ์ด ์ฌ๋ผ์ง
- signup ๋ธ๋์น์์ ํ์ผ์ ์์ฑํ๊ณ commit ํ๊ธฐ ๋๋ฌธ!
$ git checkout master
Switched to branch 'master'
$ git log --oneline
6302c25 (HEAD -> master) login test 1
9fe5f83 master test 1
4. master ๋ธ๋์น์์ ์ถ๊ฐ ์์ ์ ์งํํ ๋ค์ commit
$ touch master.txt
# add, commit
$ git add .
$ git commit -m'master test 1'
[master 2c9f3c0] master test 1
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 master.txt
# log
$ git log --oneline
2c9f3c0 (HEAD -> master) master test 1
6302c25 login test 1
9fe5f83 master test 1
# ๋ธ๋์น ํฌํจ ๋ชจ๋ log๋ฅผ ํ์ธํ ๋
$ git log --all --oneline
2c9f3c0 (HEAD -> master) master test 1
586d298 (feature/signup) signup test 1
6302c25 login test 1
9fe5f83 master test 1
5. master ๋ธ๋์น์ ๋ณํฉ
- merge๋ฅผ ์งํํ๊ณ ๋์ ํ์ธํด๋ณด๋ signup.txt๊ฐ ์๊ธฐ๋ ๊ฑธ ํ์ธํ ์ ์์
$ git merge feature/signup
Merge made by the 'recursive' strategy.
signup.txt | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 signup.txt
# log
$ git log --oneline
6e469f3 (HEAD -> master) Merge branch 'feature/signup' # ์ฐ๋ฆฌ๊ฐ ์ง์ ์
๋ ฅํ commit x -> Merge commit
2c9f3c0 master test 1
586d298 (feature/signup) signup test 1
6302c25 login test 1
9fe5f83 master test 1
6. log
- graph๋ก ๋ณด๊ณ ์ ํ ๋
$ git log --all --graph --oneline
* 6e469f3 (HEAD -> master) Merge branch 'feature/signup'
|\
| * 586d298 (feature/signup) signup test 1
* | 2c9f3c0 master test 1
|/
* 6302c25 login test 1
* 9fe5f83 master test 1
7. branch ์ญ์
$ git branch -d feature/signup
Deleted branch feature/signup (was 586d298).
$ git log --all --graph --oneline
* 6e469f3 (HEAD -> master) Merge branch 'feature/signup'
|\
| * 586d298 signup test 1
* | 2c9f3c0 master test 1
|/
* 6302c25 login test 1
* 9fe5f83 master test 1
3. Merge commit -> conflict o
๋ค๋ฅธ ๋ธ๋์น(feature/signup)๊ฐ ์์ฑ๋ ์ดํ์ master ๋ธ๋์น์ ๋ณ๊ฒฝ ์ฌํญ์ด ์๊ณ feature/signup ๋ธ๋์น์๋ ๋ณ๊ฒฝ ์ฌํญ์ด ์กด์ฌํ๋ ๊ฒฝ์ฐ -> ์ด๋ ์ถฉ๋ ์ํฉ์ด ๋ฐ์ํ๋ ๊ฒฝ์ฐ
-
๊ฐ์ ํ์ผ์ ๊ฐ์ ๋ถ๋ถ์ ์์ฑ(์์ )ํ์ฌ commitํ ๊ฒฝ์ฐ
Merge commit
์ด ๋ฐ์ํจ- ์ด commit ๋ด์ญ์ ์ฐ๋ฆฌ๊ฐ ์ง์ ์์ฑํ commit์ด ์๋ git์ด ์๋์ผ๋ก ์์ฑํด์ค commit message
- ์ถฉ๋์ด ๋ฐ์ํ๋ ์ด์ ๋ ๊ฐ์ ํ์ผ์ ๊ฐ์ ๋ถ๋ถ์ ์์ ํ๊ธฐ ๋๋ฌธ์ ์ด๋ค ๋ฒ์ ์ผ๋ก ๋ง๋ค์ง git์ ์ ์ ์๊ธฐ ๋๋ฌธ!
- ๊ทธ๋์ ์ง์ ์ฐ๋ฆฌ๊ฐ(==์ฌ๋์ด) ์์ ํด์ค์ผ ํ๋ค.
1. hotfix ๋ธ๋์น ์์ฑ ํ ์ด๋
$ git checkout -b hotfix
Switched to a new branch 'hotfix'
2. ํน์ ์์ ์๋ฃ ํ & commit ์งํ
# hotfix branch์ test.txt
master test 1
์ด๊ฑด hotfix ๋ธ๋์น์์ # ์ถ๊ฐ
์์ ํ์ต๋๋ค.
$ git add .
$ git commit -m'hotfix test 1'
[hotfix 03ba33b] hotfix test 1
1 file changed, 3 insertions(+), 1 deletion(-)
$ git log --oneline
03ba33b (HEAD -> hotfix) hotfix test 1
6e469f3 (master) Merge branch 'feature/signup'
2c9f3c0 master test 1
586d298 signup test 1
6302c25 login test 1
9fe5f83 master test 1
3. master๋ก ์ด๋
$ git checkout master
Switched to branch 'master'
4. ํน์ ์์ ํ commit
- hotfix ๋ธ๋์น์์ ์์ ํ ๋ถ๋ถ๊ณผ ๋์ผํ ๋ถ๋ถ์ ์์
# master branch์ test.txt
master test 1
์ด๊ฑด master ๋ธ๋์น์์ # ์ถ๊ฐ
์์ ํ ๋ด์ฉ์
๋๋ค.
$ git add .
$ git commit -m'master test 1'
[master b74a431] master test 1
1 file changed, 3 insertions(+), 1 deletion(-)
# log
$ git log --oneline
b74a431 (HEAD -> master) master test 1
6e469f3 Merge branch 'feature/signup'
2c9f3c0 master test 1
586d298 signup test 1
6302c25 login test 1
9fe5f83 master test 1
5. Merge
$ git merge hotfix
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt # ์ถฉ๋.. -> merge ํ๋ ๊ณผ์ ์์ ์ถฉ๋์ด ๋ฐ์ํจ -> test.txt
# ์๋์ผ๋ก ๋ณํฉํ๋ ๊ฒ ์คํจ!! -> ์ถฉ๋์ ํด๊ฒฐํ๊ณ ๋ ๋ค์์ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ commit ํ์
Automatic merge failed; fix conflicts and then commit the result.
-
๊ฐ์ ๋ถ๋ถ(๋ผ์ธ)์ ๋ค๋ฅธ ๋ธ๋์น์์ ๊ฐ๊ฐ ์์ ํ๊ณ commit ํ๊ธฐ ๋๋ฌธ์ ๋ณํฉํ๋ ๊ณผ์ ์์ git์ ์ด๋ค ๋ถ๋ถ์ ํฉ์ณ์ผ ํ๋์ง ์ ์ ์์ -> ๊ทธ๋์ ์ฐ๋ฆฌ๊ฐ ์ง์ ํฉ์น๊ณ ๋์ merge commit์ ๋ฐ์ ์์ผ์ผ ํ๋ค.
master test 1 <<<<<<< HEAD ์ด๊ฑด master ๋ธ๋์น์์ ์์ ํ ๋ด์ฉ์ ๋๋ค. ======= ์ด๊ฑด hotfix ๋ธ๋์น์์ ์์ ํ์ต๋๋ค. >>>>>>> hotfix
# ์์
master test 1
์ด๊ฑด master ๋ธ๋์น์์
์์ ํ ๋ด์ฉ์
๋๋ค.
์ด๊ฑด hotfix ๋ธ๋์น์์
์์ ํ์ต๋๋ค.
# master | MERGING -> Merge commit์ ๋จ๊ฒจ์ผ ํจ
$ git status
On branch master
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.txt # both modified
no changes added to commit (use "git add" and/or "git commit -a")
# add๋ก ์ฌ๋ฆฌ๊ณ commit์ผ๋ก Merge commit์ ์ง์ ๋จ๊ธด๋ค.
$ git add .
$ git commit -m "์ถฉ๋ ํด๊ฒฐ!!!"
[master 03c4743] ์ถฉ๋ ํด๊ฒฐ!!!
6. log ํ์ธ
- ์ถฉ๋์ด ๋ฐ์ํ์ง ์์ ๊ฒฝ์ฐ์๋
6e469f3 (HEAD -> master) Merge branch 'feature/signup'
์ด๋ฌํ commit์ git์ด ์๋์ผ๋ก ๋ง๋ค์ด์ฃผ์ง๋ง ์ถฉ๋์ ์ง์ ํด๊ฒฐํ๋ ๊ฒฝ์ฐ ์ฐ๋ฆฌ๊ฐ ์ถฉ๋์ ํด๊ฒฐํ๋ค๋ ์๋ฏธ์ ์ปค๋ฐ์ ์ง์ ๋จ๊ธธ ์ ์๋ค.
$ git log --oneline
03c4743 (HEAD -> master) ์ถฉ๋ ํด๊ฒฐ!!!
b74a431 master test 1
03ba33b (hotfix) hotfix test 1
6e469f3 Merge branch 'feature/signup'
2c9f3c0 master test 1
586d298 signup test 1
6302c25 login test 1
9fe5f83 master test 1
7. branch ์ญ์
$ git branch -d hotfix
Deleted branch hotfix (was 03ba33b).
# log
$ git log --all --oneline --graph
* 03c4743 (HEAD -> master) ์ถฉ๋ ํด๊ฒฐ!!!
|\
| * 03ba33b hotfix test 1
* | b74a431 master test 1
|/
* 6e469f3 Merge branch 'feature/signup'
|\
| * 586d298 signup test 1
* | 2c9f3c0 master test 1
|/
* 6302c25 login test 1
* 9fe5f83 master test 1
๋๊ธ๋จ๊ธฐ๊ธฐ