본문 바로가기
개발

GIT 원격 브랜치 가져오기(checkout)

by lucidmaj7 2020. 4. 22.
728x90
반응형

GIT 원격 브랜치 가져오기

Git은 분산 버전관리 시스템이기 때문에 로컬 레포지토리와 원격 레포지토리가 다를 수 있습니다. 브랜치 역시 원격에는 있지만 로컬에는 없을 수 있는데요.
원격 레포지토리의 브랜치를 로컬 레포지토리에 가져오는 명령어를 알아보겠습니다.

1. 리모트 원격 레포지토리 보기

로컬 레포지토리에 리모트의 모든 브랜치가 없을 수 있습니다. 다음 명령어를 통해 원격레포지토리의 브랜치 목록을 볼 수 있습니다.

git branch -r
[root@localhost myapp]# git branch -r
  origin/dev
  origin/master
  origin/release

2. 원격 레포지토리 브랜치 소스 보기

원격 레포지토리의 브랜치의 소스를 굳이 로컬에 똑같이 갖고 있을 필요는 없습니다. 단지 보기만 하고싶을 때에는 임시로 원격브랜치로 checkout할 수 있습니다. checkout 명령어는 다른 브랜치로 스위치하는 명령어인데요. 여기에 원격 브랜치 이름을 넣어주면 임시적으로 원격브랜치로 checkout할 수 있습니다.

git checkout [원격 브랜치 이름]

만약 원격브랜치 이름이 origin/release라면 다음과 같이 할 수 있습니다.

[root@localhost myapp]# git checkout origin/release
Note: checking out 'origin/release'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

[root@localhost myapp]# git branch
* (detached from origin/release)
  master

checkout이 완료된 후 branch명령어로 확인을 해보면 * (detached from origin/release)로 뜨는 것을 볼 수 있습니다. 이렇게 생긴 브랜치는 다른 브랜치로 checkout하면 사라집니다.

[root@localhost myapp]# git checkout master
Switched to branch 'master'
[root@localhost myapp]# git branch
* master

3. 원격 레포지토리 브랜치를 로컬브랜치에 checkout하기

원격레포지토리의 브랜치를 영구적으로 로컬브랜치에서 tracking하고 싶다면 다음 명령어로 checkout을 할 수 있습니다.

git checkout -t [원격브랜치명]

만약 원격 브랜치 이름이 origin/release라면 다음과 같이 할 수 있습니다.

[root@localhost myapp]# git checkout -t origin/release
Branch release set up to track remote branch release from origin.
Switched to a new branch 'release'
[root@localhost myapp]# git branch
  master
* release

이렇게 하게되면 원격 브랜치 이름과 로컬 브랜치 이름이 같게 checkout됩니다.

원격 레포지토리가 여러개일 경우, 혹은 내가 만든 로컬의 브랜치 이름과 겹칠 경우 로컬 브랜치의 이름을 다르게 해야할 때도 있는데요.
다음 명령어를 통해 내가 원하는 이름의 브랜치 이름으로 checkout할 수 있습니다.

git checkout -b [생성할 브랜치 이름] [원격 브랜치 이름]

만약 원격의 origin/release브랜치를 로컬에 my_release라는 브랜치로 checkout받고 싶을 때는 다음과 같습니다.

[root@localhost myapp]# git checkout -b my_release origin/release
Branch my_release set up to track remote branch release from origin.
Switched to a new branch 'my_release'
[root@localhost myapp]# git branch
  master
* my_release
728x90
반응형

댓글