git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Troubles redirecting git output

From
Marc Branchaud <marcnarc@xiplink.com>
Date
Jul 9, 2021, 13:40 UTC
Message-ID
<87f4a68e-3a74-eddf-c4ad-e6674d9f7994@xiplink.com>
In-Reply-To
<57ebeb1d-f467-6cbd-5ad9-a55cf04dde34@gmail.com>
On 2021-07-09 7:59 a.m., Baptiste Chocot wrote:
Show 10 quoted lines
> Hello guys, hope you're doing great.
> 
> I'm reaching out today because I cannot find a fix for my issue. I'm 
> using git 2.25.1 from the debian repo. I'm having difficulties to 
> redirect git stderr, here is an example:
> 
> ---
> $ git init
> Initialized empty lol/.git/
> $ git branch >&2 2>/dev/null
Try swapping the two redirections:
	git branch 2>/dev/null >&2
I believe that standard shell behaviour is to apply a redirection 
immediately when it appears while parsing the command.  I believe that 
although the man pages say "redirect" internally the code just 
*replaces* the old file descriptor, e.g. basically something equivalent to
	stdout = 2
So when you say
	git branch >&2 2>/dev/null
The shell sees
">&2" -- The shell redirects stdout to file descriptor 2.

"2>/dev/null" -- The shell opens /dev/null (as descriptor 3) and *replaces* descriptor 2 with the new descriptor. But at this point, stdout is still being sent to the original descriptor 2.

When I redirect, I'm in the habit of setting stdout first, then set 
stderr to stdout. The numerical ordering helps me avoid this mistake:
	git branch >/dev/null 2>&1
		M.
Show 11 quoted lines
> fatal: your current branch 'master' does not have any commits yet
> ---
> 
> Is this an expected behavior? I find it weird not to be able to silent 
> git. Are you aware of a workaround? I've been browsing a bit but did not 
> find an answer yet.
> 
> Thanks for your time!
> 
> Baptiste
> 
Previous: Baptiste ChocotNext: Baptiste Chocot
Message 2 of 3 in “Troubles redirecting git output”
  1. Baptiste ChocotJul 9, 2021
  2. Marc BranchaudJul 9, 2021
  3. Baptiste ChocotJul 9, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.