CI\CD/Git Remote

[Git Remote] git remote, push를 이용해 Remote 저장소(GitHub, GitLab, etc)를 다루는 방법

git의 remote란 무엇인가?

git에서의 remote 는 외부의 깃헙 저장소가 있는 url에 대한 alias(별칭)을 만들어 관리하기 위한 명령어이다. 이 명령어는 단순히 GitHub만을 위한 것이 아니며, GitLab, Garrit 과 같은 외부 저장소를 다루기 위해서 사용한다.

 

이 글에서는 다음 순서로 GitHub Repository를 조작하는 방법을 다룬다.

  • Github 저장소를 로컬 git에서 별칭으로 만들어 관리하기
  • git remote 사용해 remote 저장소 목록 가져오기
  • git push 사용해 remote 저장소에 로컬 커밋 올리기

 

 


Github 저장소를 로컬 git에서 별칭으로 만들어 관리하기

git remote add [alias] [url] 을 이용해 특정 url을 alias로 다루도록 할 수 있다. alias는 아무렇게나 설정 가능하다.

$ git remote add [alias] [url]

 

예를 들어 https://github.com/simplistudio/demo-github.git 이라는 저장소를 만들고 해당 저장소를 로컬 git에서 origin이라는 이름으로 다루고 싶다면 다음과 같이 설정하면 된다.

$ git remote add origin https://github.com/simplistudio/demo-github.git

 

ssh 이용해 저장소에 접근하는 방법

이전 글에서 ssh를 설정하는 방법을 다루었다. ssh를 이용해 GitHub 저장소에 접근하기 위해서는 다음의 명령어를 사용하면 된다.

$ git add origin origin-ssh git@github.com:simplistudio/demo-github.git

 

외부 git 저장소에 대한 ssh 주소값은 git@[GitHub url]:[GitHub user name]/[GitHub repo name].git 이다. 이 값은 repo를 새로 만든 후 ssh를 눌러 곧바로 확인 가능하다.

그림1. ssh이용해 github 저장소 접근하기

 

 

 


git remote 사용해 remote 저장소 목록 가져오기

remote 저장소 목록을 가져오기 위해서는 두 개의 명령어를 사용할 수 있다.

  • git remote : alias 목록을 가져온다
  • git remote -v : alias와 alias가 연결된 url도 같이 표시

 

다음의 명령어를 이용해 두 개의 origin, origin-ssh remote repository를 add 한 상황을 생각해보자.

$ git remote add origin-ssh git@github.com:simplistudio/demo-github.git
$ git remote add origin https://github.com/simplistudio/demo-github.git

 

git remote 사용해 alias 목록 가져오기

git remote를 사용하면 alias가 모두 출력된다. 하지만 이 값으로는 url을 알 수 없다.

$ git remote
origin
origin-ssh

 

git remote -v 사용해 alias와 url 모두  가져오기

git remote -v를 사용하면 alias와 url을 모두 가져올 수 있다. -v는 verbose의 약자로 "말수가 많은" 이라는 뜻을 가진다. 뜻만큼 모든 내용을 출력한다.

$ git remote -v
origin	https://github.com/simplistudio/demo-github.git (fetch)
origin	https://github.com/simplistudio/demo-github.git (push)
origin-ssh	git@github.com:simplistudio/demo-github.git (fetch)
origin-ssh	git@github.com:simplistudio/demo-github.git (push)

 

 

 


git push 사용해 remote 저장소에 로컬 커밋 올리기

git push 명령어는 remote 저장소에 로컬 커밋을 올리기 위한 명령어이다. 다음 명령어로 remote 저장소의 어느 브랜치에 커밋을 올릴 것인지를 설정할 수 있다.

$ git push [alias] [branch name]

 

예를 들어 origin 에 해당하는 https://github.com/simplistudio/demo-github.git 의 master 브랜치에 현재 브랜치의 변경사항을 올리고 싶다면 다음과 같이 쓰면 된다. 주의할 점은 push는 remote 전용 명령어이기 때문에 remote를 따로 쓰지 않아도 된다는 점이다.

$ git push origin master

 

 

git push 사용해 현재 브랜치가 아닌 다른 브랜치 remote 저장소에 올리기

다음 명령어를 이용해 현재 작업중인 브랜치가 아닌 다른 브랜치를 remote 에 올릴 수 있다.

$ git push [alias] [local branch name]:[remote branch name]

 

예를 들어 local의 feature-layout 을 master 브랜치에 푸시하고 싶은 경우 다음과 같이 사용하면 된다.

$ git push origin feature-layout:master

 

반응형

 

이 글의 저작권은 '조세영의 Kotlin World' 에 있습니다. 글, 이미지 무단 재배포 및 변경을 금지합니다.

 

 

Kotlin, Android, Spring 사용자 오픈 카톡

오셔서 궁금한 점을 질문해보세요!
비밀번호 : kotlin22

open.kakao.com