6

It there any placeholder for showing branch name that commit is in for git --pretty=format (in git log and git show)?

Like %H for commit hash?

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
david8
  • 444
  • 9
  • 23
  • possible duplicate of [How can I show the name of branches in \`git log\`](http://stackoverflow.com/questions/1841405/how-can-i-show-the-name-of-branches-in-git-log) – gpullen Jun 02 '15 at 06:55
  • 1
    Maybe this: " With `--pretty` you can use `%d` where you want the 'decorations'." – david8 Jun 02 '15 at 07:16
  • Commits aren't in branches, branches are just repo-local labels hung on a particular commit. For example, most projects have just one root and every single branch traces back to that. Git couldn't care less which if any branch is your "main" branch, that's entirely a matter of interpretation. – jthill Jun 02 '15 at 15:31
  • With Git 2.21 (Q1 2019), `git log --format=%S` would be the official way to add a branch placeholder in `git log` output. See [my answer below](https://stackoverflow.com/a/54846615/6309). – VonC Feb 23 '19 at 21:57

4 Answers4

10

Add the git log --decorate and it will display branches, tags etc.

  • If you want to log to display the graph as well you can add the --graph
  • If you use uxin based OS you can use this .githelpers

From the log documentation:

--decorate [=short|full|no]

Print out the ref names of any commits that are shown. If short is specified, the ref name prefixes refs/heads/, refs/tags/ and refs/remotes/ will not be printed. If full is specified, the full ref name (including prefix) will be printed. The default option is short

Output of the .githelprs script:

enter image description here

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
2

You now (Q1 2019) have an official placeholder, as custom userformat "log --format" learned %S atom that stands for the tip the traversal reached the commit from, i.e. --source.

See commit ad6f028 (11 Jan 2019) by Issac Trotts (ijt).
(Merged by Junio C Hamano -- gitster -- in commit a562a11, 29 Jan 2019)

log: add %S option (like --source) to log --format

Make it possible to write for example:

git log --format="%H,%S"

where the %S at the end is a new placeholder that prints out the ref (tag/branch) for each commit.

Using %d might seem like an alternative but it only shows the ref for the last commit in the branch.

Example:

git checkout --orphan source-a &&
test_commit one &&
test_commit two &&
git checkout -b source-b HEAD^ &&
test_commit three

means that git log --format=%S source-a source-b returns commits from a and b:

source-b
source-a
source-b
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

Commits aren't in branches, branches are just repo-local labels hung on a particular commit. For example, most projects have just one root and every single branch traces back to that. Git couldn't care less which if any branch is your "main" branch, that's entirely a matter of interpretation.

You can

git branch --contains $thatcommit

to see all the branches that can trace ancestry for that commit, and

git log --branches --decorate --simplify-by-decoration --oneline \
        --ancestry-path --first-parent ^$thatcommit

to see all the branches that trace ancestry to that commit via their first-parent links (i.e. not branches that incorporate that commit only by merge).

edit: it's really easy to just say "branches", but for newcomers it's really hard to keep in mind that every name you use in git traces back in very short order to an otherwise-undifferentiated object in the object db -- a refname is just a handy thumb into the repo. More pedantically correct would be "branch tip" or even "branch tip commit".

The only thing special about a branch ref is its full spelling is "refs/heads/$branchname" and when git checkout sees it it makes HEAD be a symbolic reference to that -- so everybody that updates head instead updates the branch tip, and everybody that looks at HEAD sees the current branch tip.

You can hang repo-local notes on branch refs, several convenience commands look in the branch.$branchname config section for their defaults.

jthill
  • 55,082
  • 5
  • 77
  • 137
0

Use the --source flag.

From the documentation:

--source

Print out the ref name given on the command line by which each commit was reached.

Community
  • 1
  • 1
Joseph K. Strauss
  • 4,683
  • 1
  • 23
  • 40