Home > Software engineering >  Why does only part of git console output go into log file?
Why does only part of git console output go into log file?

Time:09-06

On Windows, I have a bat file that creates a git commit like so:

@echo off
echo.
echo -------------------------

cd C:\Repository\Other\notes-repo
git add --all
git commit -m "autoCommit"
git push
cd C:\Repository\Other

I call it from the command line like this:

note_repo_sync.bat >> log.txt 2>&1

When there are changes to the repo, then this gets added to the log file:

-------------------------

[main 013c443] autoCommit
 1 file changed, 1 deletion(-)
To https://github.com/raelb/notes-repo.git
   6bceead..013c443  main -> main

However, if I omit the 2>&1, i.e. print the stderr to console, then much more is shown:

C:\Repository\Other>notes_repo_sync.bat >> log.txt
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 290 bytes | 290.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/raelb/notes-repo.git
   28a2bf3..691fb4f  main -> main

Why are the extra lines (Enumerating objects etc..) not included in the log when I append 2>&1 to the command line?

CodePudding user response:

Both git fetch and git push write their progress information (--progress output, suppressed with --no-progress) to standard error. Progress output is suppressed by default when stderr is not connected to a terminal.1

There's no particularly good reason for this, other than "Git used to do this for historical reasons, so Git still does this for historical reasons".


1There are, or were, further bugs where explicit --progress didn't always work to restore the stderr output even when stderr is connected to something other than a terminal.

  • Related