我想列出只是特定分支的一部分的所有提交。

使用下面的代码,它列出了来自分支的所有提交,也包括来自父(master)的所有提交

git log mybranch

我发现的另一种选择是排除主可达的提交,并给我我想要的,但我想避免知道其他分支名称的需要。

git log mybranch --not master

我试图使用git for-each-ref,但它也列出了mybranch,所以实际上它排除了所有:

git log mybranch --not $(git for-each-ref --format '^%(refname:short)' refs/heads/)

更新:

我正在测试一个我不久前发现的新选项,直到现在看来,这可能是我一直在寻找的:

git log --walk-reflogs mybranch

更新(2013 - 02 - 13 t15:08):

——walk-reflogs选项很好,但我检查了reflogs的过期时间(默认90天,gc.reflogExpire)。

我想我找到了我一直在寻找的答案

git log mybranch --not $(git for-each-ref --format='%(refname)' refs/heads/ | grep -v "refs/heads/mybranch")

我只是从可用的分支列表中删除当前分支,并使用该列表从日志中排除。这样我只能得到只有mybranch才能到达的提交。


当前回答

在我的情况下,我们使用Git Flow和GitHub。你所需要做的就是:在GitHub上比较你的功能分支和开发分支。

它将只显示提交到您的特性分支。

例如:

https://github.com/your_repo/compare/develop...feature_branch_name

其他回答

我正在使用以下命令:

git shortlog --no-merges --graph --abbrev-commit master..<mybranch>

or

git log --no-merges --oneline --decorate master..<mybranch>

这将输出当前分支上的提交。如果传递了任何参数,它只输出哈希值。

git_show_all_commits_only_on_this_branch

#!/bin/bash
function show_help()
{
  ME=$(basename $0)
  IT=$(cat <<EOF
  
  usage: $ME {NEWER_BRANCH} {OLDER_BRANCH} {VERBOSE}
  
  Compares 2 different branches, and lists the commits found only 
  in the first branch (newest branch). 

  e.g. 
  
  $ME         -> default. compares current branch to master
  $ME B1      -> compares branch B1 to master
  $ME B1 B2   -> compares branch B1 to B2
  $ME B1 B2 V -> compares branch B1 to B2, and displays commit messages
  
  )
  echo "$IT"
  exit
}

if [ "$1" == "help" ]
then
  show_help
fi

# Show commit msgs if any arg passed for arg 3
if [ "$3" ]
then
  OPT="-v"
fi

# get branch names
OLDER_BRANCH=${2:-"master"}
if [ -z "$1" ]
then
  NEWER_BRANCH=$(git rev-parse --abbrev-ref HEAD)
else
  NEWER_BRANCH=$1
fi

if [ "$NEWER_BRANCH" == "$OLDER_BRANCH" ]
then
  echo "  Please supply 2 different branches to compare!"
  show_help
fi

OUT=$(\git cherry $OPT $OLDER_BRANCH $NEWER_BRANCH)

if [ -z "$OUT" ]
then
  echo "No differences found. The branches $NEWER_BRANCH and $OLDER_BRANCH are in sync."
  exit;
fi

if [ "$OPT" == "-v" ]
then
  echo "$OUT"
else
  echo "$OUT" | awk '{print $2}'
fi

从上面的评论中我看到-你只希望那些提交属于该分支,而不是它的父分支。但从技术上讲,当你从另一个分支创建一个分支时,这些提交也会复制到子分支。 但是对于你的问题,下面的命令是有用的。

git log childBranch...parentBranch

如果父分支是远程分支,可以使用以下命令。

git log yourBranch --not --remotes=origin

这是有效的:这完全符合你的要求:

for a in $(git rev-list anybranch..mybranch); do echo -n "$a "; git branch --contains $a | xargs; done

你可以使用严格的git rev-list——但如果你只关心某个分支上的提交,而不关心其他地方的提交,那么选择任何你认为自己来自的分支至少会限制实际的明显差异,而且你可以一眼看出哪些提交只属于该分支。

听起来你应该用樱桃:

git cherry -v develop mybranch

这将显示mybranch中包含的所有提交,但不包含在develop中。如果省略了最后一个选项(mybranch),它将比较当前的分支。

正如VonC指出的那样,您总是在将您的分支与另一个分支进行比较,因此了解您的分支,然后选择与哪个分支进行比较。