PHP Git Integration
In this page:
Git का Introduction
Git एक version control system है जो समय के साथ आपकी codebase में हर change track करता है; PHP scripts deploy करने से पहले working tree clean है यह verify करने जैसे checks automate करने के लिए Git commands को shell out कर सकती हैं।
उदाहरण: Introduction to Git
<?php
// Declare `$status`, set to `shell_exec('git status --porcelain 2>&1')`
$status = shell_exec('git status --porcelain 2>&1');
// Print `$status === '' ? "Working tree is clean" : "There are uncommitted changes"` to the output
echo $status === '' ? "Working tree is clean" : "There are uncommitted changes";
?>
Login to try C/C++/Java/PHP code in the editor
Repositories Initialize करना
आप PHP से programmatically एक .gitignore file बना सकते हैं या 'git init' चला सकते हैं, जो कभी-कभी नए projects automatically scaffold करने वाले tooling के लिए उपयोगी है।
उदाहरण: Initializing Repositories
<?php
// shell_exec('git init');
file_put_contents('.gitignore', "vendor/\n.env\n");
echo file_get_contents('.gitignore');
?>
Login to try C/C++/Java/PHP code in the editor
Git Commands चलाना
shell_exec() एक PHP script को arbitrary shell commands चलाने देता है, Git commands सहित -- लेकिन shared hosting पर आपको errors carefully suppress और check करने चाहिए, क्योंकि shell access अक्सर restricted या risky होती है।
उदाहरण: Running Git Commands
<?php
// Declare `$output`, set to `shell_exec('git --version 2>&1')`
$output = shell_exec('git --version 2>&1');
// Print `$output !== null ? trim($output) : "shell_exec unavailable or restricted"` to the output
echo $output !== null ? trim($output) : "shell_exec unavailable or restricted";
?>
Login to try C/C++/Java/PHP code in the editor
Commits Automate करना
एक backup script modified files को stage और automatically एक schedule पर commit कर सकती है, आपको changes का एक audit trail देते हुए उस data के लिए भी जो आमतौर पर हाथ से version-controlled नहीं है।
उदाहरण: Automating Commits
<?php
// shell_exec('git add . && git commit -m "Automated backup"');
echo "A scheduled script could stage and commit changes automatically";
?>
Login to try C/C++/Java/PHP code in the editor
Safe Environment Checks
एक live, internet-facing script से Git commands चलाना dangerous है जब तक carefully locked down न हो -- एक exposed .git folder या एक unguarded shell_exec() endpoint आपकी पूरी source history किसी attacker को leak कर सकता है।
उदाहरण: Safe Environment Checks
<?php
// Declare `$path`, set to ".git/config"
$path = ".git/config";
// Check whether `file_exists($path)`
if (file_exists($path)) {
// Print "Warning: .git folder should never be publicly reachable" to the output
echo "Warning: .git folder should never be publicly reachable";
// Otherwise, run this branch
} else {
// Print "No exposed .git folder found here" to the output
echo "No exposed .git folder found here";
}
?>
Login to try C/C++/Java/PHP code in the editor
.envfiles या passwords जैसे secrets को repository में commit करना।composer.jsonऔरcomposer.lockके बजायvendorfolder commit करना।- status check किए बिना
git add .चलाना और unwanted files commit करना।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: