LoginSignup
4

More than 5 years have passed since last update.

OSSメンテに便利なgitエイリアス「git pr」

Posted at

会社の人に教えてもらった便利なエイリアスを紹介しておきます。多分出典はこれで、作者と思われるgnarf氏に感謝です。
このエイリアスはプルリクエストを管理するためのもので、主にコードレビューやOSSのメンテナンスをする人に需要があると思われます。

インストール

次の.gitconfigをコピーするかadd-pr-alias.shを実行してください。

.gitconfig
# for github remotes
[alias]
  pr  = "!f() { git fetch -fu ${2:-$(git remote |grep ^upstream || echo origin)} refs/pull/$1/head:pr/$1 && git checkout pr/$1; }; f"
  pr-clean = "!git for-each-ref refs/heads/pr/* --format='%(refname)' | while read ref ; do branch=${ref#refs/heads/} ; git branch -D $branch ; done"
# for bitbucket/stash remotes
  spr  = "!f() { git fetch -fu ${2:-$(git remote |grep ^upstream || echo origin)} refs/pull-requests/$1/from:pr/$1 && git checkout pr/$1; }; f"
add-git-pr.sh
#!/bin/sh
# For github
git config --global alias.pr '!f() { git fetch -fu ${2:-$(git remote |grep ^upstream || echo origin)} refs/pull/$1/head:pr/$1 && git checkout pr/$1; }; f'
git config --global alias.pr-clean '!git for-each-ref refs/heads/pr/* --format="%(refname)" | while read ref ; do branch=${ref#refs/heads/} ; git branch -D $branch ; done'
# For stash/bitbucket
git config --global alias.spr '!f() { git fetch -fu ${2:-$(git remote |grep ^upstream || echo origin)} refs/pull-requests/$1/from:pr/$1 && git checkout pr/$1; }; f'

使い方

このエイリアスを使うと、リモートにあるプルリクエストを簡単にローカルに落としてチェックアウトすることができます。

コマンド 内容
git pr 4 ローカルにpr/4というブランチを作り、upstreamもしくはoriginの4番のプルリクエストにチェックアウトします。
git pr 4 someremote ローカルにpr/4というブランチを作り任意のリモート(someremote)の4番のプルリクエストにチェックアウトします。
git pr-clean ローカルリポジトリからpr/*の形の名前を持つブランチを全て削除します。
git spr git prと同じ動きをするが、bitbucketやstashを使う時はこちらを使う

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
4