Diego
Diego

Reputation: 832

git commit -a --file message.txt; git format-patch removes newlines from message.txt

When using a text file for a commit message git commit -a --file message.txt and then generating a patch file with git format-patch the commit message get written with all newlines stripped (Linux).

Seems like the culprit is the format-patch

By default, the subject of a single patch is "[PATCH] " followed by the concatenation of lines from the commit message up to the first blank line (see the DISCUSSION section of git-commit[1]).

I could not find yet the way to change this default behaviour. Is there a way?

How can I change it to take the commit message verbatim from the message.txt and put it into the generated 0001-xxx.patch file?

so from

* change 1
* change 2

it goes to

* change 1 * change 2

when running git format-patch and I obviously want it to keep the newlines.

This is how I am checking the result:

less message.txt
git commit -a --file message.txt
git format-patch -1 my_branch
less *.patch

To clarify and confirm, the git show shows the commit message with intact newlines. I am using Linux term.

EDIT: rewritten the question to reflect the format-patch at the core of the problem

EDIT2: I have found a workaround: in case there is an empty line after the first line in the message.txt the rest of the file format is preserved.

So this is how I have to format the message.txt

 first line 
 empty line
 * change 1
 * change 2

With this I have solved my immediate problem but not sure if this is the right thing to do.

Upvotes: 3

Views: 759

Answers (3)

nick.wang
nick.wang

Reputation: 11

You can use the option "-k" for both "git format-patch" and "git am" commands.

For example:

  git format-patch master^..master -k 
  git am -k XXX.patch

Upvotes: 1

bk2204
bk2204

Reputation: 76409

Git doesn't offer an option to change this. git format-patch needs a subject for the patches it formats, since they are essentially email messages. As such, it has to assume some format for the commit messages.

The standard Git format for commit message is that there is a short summary on the first line, a blank line, and then an optional (but recommended) longer description, optionally with trailers separated by a blank line. There are many, many tools, including parts of Git, that assume this format, and I highly recommend that you follow it.

The "by default" text you mentioned above means that you can use the --subject-prefix option to customize the [PATCH] portion of the message, not the selection of the subject portion of the email.

Upvotes: 5

CodeWizard
CodeWizard

Reputation: 141946

You can use commit templates.

I don't know if that suitable for you since you might need a different message per commit but still useful if not others can benefit from that as well.

git commit.template

  • create a commit template file with your content

    # set the commit template in your global git config
    git config --global commit.template <.git-commit-template.txt file path>
    
  • Now your ~/.gitconfig should contain:

    [commit]
        template = <.git-commit-template.txt file path>
    
  • If you wish to use empty commit

    # If you allow empty commit messages set
    git config --global commit.cleanup strip
    

--cleanup=<mode>

This option sets how the commit message is cleaned up.
The <mode> can be one of verbatim, whitespace, strip, and default.

The default mode will strip leading and trailing empty lines and #commentary from the commit message only if the message is to be edited. Otherwise only whitespace removed.

-------------------------------------------------------------------
| Mode         | Description                                      |
---------------|--------------------------------------------------|
| verbatim     | does not change message at all,                  |
| whitespace   | removes just leading/trailing whitespace lines   |
| strip        | removes both whitespace and commentary.          |
-------------------------------------------------------------------

Upvotes: 0

Related Questions