📘

作業メモ: Gitリポジトリのお引っ越し

2025/02/02に公開

*This article contains instructions in Japanese followed by the English version.

Gitリポジトリの移行手順(履歴を含める)

あるリポジトリ (old-repo) のプロジェクトを、履歴ごとに新しいリポジトリ (new-repo) へ移行する手順をまとめます。

1. 事前準備

(1) ローカルリポジトリを最新状態にする

cd /path/to/old-repo  # 作業ディレクトリへ移動
git fetch --all --prune  # すべてのブランチとタグを取得&不要なリモート情報を削除
  • 目的: すべてのブランチ・タグを取得し、最新状態にする

(2) 現在のリモートリポジトリの確認

git remote -v
  • 目的: old-repo のリモートが設定されていることを確認

2. 新しいリポジトリへの移行

(1) 新リポジトリをリモートに追加

git remote add new-origin https://github.com/username/new-repo.git
  • 目的: new-repo をリモートとして登録

(2) すべての履歴・ブランチ・タグを新リポジトリへプッシュ

git push --mirror new-origin
  • 目的: すべてのブランチ・タグ・履歴を完全にコピー

3. 古いリモート (origin) の整理

(1) 古いリモートリポジトリを削除

git remote remove origin
  • 目的: old-repo を参照しないようにする

(2) 新しいリモートを origin に変更(任意)

git remote rename new-origin origin
  • 目的: new-repo をデフォルトの origin として扱う

(3) ローカルの古いリモートブランチ情報を整理

git remote prune origin
  • 目的: 不要になった old-repo のリモート情報を削除

4. 移行後の確認

(1) 移行したブランチ・タグの確認

git branch -a  # すべてのブランチがあるか確認
git tag        # すべてのタグがあるか確認

(2) 新しいリモートの確認

git remote -v

期待する出力:

origin  https://github.com/username/new-repo.git (fetch)
origin  https://github.com/username/new-repo.git (push)
  • 目的: originnew-repo になっていることを確認

(3) 履歴の確認

git log --oneline --all --graph
  • 目的: すべての履歴が保持されていることを確認

まとめ

git fetch --all --prune でローカルを最新状態にする
新しいリポジトリ (new-repo) をリモートに追加
git push --mirror で履歴ごと移行
古い origin を削除し、新しい origin に変更
移行後の確認 (git branch -a, git tag, git log) を実施

Git Repository Migration Guide (Including History)

This guide explains how to migrate a project from one repository (old-repo) to a new repository (new-repo) while preserving the entire Git history.

1. Preparation

(1) Update Your Local Repository

cd /path/to/old-repo  # Change to your working directory
git fetch --all --prune  # Fetch all branches and tags, and remove obsolete remote-tracking references
  • Purpose: Ensure that you have all branches and tags and that your repository is up-to-date

(2) Verify the Current Remote Configuration

git remote -v
  • Purpose: Confirm that the remote for old-repo is set correctly

2. Migrating to the New Repository

(1) Add the New Repository as a Remote

git remote add new-origin https://github.com/username/new-repo.git
  • Purpose: Register new-repo as a remote repository

(2) Push All History, Branches, and Tags to the New Repository

git push --mirror new-origin
  • Purpose: Fully copy all branches, tags, and commit history to the new repository

3. Cleaning Up the Old Remote (origin)

(1) Remove the Old Remote Repository

git remote remove origin
  • Purpose: Stop referencing the old repository (old-repo)

(2) Optionally, Rename the New Remote to origin

git remote rename new-origin origin
  • Purpose: Set new-repo as the default remote by renaming it to origin

(3) Clean Up Any Old Remote-Tracking Branch Information

git remote prune origin
  • Purpose: Remove obsolete remote-tracking branches from the old repository

4. Verifying the Migration

(1) Check the Migrated Branches and Tags

git branch -a  # Verify that all branches are present
git tag        # Verify that all tags are present

(2) Verify the New Remote Configuration

git remote -v

Expected Output:

origin  https://github.com/username/new-repo.git (fetch)
origin  https://github.com/username/new-repo.git (push)
  • Purpose: Confirm that origin now points to new-repo

(3) Verify the Commit History

git log --oneline --all --graph
  • Purpose: Ensure that the complete commit history is preserved

Summary

Update your local repository with git fetch --all --prune
Add the new repository (new-repo) as a remote
Migrate all branches, tags, and history using git push --mirror
Remove the old remote and optionally rename the new remote to origin
Verify the migration with git branch -a, git tag, and git log

Discussion