추천 가젯

Git과 CLI (3) - 원격저장소 관련 CLI 명령어

원격저장소 관련 CLI 명령어

remote, push, pull

원격 저장소를 등록하는 CLI 명렁어는 다음과 같다.

  • git remote add <원격저장소 이름> <원격저장소 주소>:

    • 원격저장소를 등록한다.

    • 원격저장소는 여러 개 등록할 수 있지만 같은 별명의 원격저장소는 하나만 가질 수 있다. 통상 첫 번째 원격저장소를 origin으로 칭한다.

  • git remote -v: 원격저장소 목록을 살펴본다.

프로젝트를 만들면 원격저장소 URL과 clone 할 수 있는 명령어를 보여준다.

yegang@yegangs:~/hello-git-cli$ git remote add origin https://github.com/yeganghwang/hello-git-cli.git
yegang@yegangs:~/hello-git-cli$ git remote -v
origin  https://github.com/yeganghwang/hello-git-cli.git (fetch)
origin  https://github.com/yeganghwang/hello-git-cli.git (push)
yegang@yegangs:~/hello-git-cli$ git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.

yegang@yegangs:~/hello-git-cli$

이전에 만들어 둔 hello-git-cli 폴더에 커밋을 푸시하면 오류가 발생한다. 왜냐하면 로컬저장소의 master 브랜치와 연결된 원격저장소의 브랜치가 없기 때문이다. 그리고 upstream 이라는 텍스트가 보이는데 업스트림 브랜치는 로컬저장소와 연결된 브랜치를 뜻한다. 즉 git remote 명령어로 원격저장소를 추가했으나 로컬저장소와 원격저장소의 브랜치를 업스트림으로 지정하지 않았으니 오류가 발생한 것이다. 이전 포스트에서 설명한 -u 옵션을 이용하여 orogin/master가 로컬저장소의 master 브랜치의 업스트림으로 지정하여 push를 할 수 있다.

yegang@yegangs:~/hello-git-cli$ git push -u origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 242 bytes | 242.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/yeganghwang/hello-git-cli.git
 * [new branch]      master -> master
branch 'master' set up to track 'origin/master'.
yegang@yegangs:~/hello-git-cli$ git log --oneline
f3d91a3 (HEAD -> master, origin/master) First commit
yegang@yegangs:~/hello-git-cli$ git push
Everything up-to-date # 업스트림을 지정하였으므로 오류가 발생하지 않음.
yegang@yegangs:~/hello-git-cli$
  • HEAD: 현재 작업중인 브랜치 혹은 커밋

  • master: 로컬의 master 브랜치

  • origin/master: 원격저장소의 master 브랜치

git clone

git clone 명령어를 사용하여 원격저장소를 복제할 수 있다. 경로와 이름만 중복되지 않는다면 한 원격저장소를 여러 번 복제하는 것도 가능하다.

yegang@yegangs:~/hello-git-cli$ pwd
/var/services/homes/yegang/hello-git-cli

yegang@yegangs:~/hello-git-cli$ cd ..

yegang@yegangs:~$ pwd
/var/services/homes/yegang

yegang@yegangs:~$ git clone https://github.com/yeganghwang/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 (from 0)
Receiving objects: 100% (3/3), done.

yegang@yegangs:~$ ls | grep hello-git-cli
hello-git-cli
hello-git-cli2

yegang@yegangs:~$ cd hello-git-cli2

yegang@yegangs:~/hello-git-cli2$ git log --oneline
f3d91a3 (HEAD -> master, origin/master, origin/HEAD) First
 commit
 
yegang@yegangs:~/hello-git-cli2$ git remote -v
origin  https://github.com/yeganghwang/hello-git-cli.git (fetch)
origin  https://github.com/yeganghwang/hello-git-cli.git (push)
yegang@yegangs:~/hello-git-cli2$

git clone 명령이 정상적으로 작동한다.

이제 새로운 커밋을 만들어 푸시할 수 있다.

yegang@yegangs:~/hello-git-cli2$ echo "second" >> file1.txt # 파일에 내용 한 줄 추가

yegang@yegangs:~/hello-git-cli2$ cat file1.txt
hello test
second

yegang@yegangs:~/hello-git-cli2$ git commit -a -m "Second commit" # -a스테이징없이 -m커밋메시지
[master 7240873] Second commit
 1 file changed, 1 insertion(+)

yegang@yegangs:~/hello-git-cli2$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 266 bytes | 266.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/yeganghwang/hello-git-cli.git
   f3d91a3..7240873  master -> master

yegang@yegangs:~/hello-git-cli2$ git log --oneline
7240873 (HEAD -> master, origin/master, origin/HEAD) Secon
d commit
f3d91a3 First commit

다시 첫 번째 저장소(hello-git-cli)로 돌아와서 명령어를 실행한다.

yegang@yegangs:~/hello-git-cli2$ cd ../hello-git-cli

yegang@yegangs:~/hello-git-cli$ git log --oneline
f3d91a3 (HEAD -> master, origin/master) First commit # 커밋이 하나만 있었는데

yegang@yegangs:~/hello-git-cli$ git pull # pull 한 후
Updating f3d91a3..7240873
Fast-forward
 file1.txt | 1 +
 1 file changed, 1 insertion(+)

yegang@yegangs:~/hello-git-cli$ git log --oneline
7240873 (HEAD -> master, origin/master) Second commit # 두 번째 커밋이 추가됨
f3d91a3 First commit

yegang@yegangs:~/hello-git-cli$ cat file1.txt
hello test
second

댓글

가장 많이 본 글