2

概要

現在のブランチで、他のブランチにも含まれている最新のコミットを見つける最速の方法は何ですか?

おまけ:この手法では、上記の質問の「その他のブランチ」を「その他のリモート ブランチ」または「その他のローカル ブランチ」にすることができますか?


バックグラウンド

を使うのが大好きgit rebase -iです。私の主な使用例は、リモートにプッシュする前にコミットを再編成することです。したがって、私はしばしばそうしますgit rebase -i origin/master。そこで、その操作のエイリアスを設定しましたgit rewrite

問題は、常に に対してリベースしたくないということですorigin/master。たとえば、ブランチで作業していて、代わりgit rewritegit 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 つだけに従うため、少し不正確です。

これを行うためのより良い/より速い/よりクリーンな方法を知っている人はいますか?

4

2 に答える 2

1

私は行くだろう

git for-each-ref refs/heads --format='git merge-base %(refname) yourbranch' \
| sh >merge-bases
git log --first-parent --format='%H %s' \
| grep -f merge-bases -

refs/heads「任意のローカル ブランチ」バージョンで、refs/remotesリモート ブランチを追加または置換します。

プロセスの置換は、明らかに Windows のパイプラインと混在しません。Linux では、そのようにして一時ファイルを回避できます。

(編集: aaaannd にはバックレベルもあります。

于 2014-02-16T20:33:53.393 に答える