fork 元のリポジトリ変更を cherry-pick して fork 先に反映する方法

本ページ内容は git に関する情報です。

 

 

 

[概要]

Git で fork 元のリポジトリの変更を cherry-pick して fork 先に反映するには、以下の手順で実施できます。

 

[手順]

Git で別リポジトリのコミットを cherry-pick するには、以下の手順で行います。

 

  1. cherry-pick したいコミットのSHAを確認
    cherry-pick 元のリポジトリで git log などを使い、対象コミットID(ハッシュ値)を調べます。
  2. cherry-pick 先のリポジトリにリモートを追加(または一時的に fetch)
    例: git remote add upstream <URL>
    または、fetch だけする場合: git fetch <URL> <branch>
    後者の場合、次項目 3 を行わない
  3. コミットを fetch する
    例: git fetch upstream
  4. cherry-pick を実行したいブランチをチェックアウト
    例: git checkout your-branch
  5. cherry-pick コマンドを実行
    例: git cherry-pick <コミットID>
  6. git リモートサーバーへ push
    例: git push origin <自分の作業ブランチ>
  7. 不要になったリモートを削除(必要に応じて)
    例: git remote remove upstream

 

具体例:

# 1. cherry-pick 元のリポジトリでコミットSHAを確認 
$ git log

# 2. cherry-pick 先のリポジトリでリモート追加 
$ git remote add temp-origin https://github.com/example/other-repo.git 

# 3. fetchする 
$ git fetch temp-origin main 

# 4. cherry-pick したいブランチに移動 
$ git checkout your-branch 

# 5. cherry-pick 実行 
$ git cherry-pick <コミットSHA>

# 6. git リモートサーバーへ push
$ git push origin <自分の作業ブランチ>

# 7. リモート削除(任意) 
$ git remote remove temp-origin

 

 

NOTE

  • cherry-pick 実行時にコンフリクトを発生した場合、手動で解決して "git add" → "git cherry-pick --continue" で進めます。
  • "upstream remote" は一時的な追加でも良いです。不要になったら "git remote remove upstream" で削除できます。
  • 削除前に remote 一覧を確認したい場合は、"git remote -v" で確認できます。

 

 

 

ライセンス

本ページの情報は、特記無い限り下記 MIT ライセンスで提供されます。

The MIT License (MIT)

  Copyright 2025 Kinoshita Hidetoshi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 

 

参考

 


 

変更履歴

2025-08-26 - 新規作成

 

Programming Items トップページ

プライバシーポリシー