# Troubles redirecting git output

3 messages from 2021-07-09 to 2021-07-09. Participants: Baptiste Chocot, Marc Branchaud.
Thread: https://gitlist.dev/t/56081

## Baptiste Chocot, 2021-07-09 11:59

Subject: Troubles redirecting git output
Message-ID: <57ebeb1d-f467-6cbd-5ad9-a55cf04dde34@gmail.com>
URL: https://gitlist.dev/e/57ebeb1d-f467-6cbd-5ad9-a55cf04dde34%40gmail.com

```
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
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


```

## Marc Branchaud, 2021-07-09 13:40

Subject: Re: Troubles redirecting git output
Message-ID: <87f4a68e-3a74-eddf-c4ad-e6674d9f7994@xiplink.com>
URL: https://gitlist.dev/e/87f4a68e-3a74-eddf-c4ad-e6674d9f7994%40xiplink.com
In-Reply-To: <57ebeb1d-f467-6cbd-5ad9-a55cf04dde34@gmail.com>

```
On 2021-07-09 7:59 a.m., Baptiste Chocot wrote:
> 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.


> 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
> 

```

## Baptiste Chocot, 2021-07-09 19:20

Subject: Re: Troubles redirecting git output
Message-ID: <356c442a-5b21-39ed-ca58-dc9c67727089@gmail.com>
URL: https://gitlist.dev/e/356c442a-5b21-39ed-ca58-dc9c67727089%40gmail.com
In-Reply-To: <87f4a68e-3a74-eddf-c4ad-e6674d9f7994@xiplink.com>

```
Marc,

Thanks for your help... :facepalm: I look like a noob but at least I've 
learnt something useful.

Have a good day!

On 7/9/21 9:40 AM, Marc Branchaud wrote:
> On 2021-07-09 7:59 a.m., Baptiste Chocot wrote:
>> 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.
>
>
>> 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
>>

```
