LoginSignup
12
15

More than 3 years have passed since last update.

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

Last updated at Posted at 2019-12-06

概要

 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 "git@path.to.repo"
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 "git@path.to.repo"
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  git@github.com:youracount/repo.git (fetch)
origin  git@github.com:youracount/repo.git (push)

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

12
15
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
12
15