Working With Remotes

git remote

로컬저장소와 연결되어있는 외부저장소의 별명?(shortname이라 한다. )을 보여준다. 만약 -v옵션을 달아준다면 외부저장소로 향하는 full path까지 보여준다. 아래는 크롬라이트하우스 외부저장소와 연결되어 있을 때의 예이다.

git remote -v

origin https://github.com/GoogleChrome/lighthouse.git (fetch)
origin https://github.com/GoogleChrome/lighthouse.git (push)
# branch의 이름이 있듯 외부저장소도 이름이 있다.

git remote add (shortname) (path)

외부저장소를 추가해 준다. 물론 같은 별명은 사용 할 수 없다.

$ git remote add baby https://github.com/baby/baby.git
$ git remote -v

origin https://github.com/GoogleChrome/lighthouse.git (fetch)
origin https://github.com/GoogleChrome/lighthouse.git (push)
baby https://github.com/baby/baby.git (fetch)
baby https://github.com/baby/baby.git (push)

git push (remote-shortname) (branch)

로컬저장소의 코드가 외부저장소의 코드보다 버전이 높을 경우, 코드를 외부저장소로 업로드 할때 사용한다. (외부저장소를 로컬저장소의 코드버전과 맞춘다)

$ git push origin master

git pull (remote-shortname) (branch)

외부저장소의 코드가 로컬저장소의 코드보다 버전이 높을 경우, 외부저장소의 코드와 로컬저장소의 코드를 동기화 시켜줄때 사용한다.

git fetch (remote-shortname) (branch)

pull과 흡사하지만, pull이 자동으로 외부저장소와 로컬저장소의 코드를 병합한다면, fetch는 외부저장소의 코드는 내려받되 다른 브랜치를 생성하여 코드가 병합되지 않게 한다. 충분히 코드 확인 후 pull할수 있는 기회를 준다.

$ git fetch origin master

 * branch            master     -> FETCH_HEAD
  58c4b91..0db9736  master     -> origin/master

origin/master와 로컬저장소의 master는 다르다. origin/master는 외부저장소 origin 에서 가져온 master라고 해석하면 된다. 그래서 push할때 origin 이라는 외부저장소를 지정하고 master를 보내는것이고, 이렇게 보내진 코드는 외부저장소에서 origin/master가 된다.

$ git diff master origin/master
# master와 origin/master를 비교한다.

diff --git a/README.md b/README.md
index e69de29..7c60b70 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1 @@
+edit readme^M

특히, 로컬저장소의 버전과 외부저장소의 버전이 상당히 다른 방향으로 흘러갔을 경우, pull을 사용하면 에러가 발생하지만, fetch를 사용하면 에러가 발생하지 않고 단지 브랜치만 추가된다. (동영상링크) 만약 두 코드에 충돌이 발생하지 않는 경우 fetch이후 git merge origin/master로 병합해주면 된다.

results matching ""

    No results matching ""