How to get git producing output to a file?

27,999

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

Share:
27,999
derHugo
Author by

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

Comments

  • derHugo
    derHugo over 1 year

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

    git 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 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 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
      ridgy over 6 years
      Did you try git clone https://github.com/someRepository > git_clone.file 2>&1?
    • derHugo
      derHugo over 6 years
      @ridgy yes, please see my EDIT
    • dessert
      dessert over 6 years
      >file 2>&1 equals &>file in modern bash, in case someone wonders.
  • derHugo
    derHugo over 6 years
    Both only gave me the very first line: tail git_clone.file -> Cloning to 'someRepository' ...
  • derHugo
    derHugo over 6 years
    I put my script onto gitsts.github.com as example. (see the BACKGROUND INFORMATION I added to my question)
  • derHugo
    derHugo over 6 years
    Hm 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 tried git 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
    derHugo over 6 years
    Thanks 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 since git only writes to it instead of stdout
  • psusi
    psusi over 6 years
    When you redirect stderr to a file, it is now attached to that file, and not a terminal.
  • ling
    ling about 5 years
    That's a lot more verbose, but at least it works. Thanks.
  • frank_108
    frank_108 over 2 years
    This works, if we want to keep the history as kind of log, it can end to &>> git_clone.file