[Git] Local, Remote 브랜치 사용법1- 생성, 연결하기, 동기화하기

728x90
반응형

 

로컬 브랜치 생성해서 remote와 연결하기

 

인텔리제이에서 작업을 하다가 Git 탭을 봤더니 다음과 같이 Local 브랜치와 Remote 브랜치가 구분되어 있는 것을 발견했다.

main 빼고 다 내가 만든 브랜치가 맞긴 한데.. 브랜치 만들어 놓고 오랜만에 건드렸더니 다 엉켜버리고,, 뭐가 다른 건지 모르겠어서 구글링을 해보았다.

 

 


 

브랜치 생성 및 이동

 

local branch 생성

git branch [브랜치 이름]

local branch 생성 후 이동

git checkout -b [브랜치 이름]

remote branch 생성

git push origin [브랜치 이름]

 

사용할 브랜치로 이동

git checkout [이동할 브랜치 이름]

 

 


 

브랜치 목록 보기


local branch 목록 보기

git branch

remote branch 목록 보기

git branch -r

local & remote 모든 branch 목록 보기

git branch -al

 

 

 


 

local, remote 브랜치 연동하기

 

생성한 local branch를 remote branch에 추가(붙여넣기)

remote branch에 local branch를 붙여넣는 과정이다. 해당 명령어를 입력한 후에 git branch -r 명령어로 잘 추가되었는지 확인해보기!

git push origin [로컬 브랜치 이름]

사용 예) main branch를 origin에 push하는 경우

git push origin main

 

 

local branch, remote branch 연동

생성된 브랜치는 각각 local 및 remote 저장소에 따로 존재한다. 따라서 local의 브랜치를 remote 브랜치와 연동하는 작업을 수행하는 것이 좋다. 브랜치 연동은 다음의 명령어로 수행한다.

git branch --set-upstream-to origin/[브랜치 이름]

 

local에서 remote로 push할 때 로컬의 해당 브랜치와 remote를 맵핑해주면 

해당 브랜치에서 push하는 경우, 뒤에 origin과 branch를 적어주지 않아도 push할 수 있다.

 

git push할 때 -u 또는 --set-upstream 옵션을 줘서 branch와 원격지를 연결해주면 그 다음부터는 git push 라고만 입력해줘도 된다.

 

git push -u origin [로컬 브랜치 이름]
git push --set-upstream [로컬 브랜치 이름]

 

 

 

📌 Tip) 사실 브랜치에서 작업하고 add, commit 해서 git push 라고만 입력하면 에러가 뜨면서 "이렇게 입력하세요~ " 하고 위의 두번째 명령어가 뜬다. 그럼 그 명령어를 그대로 복사해서 붙여넣어주면 된다! 옵션이 꽤 기니까 이것도 좋은 방법!😚

 

 

 



 

Git의 기본 브랜치를 master에서 main으로 변경하기

echo "# example-repo" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin [gitHub 레포 주소]
git push -u origin main

 

 


참고)

git cheat sheet

 

Git Cheat Sheet: Commands and Best Practices | JRebel by Perforce

Our Git command line cheat sheet includes commands, tips, tricks, best practices, and more. Download the free Git commands cheat sheet PDF, inside.

www.jrebel.com

 

728x90
반응형