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.