1

I am using subprocess module in python to checkout to a particular commit. Below is the sample code.

commit_id = '305efe41dc9e9bfa375781fa2b69dd979dcaa7f1'
process = subprocess.Popen(["git", "checkout", commit_id], stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
out, err = process.communicate()

When I print out there is no output whereas when I print err I see the following message --

b"Note: switching to '305efe41dc9e9bfa375781fa2b69dd979dcaa7f1'.\n\nYou are in 'detached HEAD' state. You can look around, make experimental\nchanges and commit them, and you can discard any commits you make in this\nstate without impacting any branches by switching back to a branch.\n\nIf you want to create a new branch to retain commits you create, you may\ndo so (now or later) by using -c with the switch command. Example:\n\n  git switch -c <new-branch-name>\n\nOr undo this operation with:\n\n  git switch -\n\nTurn off this advice by setting config variable advice.detachedHead to false\n\nHEAD is now at 305efe41 checking out back to current branch after operation complete\n"

Whereas when I do a git branch I see that my HEAD has changed to 305efe41dc9e9bfa375781fa2b69dd979dcaa7f1.

If I further checkout to other commit ids from this current detached head it still works, but the messages are in err instead of out as before.

Why am I getting messages in stderr instead of stdout? Am I doing something wrong?

My objective is to write some clean code and avoid possible hacks --

if not err:
    // do something

INSTEAD OF 

if err:
   // do something as the checkout is working but for some reason the out is in err

NOTE: I am aware that there are git libraries that I can directly use instead of doing things manually using subprocess but for some reason I cannot use them.

amitection
  • 2,696
  • 8
  • 25
  • 46
  • https://stackoverflow.com/search?q=%5Bgit%5D+stderr – phd Apr 08 '20 at 12:13
  • 1
    You can check the exit code of the command. – krisz Apr 08 '20 at 12:21
  • @krisz Yeah should have checked for the exit code. Nevertheless I was more interested in the reason behind it and found it here https://stackoverflow.com/questions/37561240/why-does-git-submodule-add-write-to-stderr-rather-than-stdout/37561629#37561629 – amitection Apr 08 '20 at 16:10

0 Answers0