1

I'm trying to get git show-branch to tell me if a branch exists using Window 7 cmd, for that I've set a .bat file and I'm trying to filter the git command output, and I know that git outputs to STDERR.

for /f %%L in ('git show-branch remotes/origin/develop-7.x 2^> nul ^| findstr /b "fatal"') do set VAR=%%L echo %VAR%

My git command output is this: fatal: bad sha1 reference remotes/origin/develop-7.x

The problem is that I can't get to pipe STDERR to findstr and VAR to give me something at all.

If I add &1 to 2^>&1 1>nul I get an error & was unexpected at this time

Deses
  • 1,074
  • 2
  • 12
  • 25
  • 1
    ```2^>^&1``` the ampersand must also be escaped –  Jun 21 '18 at 20:23
  • Goodness... that was simple. *facepalm*. Thank you very much! Post it as an answer so I can mark the question as asked! :) – Deses Jun 21 '18 at 20:29
  • I'd suggest you use a different method of determining if a branch exists: `git rev-parse --verify `. See [here](https://stackoverflow.com/q/5167957) for reference. Additionally with reference to the edited footnote, I don't understand it! Here's why; If you send the `StdErr` and `StdOut` both to `Nul`, nothing will pipe to `FindStr` and the metavariable, `%%L`, will never be propagated! – Compo Jun 21 '18 at 23:43
  • Yeah, it's weird, but `2>&1 1>nul` works, tho! I also would like some explanation about that. – Deses Jun 22 '18 at 00:20

1 Answers1

2

2^>^&1 - the ampersand must also be escaped