tech::hexagram

personal note for technical issue.

github flowで便利なgitコマンドまとめ

業務中に便利ツールを作った際に、git commandを駆使していろいろな情報を取る機会があったのでまとめておく。

git diff

$ git diff master development --name-only

masterブランチとdevelopmentブランチで差分のあるファイルをファイル名だけ表示できる。

git log

$ git log --reverse --merges --oneline --ancestry-path ${commit_hash}...development

developmentブランチがfeatureブランチになっていたケースで、developmentブランチに向けてpull requestが送られていた場合のmerge commitを表示できる。

When given a range of commits to display (e.g. commit1..commit2 or commit2 ^commit1), only display commits that exist directly on the ancestry chain between the commit1 and commit2, i.e. commits that are both descendants of commit1, and ancestors of commit2.

gitの 公式ドキュメント にもあるように、--ancestry-path で、${commit_hash} からdevelopmentブランチの間で、developmentの祖先になっていて、かつ ${commit_hash} の子孫になっているコミットを取得できる。 --merges でmerge commitに絞り込んでいるので、下記のようなリストが取得できる。

abcdef Merge pull request #XXXX from work/hoge_branch
123456 Merge remote-tracking branch 'origin/development' into huga_branch

Githubのpull requestからマージされた場合は、上記のように Merge pull request #XXXX from というcommit messageが表示されるので、ここに grep をかけるとGithubのmerge commitだけを絞り込める。

git log --reverse --no-merges --pretty=format:%h master...development

developmentブランチにあって、masterブランチにないcommitのうち、merge commitでない( = --no-merges )ものだけをcommitの古い順に表示できる。これの先頭を取得することで、「masterブランチからdevelopmentブランチが切られた後、最初にdevelopmentブランチに入った開発中のcommit」を取得することができる。

GitHub実践入門 ~Pull Requestによる開発の変革 (WEB+DB PRESS plus)

GitHub実践入門 ~Pull Requestによる開発の変革 (WEB+DB PRESS plus)