Git - 5. 원격 저장소와 push, pull

BANG·2025년 9월 4일

git

목록 보기
5/7

Local Repository

  • 지역 저장소

Remote Repository

  • 원격 저장소
  • github 도 원격 저장소 중 하나

대략 흐름
Local Repository1 --push--> Remote Repository
Local Repository2 <--clone-- Remote Repository
Local Repository2 --push--> Remote Repository
Local Repository1 <--pull-- Remote Repository
Local Repository1 --push--> Remote Repository
Local Repository2 <--pull-- Remote Repository

원격 저장소와 연결

git remote add 원격저장소별칭 원격저장소주소

  • 원격 저장소와 연결
  • 별칭은 하나의 지역 저장소는 여러 원격 저장소와 연결 가능하므로 이름 지정
$ git remote add origin https://github.com/~~~/git-study.git


$ git remote
origin


$ git remote -v
origin  https://github.com/~~~/git-study.git (fetch)
origin  https://github.com/~~~/git-study.git (push)

원격 저장소에 업로드

git push

  • 연결 후, 최초는 설정 필요
  • git push --set-upstream origin main: 지역 저장소는 앞으로 git push 명령어만 입력하면 origin의 main 브랜치에 기본적으로 push되는 것
$ git push
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin main

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


$ git push --set-upstream origin main
Enumerating objects: 13, done.
Counting objects: 100% (13/13), done.
Delta compression using up to 8 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (13/13), 949 bytes | 949.00 KiB/s, done.
Total 13 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/~~~/git-study.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

다른 로컬에 복제

  • 다른 컴퓨터에 원격 저장소의 내용을 복제하여 로컬 저장소 만들기
  • Clone = init + pull

git clone 원격저장소주소

  • 해당 폴더에 git-study 라는 디렉토리가 생성됨
$ git clone https://github.com/~~~/git-study.git
Cloning into 'git-study'...
remote: Enumerating objects: 13, done.
remote: Counting objects: 100% (13/13), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 13 (delta 1), reused 13 (delta 1), pack-reused 0 (from 0)
Receiving objects: 100% (13/13), done.
Resolving deltas: 100% (1/1), done.



$ git remote
origin

$ git remote -v
origin  https://github.com/~~~/git-study.git (fetch)
origin  https://github.com/~~~/git-study.git (push)

로컬 저장소에 다운로드

  • 원격 저장소의 내용을 로컬 저장소에 내용 동기화

git pull

  • 컴퓨터2 에서 작업하고 push로 내용 원격 저장소로 업로드
$ vi hello.txt

$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")


$ git add hello.txt

$ git commit -m "backup2"
[main 6c6b0ac] backup2
 Committer: unknown <1111@~~~~~.com>
 1 file changed, 2 insertions(+), 1 deletion(-)

$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 244 bytes | 244.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To https://github.com/~~~/git-study.git
   02c8304..6c6b0ac  main -> main


$ git log
commit 6c6b0acd25265f8ff8c33ead14b77af0303b8a32 (HEAD -> main, origin/main, origin/HEAD)
Author: unknown <1111@~~~~~.com>
Date:   Thu Sep 4 11:23:30 2025 +0900

    backup2

commit 02c83049a7f582cfa7a4977d4f9d7d1a6380f491
Author: unknown <1111@~~~~~.com>
Date:   Wed Sep 3 23:02:41 2025 +0900

    Revert "r4"

    This reverts commit 55da572beb18e1888c4ebdb64bc6127c054ab71b.

commit 55da572beb18e1888c4ebdb64bc6127c054ab71b
Author: unknown <1111@~~~~~.com>
Date:   Wed Sep 3 22:38:18 2025 +0900

    r4

commit f56068461e55712ccf5cc0553615a3bbfb084e09
Author: unknown <1111@~~~~~.com>
Date:   Wed Sep 3 22:38:03 2025 +0900

    r3

commit 26eb89e1026b4d9f0b1d171f4ff37d8db04a4ec5
Author: unknown <1111@~~~~~.com>
Date:   Wed Sep 3 20:29:20 2025 +0900

    message 2

commit 2eb09a5abdf2bef1d7c119e42ec2dc70bf93e312
Author: unknown <1111@~~~~~.com>
Date:   Wed Sep 3 20:21:52 2025 +0900

    message 1

  • 컴퓨터1이 로컬 저장소에 내용 동기화
$ 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 (from 0)
Unpacking objects: 100% (3/3), 224 bytes | 14.00 KiB/s, done.
From https://github.com/~~~/git-study
   02c8304..6c6b0ac  main       -> origin/main
Updating 02c8304..6c6b0ac
Fast-forward
 hello.txt | 3 ++-


$ git log
commit 6c6b0acd25265f8ff8c33ead14b77af0303b8a32 (HEAD -> main, origin/main, origin/HEAD)
Author: unknown <1111@~~~~~.com>
Date:   Thu Sep 4 11:23:30 2025 +0900

    backup2

commit 02c83049a7f582cfa7a4977d4f9d7d1a6380f491
Author: unknown <1111@~~~~~.com>
Date:   Wed Sep 3 23:02:41 2025 +0900

    Revert "r4"

    This reverts commit 55da572beb18e1888c4ebdb64bc6127c054ab71b.

commit 55da572beb18e1888c4ebdb64bc6127c054ab71b
Author: unknown <1111@~~~~~.com>
Date:   Wed Sep 3 22:38:18 2025 +0900

    r4

commit f56068461e55712ccf5cc0553615a3bbfb084e09
Author: unknown <1111@~~~~~.com>
Date:   Wed Sep 3 22:38:03 2025 +0900

    r3

commit 26eb89e1026b4d9f0b1d171f4ff37d8db04a4ec5
Author: unknown <1111@~~~~~.com>
Date:   Wed Sep 3 20:29:20 2025 +0900

    message 2

commit 2eb09a5abdf2bef1d7c119e42ec2dc70bf93e312
Author: unknown <1111@~~~~~.com>
Date:   Wed Sep 3 20:21:52 2025 +0900

    message 1
profile
Record Everything!!

0개의 댓글