Git을 잘 써보자- 6. git branch 다루기

아래 내용은 Pluralsight의 Master Git, 누구나 이해할 수 있는 Git 입문, 생활코딩 지옥에서 온 Git의 내용을 토대로 정리한 내용입니다.

1. Branch 란?

2. Branch 기본적인 사용

  • git branch : 옵션 없이 명령어를 실행하면, branch 리스트 및 현재 브랜치를 확인 할수 있습니다.
$ git branch
  lisa
* master
  nogood
  spaghetti
  • git branch 브랜치명 : 브랜치명으로 브랜치를 생성합니다.
$ git branch make_branch
$ git branch
  lisa
  make_branch
* master
  nogood
  spaghetti
  • git checkout 브랜치명 :
    • 브랜치명 으로 브랜치를 전환합니다.
    • commit 되지 않고 수정되거나 추가된 파일은 유지됩니다.
$ git checkout make_branch
M	menu.txt
A	test.txt
A	test2.txt
Switched to branch 'make_branch'
$ git branch
  lisa
* make_branch
  master
  nogood
  spaghetti
  • git checkout -b 브랜치명 : 브랜치명으로 브랜치 생성과 동시에 브랜치를 전환합니다.
$ git checkout -b new_make_branch
M	menu.txt
A	test.txt
A	test2.txt
Switched to a new branch 'new_make_branch'
$ git branch
  lisa
  make_branch
  master
* new_make_branch
  nogood
  spaghetti
  • git branch -d 브랜치명 :
    • 브랜치명으로 브랜치를 삭제합니다.
    • 단 현재 선택된 브랜치는 삭제시 에러가 납니다.
$ git branch -d new_make_branch
error: Cannot delete the branch 'new_make_branch' which you are currently on.
$ git branch -d make_branch
Deleted branch make_branch (was b2d2b12).
$ git branch
  lisa
  master
* new_make_branch
  nogood
  spaghetti