4

I have a feature branch - let's call it featureX' When I'm developing it - I create several commits, send them to the server, test it - several more commits etc... When later I merge this branch to master - I want to see in the history, which commits were part of this featureX branch before they were committed to master.

Currently my way is to remember to add that to the comment whenever I commit - but I'm lazy and not always remember to do so.

Is there a way to tag these commits, or automatically add to their comments a prefix with the branch name?

I'm using git in Visual Studio, using it's UI

Noam
  • 4,472
  • 5
  • 30
  • 47
  • Why do you need to identify which branch these commits were on when they were developed? – Lasse V. Karlsen Oct 11 '18 at 09:48
  • @LasseVågsætherKarlsen I was about to ask the same thing. Then I thought about one possible very useful aspect of this : in JIRA/Bitbucket commits starting with something else than the branch name are not listed on the ticket. And it's an hassle to type in long branch names repeatedly. – Romain Valeri Oct 11 '18 at 09:56
  • Other than the ticket number you mean? We use this format: `CODE-NUMBER: Comment`, where "CODE-NUMBER" is the JIRA ticket number. The branch is irrelevant for us. – Lasse V. Karlsen Oct 11 '18 at 10:07
  • @LasseVågsætherKarlsen Yes, but when a branch is created via JIRA's "create branch" fonctionnality, it's named after the (slugified) ticket title, so I guess the result is the same... – Romain Valeri Oct 11 '18 at 10:11
  • Well, no, because if it were the same we wouldn't be having this discussion under this question :) Branch names are ephemeral, meant to go away, but anyway, that is *my opinion*. The answer given is still valid, even if I question the value of having the branch name stored permanently in the repository. – Lasse V. Karlsen Oct 11 '18 at 10:13
  • My reason to remember which branch it came from - is that when I develop a feature or fix an issue (with a number) I make several changes that sometimes span over multiple commits - and later when I look at the history when working on something else - it helps know the frame of mind I was in, when I made this commit. For Example - let's say I'm fixing issue X with several commits. A your from now I need a different behavior, i find in the blame the commit message - and I want to be able to find the original issue to know why did I write that code that way. – Noam Oct 11 '18 at 11:52
  • In fact, I'll go further than @LasseVågsætherKarlsen: having the branch *name* stored in a commit provides a negative amount of information. Knowing that commit X was made on `hotfix` is *misleading* in the future, because in the future, `hotfix` is the name of the thing we were doing last week, not the thing we were doing last year. This is why Mercurial branches, which seem better than Git branches initially—certainly users like them more—are actually worse than Git branches in the end. – torek Oct 11 '18 at 15:43
  • @torek - usually my branch names represent the issue I was working on or the feature I was working on - so knowing that is useful for me – Noam Oct 24 '18 at 05:38
  • If your branch names are long and descriptive, that's probably good. If they're unique (e.g., an issue ID in some issue / feature tracking system like Jira), that's also reasonable. In practice, feature branch names tend to be something like `feature/keen`, which a year from now, nobody remembers what the heck that was. :-) – torek Oct 24 '18 at 06:21

2 Answers2

3

Yes there is a way to automatically add a prefix with the branch name to their comments. It can be acheived using the commit-msg hook. For example, if you write this code into the file .git/hook/commit-msg :

#!/bin/bash
current_branch="$(git rev-parse --abbrev-ref HEAD)"
echo '['"$current_branch"'] '$(cat "$1") > "$1"

All your commit messages will be automatically prefixed with "[branch_name]".

EDIT:

I actually managed to write the same script in for windows

In a file called .git/hooks/commit-msg.bat write this code (which is almost the equivalent of the three lines above):

@echo off

FOR /F "tokens=* USEBACKQ" %%F IN (`git rev-parse --abbrev-ref HEAD`) DO (
SET current_branch=%%F
)

FOR /F "tokens=* USEBACKQ" %%F IN (`type .git\COMMIT_EDITMSG`) DO (
SET commit_msg=%%F
)
SET p_commit_msg=[%current_branch%] %commit_msg%
ECHO %p_commit_msg% > .git\COMMIT_EDITMSG

In the file .git/hooks/commit-msg, call this script like that:

#!/bin/bash
cmd.exe /c "commit-msg.bat"
Romain Warnan
  • 1,022
  • 1
  • 7
  • 13
  • Can you translate that for windows :) I'm less of a bash style and more of a cmd \ visual studio user :) – Noam Oct 11 '18 at 11:56
  • 1
    I really don't know `cmd` I guess it should not be that different. There is also an `echo` function for sure ; there is probably a kind of `cat` function as well. For the rest you're the one who knows. – Romain Warnan Oct 11 '18 at 13:23
  • thanks - my problem is with `current_branch=` That is supposed to be an environment variable? I'm not sure how that is added automatically to my commit message - what is the command that tells that to git. – Noam Oct 11 '18 at 15:14
  • 1
    `current_branch` is a local variable, it is created as the value of the git command. `$1` is the commit message: the second line means `$1 = "[" + current_branch + "] " + $1` in another language. – Romain Warnan Oct 11 '18 at 15:35
  • 1
    To be more precise, `$1` is the name of the file that contains the commit message: so you replace its content by the original content prefixed by `"[current_branch"`. – Romain Warnan Oct 11 '18 at 15:44
  • 1
    Look at my answer, I find a way to do the same thing on Windows. – Romain Warnan Oct 11 '18 at 16:32
0

You can use the commit.template setting as discussed here.

$ git config commit.template /path/to/git-commit-template.txt

This will set the template for the current repository only. You can add the --global flag to apply to all repositories.

NOTE: it will be for all commits, not only for featureX branch, so after you finished work on this branch you need to edit the template.

In addition, if you forgot to add the branch name in the comment you can check here how to know after the merge which commits includes in the branch before the merge.

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114