1 ๋ถ„ ์†Œ์š”



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



References

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ