概要
現在のブランチで、他のブランチにも含まれている最新のコミットを見つける最速の方法は何ですか?
おまけ:この手法では、上記の質問の「その他のブランチ」を「その他のリモート ブランチ」または「その他のローカル ブランチ」にすることができますか?
バックグラウンド
を使うのが大好きgit rebase -i
です。私の主な使用例は、リモートにプッシュする前にコミットを再編成することです。したがって、私はしばしばそうしますgit rebase -i origin/master
。そこで、その操作のエイリアスを設定しましたgit rewrite
。
問題は、常に に対してリベースしたくないということですorigin/master
。たとえば、ブランチで作業していて、代わりgit rewrite
にgit rebase -i origin/branch
. または、私は地元の支社で働いていて、やりたいと思っているかもしれgit rewrite
ませんgit rebase -i localbranch
。
git rewrite
つまり、実際には、スクリプトに「他のブランチに含まれる最後のコミットからインタラクティブなリベースを実行する」ようなことをさせようとしています。私が思いついたのはこれです:(これはリモートブランチを見つけるためにのみ機能します)
#!/bin/bash
# Do a git rebase -i from the most recent common point between the
# current branch and any other remote branch.
# We go backwards in the branch's history and for each commit check if
# that commit is present in one of the remote branches. As soon as we
# find one that is, we stop and rebase on that.
commit=$(git rev-parse HEAD)
while [ true ]; do
branch=$(git branch -r --contains $commit)
if [ -n "$branch" ]; then
# This commit exists in another remote branch!
break
fi
# OK, let's try the previous commit
commit=$(git log --pretty=%P -n 1 $commit)
# Stupid heuristic, take only first commit if multiple parents
commit=${commit%% *}
done
git rebase -i $commit
この方法の問題点は、遅いことです。また、コミットに複数の親がある場合、親の 1 つだけに従うため、少し不正確です。
これを行うためのより良い/より速い/よりクリーンな方法を知っている人はいますか?