개인과제

[Git] 브랜치 생성, 브랜치 목록 확인 및 원격 브랜치 로컬로 가져오기

choijming21 2024. 8. 5. 10:06

팀프로젝트를 하면서 브랜치 생성, 브랜치  목록 확인 및 원격 브랜치 로컬로 가져오기를 매번 검색해서 하였다. 그래서 팀프로젝트를 하면서 매번 검색해보지 않아도 내 블로그를 보거나 기억할 수 있게 이 내용을 블로그에 간단하게 포스팅 하겠다.

 

 

 

 

 

 

branch 생성 (local repository)

git branch (브랜치명) 을 통해 브랜치 생성이 가능하다. 새로운 브랜치(new branch)를 생성 후 체크아웃 해보도록 하겠다.

(master)$ git branch newbranch
(master)$ git chechout newbranch
Switched to branch 'newbranch'

 

 

 

 

 

 

branch 생성 후 이동 (local repository)

git checkout -b (브랜치명) : git checkout은 브랜치 전환, 파일 체크아웃, 커밋으로 돌아가기 등 다양한 용도로 사용된다.

git switch -c (브랜치명) : git switch는 오직 브랜치 전환에만 사용된다. 이는 명령어의 의도를 명확하게 하여 사용자가 더 직관적으로 브랜치를 전환할 수 있도록 도와준다.

이 두가지 명령어를 통해 신규 브랜치 생성과 동시에 그 브랜치로 이동하는 것이 가능하다.

(master)$ git checkout -b newbranch2
Switched to branch 'newbranch2'

(master)$ git switch -c newbranch3
Switched to branch 'newbranch3'

 

 

 

 

 

 

branch 생성 (remote repository)

위에서 생성한 브랜치를 깃헙(github)같은 원격 저장소(remote repository)에 생성하려면 먼저 remote add 명령어를 사용해 원격저장소를 지정해준 뒤 git push origin (브랜치명) 명령어를 사용하여 push해주면 된다.

(newbranch)$ git remote add origin https://github.com/jigong2024/sparta-git-test.git
(newbranch)$ git push origin newbranch
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'newbranch' on GitHub by visiting:
remote:      https://github.com/jigong2024/sparta-git-test.git/pull/new/newbranch
remote:
To https://github.com/jigong2024/sparta-git-test.git

 

 

 

 

 

★ 원격 브랜치 업데이트 (remote branch update)

git remote update : 이 명령어를 통해 원격 레포지토리의 브랜치들이 로컬 헤포지토리에도 반영되게 한다.

 

 

 

 

 

 

branch 목록 조회

git branch : 로컬 브랜치 목록 조회

git branch -r : 원격 브랜치 목록 조회

git branch -a : 원격, 로컬 모든 브랜치 목록 조회

이 세가지 명령어를 통해 브랜치 목록 조회가 가능하며 옵션을 통해 원하는 목록만 조회할 수 있다.

git branch  (로컬 브랜치 목록 조회)
git branch -r  (원격 브랜치 목록 조회)
git branch -a  (모든 브랜치 목록 조회)

(master)$ git branch 
* master
  newbranch
  newbranch2

(master)$ git branch -r
  origin/master
  origin/newbranch

(master)$ git branch -a
* master
  newbranch
  newbranch2
  origin/master
  origin/newbranch

 

 

 

 

 

 

★ 원격 브랜치 로컬로 가져오기

원격 저장소에서 clone을 받은 후 git branch로 확인해보면 원격 저장소의 branch는 받아지지 않고 main 브랜치만 있는 것을 확인할 수 있다. 이때, 원격 저장소에 존재하는 branch를 확인하고 해당 브랜치를 로컬로 가져오려면 git checkout -t origin (브랜치명) 을 사용하면 된다.

git branch -r // 원격 저장소 branch 리스트 확인

git branch -a // 로컬과 원격 저장소 branch 리스트 확인

git checkout -t origin newbranch // 원격의 newbranch 브랜치 가져오기

 

 

 

 

 

 

 

 

이 정도만 안다면 팀프로젝트를 하는데 무리는 없을 것이다!!

'개인과제' 카테고리의 다른 글

3주차 팀프로젝트 중간점검  (0) 2024.08.06
CSS 활용해서 반응형 웹사이트 만들기  (0) 2024.08.05
240801 걷기반 실습  (0) 2024.08.01
240731 걷기반 수업  (0) 2024.07.31
240730 걷기반 실습  (0) 2024.07.30