How can git commands like git add .
and git commit -m
be executed using php?
It is for a project that creates a centralized repository facility for maintaining academic projects.
exec() didn't seem to work.
<?php
$path = "/var/www/repos/$_POST[project]";
$a='';
chdir($path);
exec("git add .");
exec("git commit -m'message'");
echo "<h3 align = center> Succesfully commited all the files.</h3>";
?>
PHP allows you to create web apps that run pretty much anywhere, including on cheap hosting. Instead, git allows you to keep track of the changes in your source code. You can use Git in your PHP projects to leverage the best of both.
All you have to do is load Command Prompt (Load the Start menu, then click "Run", type cmd and hit enter), then you can use Git commands as normal.
The mkdir (make directory) command in the Unix, DOS, DR FlexOS, IBM OS/2, Microsoft Windows, and ReactOS operating systems is used to make a new directory. It is also available in the EFI shell and in the PHP scripting language. In DOS, OS/2, Windows and ReactOS, the command is often abbreviated to md .
It is definetly possible. I implemented it in one of my projects. However you should be careful about permissions.
On linux, usually, the exec
command will execute using the www-data
user.
So you should allow www-data
to write and read on your work directory.
One quick and dirty way to do it is : chmod o+rw -R git_directory
There is another solution, which is to simply use php backticks like so
$message = `git log -1 --pretty=%B`; // get commit message
or
$deploy_tag = `git describe --tags`; // get commit tags
or
$hash = `git log -1 --pretty=%h`; // get the hash
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With