LoginSignup
7
7

More than 5 years have passed since last update.

複数のリポジトリを単一のリモートリポジトリで管理する

Last updated at Posted at 2015-09-29

リポジトリ数の制限により、リポジトリを統合した際のメモ。無料でプライベートリポジトリを使いたいだけなら bitbucket や codebreak などがありますが、それでも github を使いたいんだ!って場合にどうぞ。

前提としてリモート側のブランチ名は [元リポジトリ名]/[ブランチ名](以下 prefix/master)とする

ローカルリポジトリにリモートリポジトリを追加しリモートへプッシュする

git remote add repo git@github.com:user/repo.git
git push repo master:prefix/master

fetch時に特定のブランチのみを取得するように .git/config の fetchパスを編集する

.git/config
[remote "repo"]
    url = git@github.com:user/repo.git
-   fetch = +refs/heads/*:refs/remotes/repo/*
+   fetch = +refs/heads/prefix/*:refs/remotes/repo/prefix/*

これで下記のように push/pull/fetch が出来るようになった

git fetch repo --prune
git pull repo prefix/master
git push repo master:prefix/master

しかし、毎回 "prefix/master" とかを指定するのはめんどくさいので、デフォルトプッシュ設定とブランチ毎のアップストリームを設定し、省略コマンドで push/pull 出来るようにする

git config push.default upstream
git branch -u repo/prefix/master master

上記のコマンドを実行すると、.git/config に下記のセクションが追加される

.git/config
[push]
    default = upstream
[branch "master"]
    remote = repo
    merge = refs/heads/prefix/master

これで、下記のように現在のブランチを元にブランチ名を指定せずに push/pull が出来るようになった

git pull
git push

統合されたリポジトリから clone してくる際には "--single-branch" オプションを付けることで指定したブランチのみを clone することができる

git clone -b prefix/master --single-branch git@github.com:user/repo.git prefix
git branch -m prefix/master master
7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7