How to get git producing output to a file?
Solution 1
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
Solution 2
git clone
uses stderr
for the output, so just write that to the file:
git clone https://github.com/someRepository 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 https://github.com/someRepository &>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 https://github.com/someRepository | 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?.
derHugo
I am SoftwareDeveloper and have a Master in "Human-Computer-Interaction". I learned a lot from this community and try my best to contribute back. I do this mostly from the phone so sometimes you need a bit of patience until I can actually test my ideas myself 😁 For work I mostly use Unity and c# making applications for Mixed-Reality glasses. But during my studies also got at least basic knowledge in a lot other fields.
Updated on September 18, 2022Comments
-
derHugo over 1 year
I wanted to write the output of
git clone
to a file usinggit clone https://github.com/someRepository > 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 realizedgit
doesn't even seem to produce the output tostdout
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
(andstdout
) did not solve the problem.git clone https://github.com/someRepository 2> git_clone.file git clone https://github.com/someRepository &> git_clone.file git clone https://github.com/someRepository > 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.
-
ridgy over 6 yearsDid you try
git clone https://github.com/someRepository > git_clone.file 2>&1
? -
derHugo over 6 years@ridgy yes, please see my EDIT
-
dessert over 6 years
>file 2>&1
equals&>file
in modernbash
, in case someone wonders.
-
-
derHugo over 6 yearsBoth only gave me the very first line:
tail git_clone.file
->Cloning to 'someRepository' ...
-
derHugo over 6 yearsI put my script onto gitsts.github.com as example. (see the BACKGROUND INFORMATION I added to my question)
-
derHugo over 6 yearsHm I don't understand you 100% but I think it is still the same problem also with the pipe. I tried now
git clone XYZ |& tee git_clone.file
but I get the same "oneliner" result. I also triedgit clone -v ...
and this produces some more lines but the "interresting part" still is not written. Since the script will most of the type be busy downloading or uploading stuff during git ofcourse it would be most neccessary to show the progressbar during those times .. I'm after this like 3 weeks now ^^ -
derHugo over 6 yearsThanks a lot for your time and effort! I just found the solution (see my answer). Anyway I wouldn't have figured this out without knowing that I anyway have to get
stderr
sincegit
only writes to it instead ofstdout
-
psusi over 6 yearsWhen you redirect stderr to a file, it is now attached to that file, and not a terminal.
-
ling about 5 yearsThat's a lot more verbose, but at least it works. Thanks.
-
frank_108 over 2 yearsThis works, if we want to keep the history as kind of log, it can end to &>> git_clone.file