๊น(GIT) (4)- branch, checkout, merge
branch command
$ git init
Initialized empty Git repository in F:/branch_practice/.git/
$ touch a.txt
$ git add .
$ git commit -m 'Finish a.txt'
[master (root-commit) 31d9b7b] Finish a.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
๋ธ๋์น ์์ฑ
$ git branch ๋ธ๋์น์ด๋ฆ
# ์์
$ git branch feature
๋ธ๋์น ๋ชฉ๋ก ํ์ธ
*
๊ฐ ์ฐํ์๋ ๊ฒ์ ํ์ฌ ์์นํ ๋ธ๋์น๋ฅผ ์๋ฏธํจ
$ git branch
feature
* master
๋ธ๋์น ์ด๋
$ git checkout ๋ธ๋์น์ด๋ฆ
# ์์
$ git checkout feature
Switched to branch 'feature'
$ git branch
* feature
master
๋ธ๋์น ์์ฑ + ์ด๋
$ git checkout -b ๋ธ๋์น๋ช
# ์์
$ git checkout -b feature2
Switched to a new branch 'feature2'
$ git branch
feature
* feature2
master
๋ธ๋์น ๋ณํฉ (merge)
HEAD
: ํ์ฌ ๋ธ๋์น์ ์ต์ ์ปค๋ฐ
# feature2 ๋ธ๋์น์์ b.txt ํ์ผ ์์ฑ ํ add, commit
$ touch b.txt
$ git add .
$ git commit -m "Finish b.txt in feature2 branch"
[feature2 b4c6264] Finish b.txt in feature2 branch
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 b.txt
- feature2์์ b.txt๋ฅผ ์์ฑํ๋ค. ์ด๋ฅผ ๋ค์ master๋ก ๊ฐ์ feature2์ merge๋ฅผ ํ ๊ฒ์ด๋ค.
# master ๋ธ๋์น๋ก ์ด๋
$ git checkout master
Switched to branch 'master'
# merge
$ git merge ๋จธ์งํ ๋ธ๋์น์ด๋ฆ
# ์์
$ git merge feature2
Updating 31d9b7b..b4c6264
Fast-forward
b.txt | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 b.txt
# ํ์ธ
$ git log
commit b4c6264a5c45aacb80d58851e6800848723c1122 (HEAD -> master, feature2)
Author: ingu627 <rjsdudans@naver.com>
Date: Sun Nov 21 16:11:05 2021 +0900
Finish b.txt in feature2 branch
commit 31d9b7b69c3441f83c1f05295c74ac519c797d87 (feature)
Author: ingu627 <rjsdudans@naver.com>
Date: Sun Nov 21 16:06:23 2021 +0900
Finish a.txt
๋ธ๋์น ์ญ์
#
$ git branch -d ๋ธ๋์น์ด๋ฆ
# ์์
$ git branch -d feature2
Deleted branch feature2 (was b4c6264).
# ํ์ธ
$ git branch
feature
* master
branch ๋ช ๋ น์ด
1. branch ์์ฑ
git branch ๋ธ๋์น๋ช
2. branch ์ด๋
git checkout ๋ธ๋์น๋ช
3. branch ์์ฑ & ์ด๋
git checkout -b ๋ธ๋์น๋ช
4. branch ์ํ ํ์ธ (local)
git branch
5. branch ์ํ ํ์ธ (remote)
git branch -r
6. branch ์ํ ํ์ธ (local + remote)
git branch -a
7. remote branch ๊ฐ์ ธ์ค๊ธฐ
git checkout -t ๋๋ค์/๋ธ๋์น๋ช
git checkout --track ๋๋ค์/๋ธ๋์น๋ช
8. branch ๋ณํฉ
- ๋ค๋ฅธ ๋ธ๋์น๋ฅผ ํ์ฌ Checkout๋ ๋ธ๋์น์ Merge ํ๋ ๋ช ๋ น
git merge ๋ธ๋์น๋ช
9. branch ์ญ์ (local)
git branch -d ๋ธ๋์น๋ช
10. branch ๊ฐ์ ์ญ์ (local)
git branch -D ๋ธ๋์น๋ช
11. branch ์๊ฒฉ์ ์ฅ์ ์ญ์ (remote)
git push origin -d ๋ธ๋์น๋ช
git push origin --delete ๋ธ๋์น๋ช
12. ์ ํจํ์ง ์์ branch ์ ๋ฐ์ดํธ (์ฒญ์) (remote๋ด์์)
- ๋ฆฌ๋ชจํธ ๋ธ๋์น์ ๋ ์ด์ ์ ํจํ์ง ์์ ์ฐธ์กฐ๋ฅผ ๊นจ๋์ด ์ง์ฐ๋ ๋ช ๋ น์ด
git remote prune origin
git remote update --prune
13. branch ๋ก์ปฌ ์ ๋ฐ์ดํธ
- ๋ก์ปฌ ์ ์ฅ์๋ฅผ ์ต์ ์ ๋ณด๋ก ๊ฐฑ์ (๋ฆฌ๋ชจํธ ์ ์ฅ์์ ๋๊ธฐํ)ํ๋ฉฐ ์๋์ ์ผ๋ก ๋์ด์ ์ ํจํ์ง ์์ ์ฐธ์กฐ๋ฅผ ์ ๊ฑฐํ๋ค.
git fetch -p
14. commit ๊ธฐ๋ก ๊ทธ๋ํํ
git log --all --oneline --graph
15. branch ์ ๋ก๋
git push origin ๋ธ๋์น๋ช
16. branch๋ช ๋ณ๊ฒฝ
git branch -m ๋ธ๋์น๋ช
์๋ก์ด ๋ธ๋์น๋ช
17. merge์ฌ๋ถ branch ๋ณด์ฌ์ค
- ํ์ฌ Checkoutํ ๋ธ๋์น์ Mergeํ ๋ธ๋์น๋ฅผ ์ดํด๋ณธ๋ค.
- ํ์ฌ Checkoutํ ๋ธ๋์น์ Mergeํ์ง ์์ ๋ธ๋์น๋ฅผ ์ดํด๋ณธ๋ค.
git branch --merged
git branch --no-merged
๋๊ธ๋จ๊ธฐ๊ธฐ