Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

How to get git producing output to a file?

Writer Matthew Barrera

I wanted to write the output of git clone to a file using

git clone > git_clone.file

But instead I get the output displayed/updated in the terminal like

Cloning to 'someRepository' ...
remote: Counting objects: 2618, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 2618 (delta 2), reused 12 (delta 1), pack-reused 2603
Received objects: 100% (2618/2618), 258.95 MiB | 4.39 MiB/s, Done.
Resolving Differences auf: 100% (1058/1058), Done.
Check Connectivity ... Done.

But the file git_clone.file is generated but remains empty.

My original goal was to bypass the output of git into a function (see my question here). But now I realized git doesn't even seem to produce the output to stdout really but somehow different since nothing is written to the file.

How can I get this displayed output from git in order to redirect it to a file/function?


EDIT

The proposed redirection of stderr (and stdout) did not solve the problem.

git clone 2> git_clone.file
git clone &> git_clone.file
git clone > git_clone.file > 2>&1

all gave me the same result: only the line

Cloning to 'someRepository' ...

appears in git_clone.file


BACKGROUND INFORMATION

Why do I need this?

As explained in my other question here I wrote a custom progress bar always at the bottom of the output my scripts. (I use it in multible scripts but) The script in this case migrates a lot of (until now 107) git repositories from github to our own Gitlab-Server and repairs the Git LFS support which usually is lost without it.

So I would like to still see all the output of git but also would like to have my progress bar working at the bottom of the output in the terminal.

2

2 Answers

Thanks for your help!


I just have found the solution:

Part 1

(Thanks to the answer of dessert)
git by design does never write to stdout but stderr. So I needed to redirect stderr, too, in order to get the output using

git clone XYZ &> git_clone.file

Part 2

Anyway this wasn't enough and I only received the "uninteresting" part of the output to the file but not the lines of the progress I really wanted.

Doing further reserach again in man git-clone I realized there exists an option

--progress progress status is reported on the standard error stream by default when it is attached to a terminal, unless -q is specified. This flag forces progress status even if the standard error stream is not directed to a terminal.

Though I'ld think it actually was already attached to a terminal, this now seems to force git to write the lines of the progress part I'm most interested in finally to stderr as well so I can now get them using

git clone --progress XYZ &> git_clone.file
2

git clone uses stderr for the output, so just write that to the file:

git clone 2>git_clone.file

Alternatively you could redirect both stdout and stderr – that's not necessary in this particular, but this way you make sure every output produces by the command gets redirected:

git clone &>git_clone.file

In the case of git clone obviously there's a different output if you redirect it, the whole progress information running through in the terminal is not included in an output file. That's by design and IIRC you can't easily change that behaviour directly, however if you need the output in another script you may very well pipe it to it, which works just fine and gives you all the output:

git clone | cat

Inside your script you can get stdin with -, e.g. cat - to print stdin to stdout – see here for more: How to write a script that accepts input from a file or from stdin? and How to read from a file or stdin in Bash?.

4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy