0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

リモートリポジトリを経由せずにコミットを運搬する方法 (git bundle)

0
Posted at

何らかの事情で、リモートリポジトリを経由することなしに、コミットを別のマシン (あるいは別のディレクトリ) に運搬して適用したいことがあると思います。

  • 例. PC1 だけでなく PC2 でも動作確認してからリモートに push したい。
  • 例. PC1 がリモートリポジトリにアクセスできず、まず PC2 に運搬したい。

このような時、git bundle でデータをバンドルファイルにまとめて運搬できます。
Git - git-bundle Documentation

1 コミットずつパッチファイルにする git format-patch もあります。こちらは同じ履歴でなくても変更を適用できます (なので、別のブランチの変更をこのブランチにも適用したい、のような用途向けです)。必ず同じ履歴に変更を適用する場合や、全履歴を丸ごと運搬する場合は、git bundle が適しています。

コミットを別のマシン (ディレクトリ) に運搬する手順

以下、具体的な手順です (ディレクトリ間の運搬の場合は、PC1 と PC2 をディレクトリに読み替えてください)。

手順1. PC1 で変更をコミットし、バンドルファイルにまとめます (注1)。

PC1 での作業
git commit -m "Hoge fuga"
git bundle create ~/work.bundle main  # main の最新コミットとその祖先

手順2. バンドルファイルを PC2 に運搬します。

PC1 から PC2 へバンドルファイルを運搬
pscp ~/work.bundle Cookie@192.168.1.102:C:/Users/Cookie/  # Git Bush + PuTTY の場合

手順3. PC2 のローカルリポジトリにバンドルファイルを反映します。

PC2 のローカルリポジトリに今回の変更直前までの履歴がある場合は以下です (注2)。

PC2 での作業
git fetch ~/work.bundle
git merge FETCH_HEAD

そうではなく、新しく target_dir に展開する場合は以下です。

PC2 での作業
git clone ~/work.bundle target_dir

なお、後に運搬した変更をどちらかの PC からリモートリポジトリに push し、それをもう片方の PC で pull したとしても支障はありません。反映済みの変更ならば Already up to date となります。

注記

  • (注1) このコマンドでは、main ブランチの最新コミットから到達可能なすべてのオブジェクトを含むバンドルファイルが生成されます。PC2 に共通の祖先 origin/main がいることを期待するなら、コマンドを git bundle create ~/work.bundle origin/main..main とできます (ファイルサイズが節約できるはずです)。
  • (注2) このとき先に git bundle verify ~/work.bundle を実行すると、「バンドルファイルの形式が正しいか」「バンドルファイルが前提とするコミットが現在のリポジトリに存在するか」を検証し、もし不足コミットがあれば表示してくれます。
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?