Search code examples
bashgitgithubtags

Use a multiline variable in git tag message preserving line breaks


I have a multiline variable $foo, and want to use it to git tag my latest commit. I would like those line breaks to show up in git (/github). I tried

git tag -a v$MAJVER.$MINVER.$PATVER.$HOTVER -m $"$foo"

but it shows up like this, so without line breaks rendering

How can I make it (via bash) such that line breaks survive the push?


Solution

  • Just use simple double-quoting. If that isn't working, it's something in the value.

    If what's concerning you is the output of the commit, that is just a report.
    Go look at the actual log.

    $: ls -l
    total 4
    -rwxr-xr-x 1 P2759474 1049089 474 Nov  2 14:22 tst
    
    $: mv tst new
    $: git status
    On branch tst
    Changes not staged for commit:
      (use "git add/rm <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
            deleted:    tst
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            new
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    $: foo="
        testing
        multiline
        git
        commit
        message
    "
    
    $: git add .
    warning: LF will be replaced by CRLF in new.
    The file will have its original line endings in your working directory
    
    $: git commit -m "$foo" # keeps newlines, but shows without them
    [tst 74428e6]     testing     multiline     git     commit     message
     1 file changed, 0 insertions(+), 0 deletions(-)
     rename tst => new (100%)
    
    $: git log -1
    commit 74428e60d830c417ded3ad2c331f02e73319b862 (HEAD -> tst)
    Author: Paul Hodges <paul.hodges@redacted.com>
    Date:   Thu Nov 3 10:20:33 2022 -0500
    
            testing
            multiline
            git
            commit
            message
    

    The same works with tags, but you have to know how to get the data back out.

    $: git tag -a example -m "$foo"
    
    $: git tag -l
    example
    
    $: git tag -n
    example             testing
    
    $: git tag -n99
    example             testing
            multiline
            git
            commit
            message
    

    You have to tell it how many lines of the message you are willing to have it show you. The default is 1.