💽

【Git,GitHub】リモートリポジトリを複製する方法

2021/11/20に公開

概要

コミット履歴も含め、リモートリポジトリを別のリモートリポジトリに複製するときの手順です。

手順

例として、

<コピー元>
rails-docker-sample

<コピー先>
rails-docker

としてリポジトリをコピーしてみます。

新しいリポジトリをGitHubで作成

rails-dockerリポジトリを作成します。

コピー元のリポジトリのベアリポジトリをcloneする

ベアリポジトリとは、「作業ディレクトリを持たず、Gitの更新された情報のみを持つ」リポジトリのことです。
ディレクトリ名は「hogehoge.git」のように .git を付けます。

$ git clone --bare https://github.com/ユーザー名/コピー元リポジトリ名.git

今回は、コピー元であるrails-docker-sampleのベアリポジトリをcloneします。

$ git clone --bare https://github.com/WAKUZO/rails-docker-sample.git
Cloning into bare repository 'rails-docker-sample.git'...
remote: Enumerating objects: 163, done.
remote: Counting objects: 100% (163/163), done.
remote: Compressing objects: 100% (117/117), done.
remote: Total 163 (delta 24), reused 155 (delta 17), pack-reused 0
Receiving objects: 100% (163/163), 148.87 KiB | 1.25 MiB/s, done.
Resolving deltas: 100% (24/24), done.

コピー元リポジトリの複製をコピー先リポジトリにpushする

ベアリポジトリでクローンした「コピー元リポジトリ」を、「コピー先リポジトリ」にミラープッシュします。

$ cd コピー元リポジトリ名.git
$ git push --mirror https://github.com/ユーザー名/コピー先リポジトリ名.git

今回は、コピー元のrails-docker-sampleのリポジトリを、コピー先のrails-dockerに複製します。

$ cd rails-docker-sample.git
$ git push --mirror https://github.com/WAKUZO/rails-docker.git
Enumerating objects: 163, done.
Counting objects: 100% (163/163), done.
Delta compression using up to 8 threads
Compressing objects: 100% (110/110), done.
Writing objects: 100% (163/163), 148.87 KiB | 148.87 MiB/s, done.
Total 163 (delta 24), reused 163 (delta 24), pack-reused 0
remote: Resolving deltas: 100% (24/24), done.
To https://github.com/WAKUZO/rails-docker.git
 * [new branch]      main -> main

GitHubに戻りrails-dockerリポジトリを更新してみると、複製できています。
コミット履歴もそのままコピーされています。

ちなみに、以下がrails-docker-sampleリポジトリです。

まったく一緒です。

ローカルのコピー元リポジトリ(古いリポジトリ)を削除する

不要であれば削除しておきましょう。

$ cd ..
$ rm -rf old-repository.git

今回は、コピー元のrails-docker-sampleを削除します。

$ cd ..
$ rm -rf rails-docker-sample.git

参考

リポジトリを複製する

Discussion