I have a php script that runs on one server, which includes an exec() of an ssh command to another server. That ssh command runs a script which includes git commands. The git commands in the remote file, executed on the remote server, seem to be affecting the server that originated the ssh command, leaving the originating server on the wrong git branch.
I have created two scripts to minimally reproduce this. The first script git_ssh_bug_repro_1.php
prints out what its hostname and git branch are, then runs an exec()'ed ssh command of the second script on a different server (sudo ssh root@test-1 'php /path/git_ssh_bug_repro_2.php'
) and print_r's the output of that exec, then prints out again what its hostname and git branch are afterwards. The second script also begins by printing its hostname and current branch, then does git checkout testing
, then prints out its hostname and branch again. The output of these two scripts running together as described:
root@abc-1:/path# php git_ssh_bug_repro_1.php
BEFORE running ssh command:
COMMAND: git rev-parse --abbrev-ref HEAD
Hostname: abc-1, current git branch: master
COMMAND: sudo ssh root@test-1 'php /path/git_ssh_bug_repro_2.php'
Password authentication is disabled to avoid man-in-the-middle attacks.
Keyboard-interactive authentication is disabled to avoid man-in-the-middle attacks.
Switched to branch 'testing'
OUTPUT:
Array
(
[0] => master
[1] => BEFORE running git checkout command:
[2] => COMMAND: git rev-parse --abbrev-ref HEAD
[3] => Hostname: test-1, current git branch: master
[4] => COMMAND: git checkout testing
[5] => OUTPUT:
[6] => Array
[7] => (
[8] => [0] => master
[9] => [1] => Your branch is ahead of 'origin/testing' by 21 commits.
[10] => )
[11] => AFTER running git checkout command:
[12] => COMMAND: git rev-parse --abbrev-ref HEAD
[13] => Hostname: test-1, current git branch: testing
)
AFTER running ssh command:
COMMAND: git rev-parse --abbrev-ref HEAD
Hostname: abc-1, current git branch: testing
root@abc-1:/path#
SUMMARY: Why do git commands that are being run by a script on one server affect the git status of another server (in this case, causing it to switch to the wrong branch), just because said script was fired off from the other server via ssh? And how can I make it not do that so the originating server's git branch is not changed or otherwise tampered with?