0

When trying to capture git output stream to a file some of the output printed on the screen doesn't make it to the file.

to reproduce:

git show -s --oneline

the output on the console should be something similar to this:

1580ab93 (HEAD -> branch/name) some commit message

but when try to redirect the output to a file like this:

git show -s --oneline > file.txt

the output that get redirected to the file is just this:

1580ab93 some commit message

Can someone who knows the reason why this happens elaborate more and maybe suggest a solution or a workaround to be able to capture the full output from the command to my file.

Note: my final reach is to capture the output from a python script not just to a file.

Thanks

Karim Aly
  • 559
  • 5
  • 16
  • This works fine on my local Centos system. What environment are you running? – Raman Sailopal Nov 04 '20 at 19:01
  • @RamanSailopal windows 10 (git for windows) – Karim Aly Nov 04 '20 at 19:02
  • 1
    Interesting, I am getting the same results on Ubuntu. Output of `git show -s --oneline` is `2b202ce (HEAD -> master, origin/master, origin/HEAD) commit message` but the content of `tmp.txt` after `git show -s --oneline > tmp.txt` is `2b202ce commit message`. – Jonny Henly Nov 04 '20 at 19:17
  • The duplicate question does seem to answer the question. I was writing up an answer when this question got marked as a dup. I found that using the `--output=` option worked. For example: `git show -s --oneline --output="./tmp.txt"` – Jonny Henly Nov 04 '20 at 19:34
  • @JonnyHenly can you suggest how to use this flag to capture the command output in a python script ? – Karim Aly Nov 04 '20 at 19:39
  • @KarimAly try using the solution in the linked duplicate question (`--decorate=short` instead of `--output=`) with the answer to this SO question: [Use Git commands within Python code](https://stackoverflow.com/a/11115740/1241334) – Jonny Henly Nov 04 '20 at 19:50
  • 1
    @KarimAly So I believe the `process =`'s line would be `process = subprocess.Popen(['git', 'show', '-s', '--oneline', '--decorate=short'], stdout=PIPE, stderr=PIPE)`, but I'm not sure and I can't test it. You shouldn't need the `branch` variable in that answer. – Jonny Henly Nov 04 '20 at 19:53

0 Answers0