Gitリポジトリのバックアップとリストア


概要

 Git リポジトリの保存と復元方法をメモしたものです。1

手順

バックアップ

# バックアップをカレントディレクトリ上にデフォルトのフォルダ名で配置する場合
# (下記の場合は repo.git フォルダとして配置される)
git clone --mirror "git@github/path/to/repo/repo.git"

# バックアップを任意のパスに配置する場合
git clone --mirror "git@github/path/to/repo/repo.git" "/path/to/other_name.git"

Git Documentation: [ git clone ]

リストア

※リモート側にリポジトリが存在しない場合はリポジトリを作成してから下記を実行する。
※バックアップデータ中のリモートリポジトリの指定を確認する場合は git remote -v を実行する。

# リモートリポジトリがバックアップと同じ場所の場合
cd "/path/to/repo.git"
git push origin

# リモートリポジトリがバックアップと異なる場所の場合
cd "/path/to/repo.git"
git remote rm origin
git remote add --mirror=push origin "[email protected]"
git push origin

Git Documentation: [ git remote, git push ]

バックアップからローカルへの clone

# カレントディレクトリ上にデフォルトの名前で配置する場合(下記の場合は /path/to/repo フォルダとして配置される)
git clone "/path/to/repo.git"

# 任意のパスで配置する場合
git clone "/path/to/repo.git" "/path/to/other_name"

Git Documentation: [ git clone ]

トラブルシューティング

'fatal: remote origin already exists' と表示される場合

$ git remote add --mirror=push origin "[email protected]"
fatal: remote origin already exists.

既に origin が指定されています。git remote rm origin で削除してから実行します。

'ERROR: Repository not found' と表示される場合

$ git push origin
ERROR: Repository not found.
fatal: Could not read from remote repository.

リモート側にリポジトリが存在しないので、リポジトリを作成してから実行します。

バックアップデータ中のリモートリポジトリ指定を確認したい場合

バックアップデータ(repo.gitフォルダ)内部にて git remote -v を実行します。

$ git remote -v
origin  [email protected]:youracount/repo.git (fetch)
origin  [email protected]:youracount/repo.git (push)

  1. GitHub と BitBucket の repository にて backup, restore, transfer(BitBucketで backup して GitHub に restore) を実施しました。