0

As far as I understand, git doesn't store branch information in commits. From the perspective of gits logic, where a branch is merely a pointer to a commit, this perfectly makes sense.

However, I would find it helpful to have the branch name stored along with the commit, in order to more easily identify the meaning of branches preserved in the commit history, when combined with git merge --no-ff.

My (clumsy) current workarounds include

  • Typing the branch name as part of the commit name, or committing with a custom command that does just that.
  • Tagging commits immediately before a merge with the branch name.

Is there some way to make git preserve the branch name in commits and then display that information as part of git log without such workarounds?

kdb
  • 4,098
  • 26
  • 49
  • 1
    You can write a commit message hook (there are two, starting with [the `prepare` one](https://www.kernel.org/pub/software/scm/git/docs/githooks.html#_prepare_commit_msg)) to do this. The end product is not the same as what Mercurial does—commits are still independent of branches—but by sticking the name into the commit message text, the name will be stored permanently: the text is just as unchangeable as everything else in the commit. – torek Jan 17 '18 at 15:36
  • 1
    It might be more useful to use the id from your ticketing system instead of the branch name, grouping the commits by topic rather than by version control artifacts like branches. – LightBender Jan 17 '18 at 15:44
  • Git can not store branch name in commits automatically unless you config it manually as torek mentioned (such as git hooks). And it's not essential to store branch name in commits, since we can get the branch name by multiple ways (such as `git branch --contains `). – Marina Liu Jan 18 '18 at 03:01
  • @MarinaLiu-MSFT Is this still applicable once the branch has been deleted after the final merge? – kdb Jan 18 '18 at 12:04
  • @kdb No, the deleted branch(es) won't be showed. – Marina Liu Jan 19 '18 at 09:11

0 Answers0