Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the From: address used for git format-patch?

Tags:

git

I'm generating some patches for an opensource project and my From: line is always [email protected]. I've been manually editing the From: line to be [email protected] before running git send-email.

  1. Is there anyway to automatically do this?
  2. Should I be doing this at all? I'm already using the envelope sender config.
  3. Will the author line in the upstream repo be "[email protected]" because I changed the From address?

Example:

From fab0cf45f10686688a8138f60a09505200cbb2a4 Mon Sep 17 00:00:00 2001
From: John Doe <[email protected]>
Date: Mon, 25 Feb 2013 23:06:23 -0500
Subject: [PATCH] nand: adjust erase/read/write partition/chip size for bad blocks

After considering mvp's answer, my explicit answers are:

  1. No, because...
  2. No, because...
  3. Yes, the From: line is taken from the Author of the commit because git am uses the From: line when setting the name of the author in the upstream repository.
like image 904
Harvey Avatar asked Dec 03 '25 18:12

Harvey


2 Answers

With git1.8.4 (July 2013), you now can set that "From field":

"git format-patch" learned "--from[=whom]" option, which sets the "From: " header to the specified person (or the person who runs the command, if "=whom" part is missing) and move the original author information to an in-body From: header as necessary.

See commit a90804752f6ab2b911882d47fafb6c2b78f447c3:

format-patch generates emails with the "From" address set to the author of each patch. If you are going to send the emails, however, you would want to replace the author identity with yours (if they are not the same), and bump the author identity to an in-body header.

Normally this is handled by git-send-email, which does the transformation before sending out the emails. However, some workflows may not use send-email (e.g., imap-send, or a custom script which feeds the mbox to a non-git MUA). They could each implement this feature themselves, but getting it right is non-trivial (one must canonicalize the identities by reversing any RFC2047 encoding or RFC822 quoting of the headers, which has caused many bugs in send-email over the years).

This patch takes a different approach: it teaches format-patch a "--from" option which handles the ident check and in-body header while it is writing out the email.
It's much simpler to do at this level (because we haven't done any quoting yet), and any workflow based on format-patch can easily turn it on.

Signed-off-by: Jeff King <[email protected]>

You now can do a:

git format-path --from=...

With:

--from::
--from=<ident>::

Use ident in the From: header of each commit email.
If the author ident of the commit is not textually identical to the provided ident, place a From: header in the body of the message with the original author.
If no ident is given, use the committer ident.

Note that this option is only useful if you are actually sending the emails and want to identify yourself as the sender, but retain the original author (and git am will correctly pick up the in-body header).
Note also that git send-email already handles this transformation for you, and this option should not be used if you are feeding the result to git send-email.


Update August 2016 (3 years later)

See commit 6bc6b6c (01 Aug 2016) by Josh Triplett (joshtriplett).
(Merged by Junio C Hamano -- gitster -- in commit db40a62, 10 Aug 2016)

format-patch: format.from gives the default for --from

This helps users who would prefer format-patch to default to --from, and makes it easier to change the default in the future.

git config man page now includes:

format.from:

Provides the default value for the --from option to format-patch.
Accepts a boolean value, or a name and email address.

  • If false, format-patch defaults to --no-from, using commit authors directly in the "From:" field of patch mails.
  • If true, format-patch defaults to --from, using your committer identity in the "From:" field of patch mails and including a "From:" field in the body of the patch mail if different.
  • If set to a non-boolean value, format-patch uses that value instead of your committer identity.

Defaults to false.


Note that, with Git 2.26 (Q1 2020), spaces are better managed.

See commit f696a2b, commit ffbea18, commit f447d02, commit b6537d8 (11 Feb 2020) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit d880c3d, 17 Feb 2020)

mailinfo: be more liberal with header whitespace

Signed-off-by: Jeff King

RFC822 and friends allow arbitrary whitespace after the colon of a header and before the values.
I.e.:

Subject:foo
Subject: foo
Subject:  foo

all have the subject "foo". But mailinfo requires exactly one space.
This doesn't seem to be bothering anybody, but it is pickier than the standard specifies. And we can easily just soak up arbitrary whitespace there in our parser, so let's do so.

Note that the test covers both too little and too much whitespace, but the "too much" case already works fine (because we later eat leading and trailing whitespace from the values).


With Git 2.38 (Q3 2022), "git format-patch --from=<ident>"(man) can be told to add an in-body From: line even for commits that are authored by the given <ident> with --force-in-body-from"option.

See commit d5fc07d, commit 34bc1b1, commit b84d013 (29 Aug 2022) by Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster -- in commit 0e2a476, 09 Sep 2022)

format-patch: allow forcing the use of in-body From: header

Users may be authoring and committing their commits under the same e-mail address they use to send their patches from, in which case they shouldn't need to use the in-body From: line in their outgoing e-mails.
At the receiving end, "git am"(man) will use the address on the From: header of the incoming e-mail and all should be well.

Some mailing lists, however, mangle the From: address from what the original sender had; in such a situation, the user may want to add the in-body "From:" header even for their own patches.

git format-patch --[no-]force-in-body-from` was invented for such users.

git format-patch now includes in its man page:

--[no-]force-in-body-from

With the e-mail sender specified via the --from option, by default, an in-body "From:" to identify the real author of the commit is added at the top of the commit log message if the sender is different from the author.

With this option, the in-body "From:" is added even when the sender and the author have the same name and address, which may help if the mailing list software mangles the sender's identity.


With Git 2.47 (Q4 2024), batch 9, "git send-email"(man) learned --translate-aliases option that reads addresses from the standard input and emits the result of applying aliases on them to the standard output.

See commit c038a6f, commit bbc04b0, commit 16d89aa (13 Aug 2024) by Jacob Keller (jacob-keller).
(Merged by Junio C Hamano -- gitster -- in commit 3dd2a2f, 26 Aug 2024)

send-email: teach git send-email option to translate aliases

Signed-off-by: Jacob Keller

git send-email(man) has support for converting shorthand alias names to canonical email addresses via the alias file.
It supports a wide variety of alias file formats based on popular email program file formats.

Other programs, such as b4, would like the ability to convert aliases in the same way as git send-email without needing to re-implement the logic for understanding the many file formats.

Teach git send-email a new option, --translate-aliases, which will enable this functionality.
Similar to --dump-aliases, this option works like a new mode of operation for git send-email.

When run with --translate-aliases, git send-email reads from standard input and converts any provided alias into its canonical name and email according to the alias file.
Each expanded name and address is printed to standard output, one per line.

git send-email now includes in its man page:

--translate-aliases

Instead of the normal operation, read from standard input and interpret each line as an email alias. Translate it according to the configured alias file(s). Output each translated name and email address to standard output, one per line. See 'sendemail.aliasFile' for more information about aliases.

like image 53
VonC Avatar answered Dec 06 '25 09:12

VonC


Setting your git identity should be enough:

git config --global user.email [email protected]
git config --global user.name "John Doe"

You can also set your identity per git repository: run these commands inside that git repostory and simply omit --global flag.

You probably should not use [email protected] because if someone git am it, this commit will have not your identity.

Probably the only reason to do that is to avoid spam to your primary address - but I think you will get it anyway as soon as it becomes public.

like image 44
mvp Avatar answered Dec 06 '25 08:12

mvp