본문 바로가기
IT/Git

여러 개의 Git Repository로 소스코드 Push하기

by twofootdog 2020. 2. 25.

프로젝트를 진행하다보면 로컬PC에 있는 소스코드를 여러 개의 Git Repository에서 동시에 관리해야 하는 경우가 생긴다.

그럴 경우 아래 명령어를 활용해보자.

 

 

1. 다수의 단축이름으로 여러개의 Git Repository 관리하기

1-1. 연동된 Git Repository 확인

$ git remote -v
origin  ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/gajoa-test-repository (fetch)
origin  ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/gajoa-test-repository (push)

 

 

1-2. 새로운 단축이름으로 Git Repository 추가 후(git remote add) Git Repository 확인

$ git remote add origin2 https://twofootdog@github.com/TwoFootDog/test_repo.git  
$ git remote -v
origin  ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/gajoa-test-re                                                                                                                                                                                               pository (fetch)
origin  ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/gajoa-test-re                                                                                                                                                                                               pository (push)
origin2 https://twofootdog@github.com/TwoFootDog/test_repo.git (fetch)
origin2 https://twofootdog@github.com/TwoFootDog/test_repo.git (push)

Git Repository를 추가할 때는 "git remote add [단축이름] [Git저장소 URL]" 이며 만약 git push를 할 때마다 로그인ID를 입력하기 싫다면 "git remote add [단축이름] [로그인ID@Git저장소 URL]" 로 입력하면 된다.

 

 

1-3. 소스 commit 후 각 저장소(origin, origin2)에 Git Push 하기

$ git add .
$ git commit -m "20192025_2"
$ git push origin master
Enter passphrase for key '/c/Users/minkyu/.ssh/codecommit_rsa.pem':
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 401 bytes | 0 bytes/s, done.
Total 4 (delta 3), reused 0 (delta 0)
To ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/gajoa-test-repository
   0b19e59..43fbc10  master -> master


$ git push origin2 master
Counting objects: 597, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (479/479), done.
Writing objects: 100% (597/597), 15.73 MiB | 3.43 MiB/s, done.
Total 597 (delta 288), reused 5 (delta 0)
remote: Resolving deltas: 100% (288/288), done.
To https://github.com/TwoFootDog/test_repo.git
 * [new branch]      master -> master

 

 

1-4. Git Repository 연동 삭제(git remote remove)

$ git remote remove origin2

$ git remote -v
origin  ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/gajoa-test-repository (fetch)
origin  ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/gajoa-test-repository (push)

 

 

 


2. 한개의 단축이름으로 여러개의 Git Repository 관리하기

2-1. origin에 두 개의 Git Repository를 등록하고 싶은 경우(git remote set-url --push --add)

$ git remote -v
origin  ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/gajoa-test-repository (fetch)
origin  ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/gajoa-test-repository (push)

$ git remote set-url origin --push --add https://github.com/TwoFootDog/test_repo.git

$ git remote -v
origin  ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/gajoa-test-repository (fetch)
origin  ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/gajoa-test-repository (push)
origin  https://github.com/TwoFootDog/test_repo.git (push)

위와같이 등록할 경우 git push origin master 명령어를 수행하면 두 개의 저장소로 소스 push가 일어난다.

 

 

 

2-2. origin에 등록된 Repository 삭제(git remote set-url --push --delete)

$ git remote -v
origin  ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/gajoa-test-repository (fetch)
origin  ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/gajoa-test-repository (push)
origin  https://github.com/TwoFootDog/test_repo.git (push)

$ git remote set-url origin --push --delete https://github.com/TwoFootDog/test_repo.git

$ git remote -v
origin  ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/gajoa-test-repository (fetch)
origin  ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/gajoa-test-repository (push)

 

 

2-3. (참고)origin Repository 변경하기(git remote set-url)

$ git remote -v
origin  ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/gajoa-test-repository (fetch)
origin  ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/gajoa-test-repository (push)

$ git remote set-url origin --push  https://github.com/TwoFootDog/test_repo.git

$ git remote -v
origin  ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/gajoa-test-repository (fetch)
origin  https://github.com/TwoFootDog/test_repo.git (push)

 

 

 


참고

https://git-scm.com/book/en/v2

 

Git - Book

 

git-scm.com

https://stackoverflow.com/questions/849308/pull-push-from-multiple-remote-locations

 

pull/push from multiple remote locations

The short: is there a way to have a git repo push to and pull from a list of remote repos (rather than a single "origin")? The long: I often have a situation when I'm developing an app in multiple

stackoverflow.com

 

댓글