← Back to PHP Course | Chapter 16: Testing & Tools | Lesson 4 of 10

PHP Git Integration

Git आपके project में किए गए हर change track करता है, एक save-game history जैसा। इसे PHP के साथ इस्तेमाल करना आपको किसी पुराने version पर वापस जाने और दूसरों के साथ safely काम करने देता है।

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
<?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";
?>

Repositories Initialize करना

आप PHP से programmatically एक .gitignore file बना सकते हैं या 'git init' चला सकते हैं, जो कभी-कभी नए projects automatically scaffold करने वाले tooling के लिए उपयोगी है।

उदाहरण: Initializing Repositories

php
<?php
// shell_exec('git init');
file_put_contents('.gitignore', "vendor/\n.env\n");
echo file_get_contents('.gitignore');
?>

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
<?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";
?>

Commits Automate करना

एक backup script modified files को stage और automatically एक schedule पर commit कर सकती है, आपको changes का एक audit trail देते हुए उस data के लिए भी जो आमतौर पर हाथ से version-controlled नहीं है।

उदाहरण: Automating Commits

php
<?php
// shell_exec('git add . && git commit -m "Automated backup"');
echo "A scheduled script could stage and commit changes automatically";
?>

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
<?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";
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. .env files या passwords जैसे secrets को repository में commit करना।
  2. composer.json और composer.lock के बजाय vendor folder commit करना।
  3. status check किए बिना git add . चलाना और unwanted files commit करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.