Pro git - 2.5

Updated:

Pro git - 리모트 저장소

1. 리모트 저장소

  • 리모트 저장소는 인터넷이나 네트워크 어딘가에 저장소를 말한다.
  • 저장소는 여러 개가 있을 수 있고 어떤 저장소는 읽고 쓰기 모두 할 수 있고 어떤 저장소는 읽기만 가능하다.
  • 다른 사람들과 함께 저장소를 관리하면서 데이터를 거기에 push, pull하는 것이다.
  • 리모트 저장소를 관리한다는 것은 저장소를 추가, 삭제뿐만 아니라 branch를 관리하고 추적할지 말지 관리하는 것을 말한다.

1.1 원격 저장소라고 하더라도 로컬 시스템에 위치할 수도 있다.

  • remote 저장소라고 이름이 붙어있어도 이 원격 저장소가 사실 같은 로컬 시스템에 존재할 수도 있다.
  • 즉, remote라는 이름이 반드시 네트워크나 인터넷을 통해 어디 멀리 떨어져 있는 것을 의미하는 것은 아니다.

2. 리모트 저장소 확인하기

  • git remote 명령으로 현재 프로젝트에 등록된 리모트 저장소를 확인할 수 있다.
  • 저장소를 Clone 하면 origin이라는 리모트 저장소가 자동으로 등록되기 때문에 origin이라는 이름을 볼 수 있다.
user@DESKTOP-AVTBP4I MINGW64 /c/workspace (master)
$ git rm simplegit-progit/
fatal: pathspec 'simplegit-progit/' did not match any files

user@DESKTOP-AVTBP4I MINGW64 /c/workspace (master)
$ git clone https://github.com/schacon/ticgit
Cloning into 'ticgit'...
remote: Enumerating objects: 1857, done.
Receiving objects:  97% (18remote: Total 1857 (delta 0), reused 0 (delta 0), pack-reused 1857
Receiving objects: 100% (1857/1857), 334.06 KiB | 711.00 KiB/s, done.
Resolving deltas: 100% (837/837), done.

user@DESKTOP-AVTBP4I MINGW64 /c/workspace (master)
$ cd ticgit/

user@DESKTOP-AVTBP4I MINGW64 /c/workspace/ticgit (master)
$ git remote
origin
  • -v 옵션으로 단축이름과 URL을 함께 볼 수 있다.
user@DESKTOP-AVTBP4I MINGW64 /c/workspace/ticgit (master)
$ git remote -v
origin  https://github.com/schacon/ticgit (fetch)
origin  https://github.com/schacon/ticgit (push)
  • 리모트 저장소가 여러 개 있다면 등록된 전부를 보여줌.
user@DESKTOP-AVTBP4I MINGW64 /c/workspace/PAMS-Backend-API (pams-be-11-create-paymentList-api-test-code-function)
$ git remote -v
origin  https://github.com/sunlike0508/PAMS-Backend-API.git (fetch)
origin  https://github.com/sunlike0508/PAMS-Backend-API.git (push)
upstream        https://github.com/kwpu-system-integration/PAMS-Backend-API.git (fetch)
upstream        https://github.com/kwpu-system-integration/PAMS-Backend-API.git (push)
  • 리모트 저장소가 여러 개 등록되어 있으면 다른 사람이 기여한 내용을 쉽게 가져올 수 있다.

3. 리모트 저장소 추가하기

  • 기존 워킹 디렉토리에 새 리모트 저장소를 쉽게 추가할 수 있는데 git remote add <단축이름> 명령을 사용한다.
$ git remote
origin
$ git remote add sunlike https://github.com/paulboone/ticgit
$ git remote -v
origin	https://github.com/schacon/ticgit (fetch)
origin	https://github.com/schacon/ticgit (push)
sunlike	https://github.com/paulboone/ticgit (fetch)
sunlike	https://github.com/paulboone/ticgit (push)
  • URL 대신 sunlike라는 이름을 사용할 수 있다.

  • 만약 로컬 저장소에는 없지만 sunlike의 저장소에 있는 것을 가져오려면 fetch를 사용한다.

user@DESKTOP-AVTBP4I MINGW64 /c/workspace/ticgit (master)
$ git fetch sunlike
remote: Enumerating objects: 22, done.
remote: Counting objects: 100% (22/22), done.
remote: Total 43 (delta 22), reused 22 (delta 22), pack-reused 21
Unpacking objects: 100% (43/43), done.
From https://github.com/paulboone/ticgit
 * [new branch]      master     -> sunlike/master
 * [new branch]      ticgit     -> sunlike/ticgit
  • sunlike/master 브랜치를 로컬 브랜치 중 하나에 merge하거나 checkout해서 브랜치 내용을 확인 할 수 있다.

4. 리모트 저장소를 Pull하거나 Fetch하기

  • 위처럼 리모트 저장소에서 데이터를 가져오려면 fetch를 사용한다.
$ git fetch <remote>
  • 로컬에는 없지만 리모트 저장소에 있는 데이터를 모두 가져온다.
  • 그러면 리모트 저장소의 모든 브랜치를 로컬에서 접근할 수 있어서 언제든지 merge를 하거나 내용을 살펴볼 수 있다.
  • 저장소를 clone하면 명령은 자동으로 리모트 저장소를 “origin”이라는 이름으로 추가한다.
  • 그래서 나중에 git fetch origin 명령을 실행하면 clone한 이후에 수정된 것을 모두 가져온다.
  • git fetch 명령은 리모트 저장소의 데이터를 모두 로컬로 가져오지만, 자동으로 merge하지 않는다.
  • 따라서 수동으로 merge해야 한다.
  • 이 때문에 merge까지 자동으로 해주는 git pull 명령을 사용한다.

5. 리모트 저장소에 Push하기

  • 프로젝트를 공유하고 싶을 때 Upstream 저장소에 Push할 수 있다.
  • 형식은 git push <리모트 저장소="" 이름=""> <브랜치 이름=""> 이다.
  • master 브랜치를 origin 서버(clone하면 자동으로 생기는 origin)에 push하라면 아래와 같이 서버에 push 한다.
$ git push origin master
  • 이 명령은 Clone한 리모트 저장소에 쓰기 권한이 있고, Clone하고 난 이후에 아무도 Upstream 저장소에 Push하지 않았을 때만 사용할 수 있다.
  • 다시 말하면 Clone한 사람이 여러명 있고, 그 중에 한 명이라도 Push한 사람이 있다면 나는 Push할 수 없다는 뜻.
  • 다른 사람이 작업한 것을 가져와서 merge 후에 push가 가능하다.

6. 리모트 저장소 살펴보기

  • git remote show <리모트 저장소 이름> 명령으로 리모트 저장소의 구체적인 정보를 확인할 수 있다.
user@DESKTOP-AVTBP4I MINGW64 ~/Desktop/myBlog/sunlike0508.github.io (master)
$ git remote show origin
* remote origin
  Fetch URL: https://github.com/sunlike0508/sunlike0508.github.io.git
  Push  URL: https://github.com/sunlike0508/sunlike0508.github.io.git
  HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (fast-forwardable)
  • 리모트 저장소의 URL과 추적하는 브랜치를 출력한다.
  • 이 명령은 git pull 명령을 실행할 때 mater 브랜치와 merge할 브랜치가 무엇인지 보여준다.

  • 브랜치명을 생략하고 git push 명령을 실행하면 다음과 여러가지를 보여 준다.

    1. 어떤 브랜치가 어떤 브랜치로 Push 되는지 보여준다.

    2. 아직 로컬로 가져오지 않은 리모트 저장소의 브랜치는 어떤 것들이 있는지 보여준다.
    3. 서버에서는 삭제됐지만 아직 가지고 있는 브랜치는 어떤 것인지 보여준다.
    4. git pull 명령을 실행했을 때 자동으로 Merge 할 브랜치는 어떤 것이 있는지 보여준다.

7. 리모트 저장소 이름을 바꾸거나 리모트 저장소를 삭제하기

  • git remote rename 명령으로 리모트 저장소의 이름을 변경할 수 있다.
user@DESKTOP-AVTBP4I MINGW64 /c/workspace/ticgit (master)
$ git remote -v
origin  https://github.com/schacon/ticgit (fetch)
origin  https://github.com/schacon/ticgit (push)
sunlike https://github.com/paulboone/ticgit (fetch)
sunlike https://github.com/paulboone/ticgit (push)

user@DESKTOP-AVTBP4I MINGW64 /c/workspace/ticgit (master)
$ git remote rename sunlike sunlike0508

user@DESKTOP-AVTBP4I MINGW64 /c/workspace/ticgit (master)
$ git remote -v
origin  https://github.com/schacon/ticgit (fetch)
origin  https://github.com/schacon/ticgit (push)
sunlike0508     https://github.com/paulboone/ticgit (fetch)
sunlike0508     https://github.com/paulboone/ticgit (push)

user@DESKTOP-AVTBP4I MINGW64 /c/workspace/ticgit (master)
$ git remote rename origin testOrigin

user@DESKTOP-AVTBP4I MINGW64 /c/workspace/ticgit (master)
$ git remote -v
sunlike0508     https://github.com/paulboone/ticgit (fetch)
sunlike0508     https://github.com/paulboone/ticgit (push)
testOrigin      https://github.com/schacon/ticgit (fetch)
testOrigin      https://github.com/schacon/ticgit (push)
  • 로컬에서 관리하던 리모트 저장소의 브랜치 이름도 바뀔 수 있다.

  • 리모트 저장소를 삭제해야 한다면 git remote removegit remote rm 명령을 사용한다.

user@DESKTOP-AVTBP4I MINGW64 /c/workspace/ticgit (master)
$ git remote remove sunlike0508

user@DESKTOP-AVTBP4I MINGW64 /c/workspace/ticgit (master)
$ git remote -v
testOrigin      https://github.com/schacon/ticgit (fetch)
testOrigin      https://github.com/schacon/ticgit (push)
  • 해당 리모트 저장소에 관련된 추적 브랜치 정보나 모든 설정 내용도 함께 삭제 된다.