LoginSignup
13
11

More than 5 years have passed since last update.

GitHubで、もらったプルリクに対して自分のコミットを混ぜ込む

Posted at

あるいは、1つのプルリクで共同開発というやつ。GitHubに限らないと思うし、こんなテクニック、有名すぎてどこにでも書いてあると思って調べたけど全然出てこなくて僕のググり力の低さを痛感しつつメモ書きを残すことにしました。

まず、プルリクを出してきた人のリポジトリを登録します。この例だと、hogeさんのexampleリポジトリを、hogeとして登録しています。

$ git remote add hoge git@github.com:hoge/example.git

確認します。通常はoriginとupstreamだけだと思います。そこに、追加したhogeがあります。

$ git remote -v
hoge    git@github.com:hoge/example.git (fetch)
hoge    git@github.com:hoge/example.git (push)
origin  git@github.com:zaru/example.git (fetch)
origin  git@github.com:zaru/example.git (push)
upstream    git@github.com:example-team/example.git (fetch)
upstream    git@github.com:example-team/example.git (push)

次に、プルリクをfetchで取得できるようにします。.git/configファイルを直接いじるか、config --addコマンドで追加します。

$ git config --add remote.upstream.fetch '+refs/pull/*:refs/remotes/pr/*'
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = git@github.com:zaru/example.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[remote "upstream"]
    url = git@github.com:example-team/example.git
    fetch = +refs/heads/*:refs/remotes/upstream/*
    fetch = +refs/pull/*:refs/remotes/pr/* # ← 追加
[remote "hoge"]
    url = git@github.com:hoge/example.git
    fetch = +refs/heads/*:refs/remotes/hoge/*

これで、プルリクをfetchしてもってくることができました。

$ git fetch --all
Fetching origin
Fetching upstream
remote: Counting objects: 809, done.
remote: Total 809 (delta 565), reused 565 (delta 565), pack-reused 244
Receiving objects: 100% (809/809), 94.07 KiB | 0 bytes/s, done.
Resolving deltas: 100% (575/575), completed with 47 local objects.
From github.com:basicinc/case-store
 * [new ref]         refs/pull/10/head -> pr/10/head
 * [new ref]         refs/pull/100/head -> pr/100/head
 * [new ref]         refs/pull/101/head -> pr/101/head
 * [new ref]         refs/pull/11/head -> pr/11/head
 * [new ref]         refs/pull/12/head -> pr/12/head

プルリクの一覧を確認します。

$ git branch -a
  remotes/pr/10/head
  remotes/pr/100/head
  remotes/pr/101/head

修正したいプルリクをローカルにcheckoutします。

$ git checkout -b pr100 remotes/pr/100/head

あとは普通に修正してcommitします。

最後にpushをするんですが、自分のリポジトリではなく相手のリポジトリに対してpushをします。自分のブランチ名がpr100で、相手のブランチ名がpiyoだとしたら、以下のようなコマンドになります。

$ git push hoge pr100:piyo

これで無事、1つのプルリクに対して複数人でコミットを混ぜ混ぜしてプルリクを育てることができます。

13
11
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
13
11