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?
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?
Add the git log --decorate
and it will display branches, tags etc.
--graph
.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
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
) tolog --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
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.
Use the --source
flag.
From the documentation:
--source
Print out the ref name given on the command line by which each commit was reached.