Laurent Kempé
Laurent Kempé

Reputation: 761

Git current branch commits list

When you do a Pull Request in Github or Stash you get a list of the commit from your current branch.

What would be the git command to get a list of the commit which makes up the current branch without specifying the name of the branch which we started from?

Upvotes: 3

Views: 9792

Answers (4)

Pascal
Pascal

Reputation: 8939

Based on the above answer (https://stackoverflow.com/posts/26670064/revisions), here are a couple shell aliases:

# output the name of the current git branch
currentbranch(){
  if [ $(git rev-parse --is-inside-work-tree) ]; then
    git rev-parse --abbrev-ref HEAD
  fi
}

# log only commits that belong to the current branch
logbranch() {
  git log $(currentbranch) --not $(git for-each-ref --format='%(refname)' refs/heads/ | grep -v "refs/heads/$(currentbranch)")
}

Upvotes: 0

Alex Wolf
Alex Wolf

Reputation: 20118

In git it's not possible to only list commits for a specific branch without specifying references you want to exclude.

But you could determine the references programatically, such a command could look like this:

git log HEAD $(git branch -a | grep -v "^*" | grep -v "\->" | sed "s/^ /--not /")

For easier usage you could define an alias:

git config --global alias.branchlog '!git log HEAD $(git branch -a | grep -v "^*" | grep -v "\->" | sed "s/^ /--not /")'

And then just use it by typing git branchlog.

Note: If you want to ignore remote branches, you have to remove the -a option from the git branch -a call.


This command will log all commits which are ONLY reachable from the current HEAD. It achieves this by listing all branches (git branch -a), removing the current branch from the result and remote HEADs (grep -v "^*" and grep -v "\->"). In the last step it prepends --not to each branch to tell git log to exclude this reference.

Note: Remote HEADs look like this remote/origin/HEAD -> remote/origin/master and mess with git log.

If you would type the command by hand it could look like this:

git log HEAD --not master --not origin/master

Upvotes: 1

errakeshpd
errakeshpd

Reputation: 2552

change your current git branch

git branch - > to list out the branches in the repos (* indicates you are in that branch, your commits will come under this branch) if you want to see the commit of any of the branch then

git checkout branchname -> change the branch which you want to see the commits then

git log -> will show you the commits under the branch

Upvotes: -2

VonC
VonC

Reputation: 1323223

You usually need to know from which branch you are coming from in order to list the commit specific to your current branch, as I explained in "Git log to get commits only for a specific branch"

Otherwise, you need to exclude the commits which are not part of only your branch (as suggested by dimirc):

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

Or simpler, using git merge-base (if HEAD is not on your branch as in this question):

git log $(git merge-base HEAD branch)..branch

Upvotes: 1

Related Questions