0. 새로 추가된 remote branch를 local repository로
Remote Repository에 새로운 Branch가 추가되었고,
이를 가져와서 작업해야하는 경우에 대한 글임.
실제로 branch를 다루는 내용이며, branch를 다루는 더 자세한 내용들을 위해선 다음을 참고:
2023.12.30 - [utils/git and github] - [git] branch : branch를 생성하거나 확인 또는 삭제 및 변경
[git] branch : branch를 생성하거나 확인 또는 삭제 및 변경
git branchbranch : Branch를 생성하거나 확인하는 명령.branch 확인아래와 같이 인자 없이 수행할 경우, 현재 존재하는 branch들 (local repository의)을 보여줌.기본적으로 main (과거엔 master) 라는 branch가 존재
ds31x.tistory.com
1. Checking the Name of the New Remote Branch to Be Added
새로 추가하고자 하는 New Remote Branch 의 이름을 확인.
git fetch --all
git branch -r
다음의 형태의 출력이 보임
❯ git branch -r
origin/HEAD -> origin/main
origin/dsaint31
origin/main
origin/model_imputer
origin/s3
새로 추가된 Remote Branch의 이름이 origin/model_imputer 이라고 가정하고 다음을 진행하겠음.
2. Creating and Switching to a Local Branch for the New Remote Branch
새로운 원격 브랜치에 연결된 새로운 로컬 브랜치를 생성하고 전환.
# 새로운 branch인 origin/model_imputer에 기반한 브랜치 생성
git switch -c model_imputer origin/model_imputer
# 앞서 새로 만든 local branch인 model_imputer를 원격브랜치를 추적하도록 연결
git branch --set-upstream-to=origin/model_imputer model_imputer
# --------------------
# 위 둘을 합쳐서 다음으로 처리해되 됨.
# 이는 원격브랜치 이름과 같은 로컬브랜치를 만들고 추적하도록 연결.
git switch --track origin/model_imputer
# 다음도 가능 (이는 model_imputer 라는 이름의 로컬 브랜치를 생성하고 연결)
# -c 옵션 다음에 다른 이름으로 로컬브랜치가 가능함.
git switch -c model_imputer --track origin/model_imputer
이 명령어는 다음을 수행합니다:
model_imputer라는 새 로컬 브랜치를 생성origin/model_imputer와 연결- 해당 브랜치로 전환
2-1. 동작 원리
git fetch:
- 원격 저장소의 상태를 local repository에 업데이트.
git switch -c [new_local_branch] [remote_branch]:
- 새 local branch를 생성하고 특정 원격 브랜치
[remote_branch]를 기반으로 작업 환경을 전환. - 이는 추적 연결은 하지 않으므로 push -u 옵션을 이용하여 추적연결하던지,
- 예제에 나온대로
branch --set-upstream-to=...로 추적연결을 해야함.
git switch --track 옵션을 사용하는 것이 가장 편함.
2-2. 참고
2024.05.26 - [utils/git and github] - [Git] pull and fetch
[Git] pull and fetch
git pullRemote repository 의 최신 commit 을 가져와 Local repository의 해당 branch에 병합.최신 commits 을 받아와 임시 영역에 저장: (이 과정을 fetch라고 함).(remote resository 를 위한 임시 branch 에 저장하여 stage
ds31x.tistory.com
2023.12.31 - [utils/git and github] - [git] switch : branch 전환
[git] switch : branch 전환
git switchgit switch 는 Git 2.23 버전에서 도입.branch 전환 과 working tree 를 최근 commit 상태 로 돌리는 등의 다양한 기능을 수행하던 git checkout 을git switch 와 git restore 로 분리하면서 등장함.git switch는branch
ds31x.tistory.com
3. Check the Result
git status
git branch -vv
2023.12.30 - [utils/git and github] - [git] branch : branch를 생성하거나 확인 또는 삭제 및 변경
[git] branch : branch를 생성하거나 확인 또는 삭제 및 변경
git branchbranch : Branch를 생성하거나 확인하는 명령.branch 확인아래와 같이 인자 없이 수행할 경우, 현재 존재하는 branch들 (local repository의)을 보여줌.기본적으로 main (과거엔 master) 라는 branch가 존재
ds31x.tistory.com
같이 보면 좋은 자료
2024.05.20 - [utils/git and github] - [Git] Git Summary (작성중)
[Git] Git Summary (작성중)
git이란2024.05.20 - [utils/git and github] - Git : 소개 git 설치 후 해줘야 하는 작업들[Git] git 설치 후 우선 해줘야 하는 작업들 (tistory.com)local repository 초기화2024.05.20 - [utils/git and github] - [Git] init : local repo
ds31x.tistory.com
'utils > git and github' 카테고리의 다른 글
| SSH Agent 사용법 (2) | 2025.07.21 |
|---|---|
| 새로 remote 에 추가된 submodule 을 local 에 반영하기 (3) | 2025.07.11 |
| [Git] rebase: Tutorial (0) | 2024.05.28 |
| [Git] pager 옵션 조정-상세 (0) | 2024.05.27 |
| [Git] clone: 원격저장소 복제 (0) | 2024.05.27 |