[Git] 6.4. 원격저장소 관련 CLI 명령어

업데이트:

카테고리:

태그: , ,

원격저장소 등록 및 push

커밋을 repository에 반영하는 명령어를 사용해야 하므로 연습용 repository가 필요했다. GitHub에 접속한 후 새로운 repository인 [hello-git-cli]를 만들었다.

image

다음의 명령어를 이용하여 원격저장소 등록 및 push를 해보자.

tmdgn@ASUSSH MINGW64 ~/Documents/hello-git-cli (master)
$ git remote add origin https://github.com/Orchemi/hello-git-cli.git

tmdgn@ASUSSH MINGW64 ~/Documents/hello-git-cli (master)
$ git remote -v
origin https://github.com/Orchemi/hello-git-cli.git (fetch)
origin https://github.com/Orchemi/hello-git-cli.git (push)

tmdgn@ASUSSH MINGW64 ~/Documents/hello-git-cli (master)
$ git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    fit push --set-upstream origin master

위 명령을 실행한 결과, 다음과 같은 git bash 화면을 얻을 수 있었다.

image

$ git push 명령이 실패했다. 에러 메시지를 읽어보면 로컬저장소의 [master] 브랜치와 연결된 원격저장소의 브랜치가 없어서 발생한 오류이다.

upstream 브랜치는 로컬저장소와 연결된 원격저장소라는 단어이다.

해결방법

에러 메시지에서 권장하는대로 –set-upstream을 쓰거나, 이 명령의 단축키인 -u 옵션을 사용한다.


git push 재시도

tmdgn@ASUSSH MINGW64 ~/Documents/hello-git-cli (master)
$ git push -u origin master
Counting objects: 3, done.
Writing objects: 100%(3/3), 308 bytes | 308.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/Orchemi/hello-git-cli.git

- [new branch] master -> master
  Branch 'master' set up to track remote branch 'master' from 'origin'

tmdgn@ASUSSH MINGW64 ~/Documents/hello-git-cli (master)
$ git log --oneline -n1
b45b44d (HEAD -> master, origin/master) 첫 번째 커밋

tmdgn@ASUSSH MINGW64 ~/Documents/hello-git-cli (master)
$ git push
Everything up-to-date
  • git log 명령을 보면 HEAD는 [master] 브랜치를 가르키고 있고, [origin/master] 브랜치도 생성된 것을 알 수 있다.

  • HEAD는 항상 현재 작업 중인 브랜치 혹은 커밋
  • 마지막으로 git push 명령을 한 번 더 수행했는데 에러 없이 잘 수행되었다.

여기까지의 과정을 소스트리로 확인해보면 다음과 같다.

image

GUI를 이용하여 조작할 때에는 간단한 조작이었는데, CLI로 직접 타이핑을 치려니 어색하고 어려운 것이 사실이다.


Clone

clone의 저장소 주소가 꼭 원격일 필요는 없다. 때에 따라 로컬저장소를 클론하여 편리하게 사용할 수 있다.

tmdgn@ASUSSH MINGW64 ~/Documents/hello-git-cli (master)
$ pwd
/c/Users/tmdgn/Documents/hello-git-cli

tmdgn@ASUSSH MINGW64 ~/Documents/hello-git-cli (master)
$ cd ../

tmdgn@ASUSSH MINGW64 ~/Documents
$ git clone https://github.com/Orchemi/hello-git-cli.git
fatal: destination path 'hello-git-cli' already exists and is not an empty directory.

위의 코드를 실행한 결과이다.

image

위의 실습을 보면 [hello-git-cli] repo를 클론하는 데에 실패했다. 이유가 무엇일까. [새로운 폴더명] 옵션을 지정하지 않으면 복제한 프로젝트 이름과 같은 이름의 폴더를 만들게 되는데 이미 [hello-git-cli]라는 폴더가 있기 때문이다.


git clone으로 로컬저장소 복제하기

이제는 [새로운 폴더명] 옵션을 지정해서 다시 시도해보겠다.

tmdgn@ASUSSH MINGW64 ~/Documents
$ git clone https://github.com/Orchemi/hello-git-cli.git hello-git-cli2
Cloning into 'hello-git-cli2'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.

tmdgn@ASUSSH MINGW64 ~/Documents
$ ls
'My Music'@ 'My Pictures'@ 'My Videos'@ hello-git-cli/ hello-git-cli2/

tmdgn@ASUSSH MINGW64 ~/Documents
$ cd hello-git-cli2

위의 코드를 실행한 결과이다.

image

위의 코드를 실행함으로써 [hello-git-cli2] 폴더가 생기고, 그 안에는 [master] 브랜치의 최신 커밋으로 체크아웃되었다. 이 저장소에서 다시 한 번 커밋과 push를 실행해보자.


추가 commit and push

tmdgn@ASUSSH MINGW64 ~/Documents/hello-git-cli/hello-git-cli2 (master)
$ echo "second" >> file1.txt

tmdgn@ASUSSH MINGW64 ~/Documents/hello-git-cli/hello-git-cli2 (master)
$ cat file1.txt
hello git
second

tmdgn@ASUSSH MINGW64 ~/Documents/hello-git-cli/hello-git-cli2 (master)
$ git add file1.txt
warning: LF will be replaced by CRLF in file1.txt.
The file will have its original line endings in your working directory

tmdgn@ASUSSH MINGW64 ~/Documents/hello-git-cli/hello-git-cli2 (master)
$ git commit -m "두 번째 커밋"
[master 272deb9] 두 번째 커밋
1 file changed, 1 insertion(+)

tmdgn@ASUSSH MINGW64 ~/Documents/hello-git-cli/hello-git-cli2 (master)
$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 279 bytes | 279.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/Orchemi/hello-git-cli.git
b45b44d..272deb9 master -> master

tmdgn@ASUSSH MINGW64 ~/Documents/hello-git-cli/hello-git-cli2 (master)
$ git log --oneline
272deb9 (HEAD -> master, origin/master, origin/HEAD) 두 번째 커밋
b45b44d 첫 번째 커밋

$ git commit -a 명령이 문제가 발생하였기 때문에 $ git commit -m 명령으로 대체하여 진행했다. 실행결과는 다음과 같다.

image


git pull

tmdgn@ASUSSH MINGW64 ~/Documents/hello-git-cli/hello-git-cli2 (master)
$ cd ~/Documents/hello-git-cli

tmdgn@ASUSSH MINGW64 ~/Documents/hello-git-cli (master)
$ git log --oneline
b45b44d (HEAD -> master, origin/master) 첫 번째 커밋

tmdgn@ASUSSH MINGW64 ~/Documents/hello-git-cli (master)
$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 259 bytes | 28.00 KiB/s, done.
From https://github.com/Orchemi/hello-git-cli
b45b44d..272deb9 master -> origin/master
Updating b45b44d..272deb9
Fast-forward
file1.txt | 1 +
1 file changed, 1 insertion(+)

tmdgn@ASUSSH MINGW64 ~/Documents/hello-git-cli (master)
$ git log --oneline
272deb9 (HEAD -> master, origin/master) 두 번째 커밋
b45b44d 첫 번째 커밋

tmdgn@ASUSSH MINGW64 ~/Documents/hello-git-cli (master)
$ cat file1.txt
hello git
second

위의 코드의 실행 결과는 다음과 같다.

image

[hello-git-cli] 폴더로 이동한다. 그러면 file1.txt는 [hello-git-cli2] 폴더에서만 조작하였으니, 초기 상태인 “hello git” 텍스트 밖에 없다. 하지만 $ git pull 명령어를 이용해 repo의 내용을 그대로 가져온다. 실제로 [hello-git-cli2] 폴더에서 작업한 내용을 pushg하여 repo도 두 번째 커밋까지 반영된 상태이므로 pull한 결과 [hello-git-cli] 폴더도 두 번째 커밋 기록까지 동기화할 수 있게 되었다.

Reference

  • 팀 개발을 위한 Git GitHub 시작하기, 한빛미디어, 정호영,진유림

댓글남기기