繁体   English   中英

如何使用 git 拉取数据 使用 php exec

[英]How to use git pull to pull data using php exec

我试图拉的代码是

shell_exec('git init');
$d = shell_exec('git fetch origin master');
$output = shell_exec('git pull origin master');

但它没有按预期工作。 有时它工作,有时它不工作。

我在我的 git 帐户中启用了双因素身份验证。 你觉得是什么原因?

还是需要做其他事情来提取数据?

您编写的代码在当前工作目录中创建一个新存储库,然后尝试从origin远程获取/拉取,但尚未配置任何内容。

你需要添加类似于shell_exec("git remote add origin GIT_URL"); 其中GIT_URL是您的存储库远程 url


您也没有进行任何错误检查,也许可以尝试使用exec systemproc_*来检测故障并在需要时中止

这应该工作,使用czproject/git-php及其pull function

$repo = GitRepository::init('gitrepo');
$repo->pull('https://url/remote/repo', array('master')); 

这样,您可以明确指定远程 URL。

使用system()而不是 shell_exec

你也可以做一个

$escaped_cmd = escapeshellcmd($command);
system($escaped_cmd);

如果你偏执

在开始 fetch 之前,您应该有origin (远程存储库)。在本地机器中的存储库上初始化后,您错过了git remote add origin URL命令。 此外,如果您在本地处理中创建了新的空存储库,则需要先克隆现有的远程存储库。 这是从远程服务器克隆存储库的方法。

$repo = 'YOUR_REPOSITORY_URL_HERE';
shell_exec('git clone '.$repo);
shell_exec('git remote add origin '.$repo);
$d = shell_exec('git fetch origin master');
$output = shell_exec('git pull origin master');

确保存储库不是私有的,否则您将需要ssh来连接远程服务器。

我建议您不要通过重新发明轮子来 go,您可以使用git-php库使代码无忧无虑。

我不想表现得像拖钓,但是: 第一:学习 git 的工作流程 - 你需要克隆你的存储库,然后是 checkout master,然后是 git pull。 如果你已经这样做了 - 你不能再使用“git clone”了,只签出 master 并做 git pull。 第二:您需要将 ssh 密钥设置为 PHP - 我不会那样做,但那是你的事。 使用 2 因素身份验证与您尝试解决的问题无关。 Simply use the SSH URL from Github to clone your repository and make sure, that PHP can access your SSH Key so it can authenticate to github.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM