From 39919e82a73204d750eeb1560c08b7f7d989c2ee Mon Sep 17 00:00:00 2001 From: Parsa Aghayi Date: Mon, 2 Nov 2026 16:52:05 +0000 Subject: [PATCH] feat(blog): publish comprehensive battle-tested Git engineering article --- .../seeders/Blog/RealBlogContentSeeder.php | 190 ++++++++++++++++++ 1 file changed, 190 insertions(+) diff --git a/database/seeders/Blog/RealBlogContentSeeder.php b/database/seeders/Blog/RealBlogContentSeeder.php index 31a71df..bda1c2d 100644 --- a/database/seeders/Blog/RealBlogContentSeeder.php +++ b/database/seeders/Blog/RealBlogContentSeeder.php @@ -1323,6 +1323,88 @@ A: HTTP/3 uses QUIC over UDP, reducing head-of-line blocking and recovering fast TEXT, ], + [ + 'title' => 'Git from a Veteran Developer: Mental Models, Battle Scars, and Habits That Saved My Work', + 'slug' => 'git-mental-model-and-battle-tested-habits', + 'excerpt' => 'Git is not a bag of incantations to memorize for an interview. When I realized Git stores snapshots of files rather than diffs, my fear disappeared. Here are the practical lessons from ten years of botched rebases, atomic commits, and rescuing vanished code.', + 'category' => 'engineering-practices', + 'tags' => ['git', 'clean-code'], + 'cover' => 'Git Mental Models and Habits', + 'custom_cover' => 'https://parsaaghayi.ir/images/blog/posts/git-craft.png', + 'views' => 2410, + 'likes' => 189, + 'days_ago' => 1, + 'body' => <<<'TEXT' +# Git from a Veteran Developer: Mental Models, Battle Scars, and Habits That Saved My Work + +Early in my career, whenever the terminal flashed `CONFLICT (content): Merge conflict in...`, my pulse jumped to 120. I would copy the folder onto my desktop, run `git stash` with fingers crossed, and hope nothing vanished into thin air. If you have ever kept a folder named `project_final_v2_really_final`, know that you are not alone. Almost every seasoned engineer went through that rite of passage. + +The trouble is that most tutorials treat Git like magical spells: "Type this incantation, then that one, then push." Until you understand what is actually going on under the hood, any unexpected edge case will freeze you. Here is the realistic mental model and the habits that kept my sanity across years of production work. + +--- + +## 1. The Mental Model that Clicked: Git Does Not Store Diffs + +The single biggest breakthrough in understanding Git is realizing that **Git does not store differences between files (deltas); it stores full snapshots of your project.** + +Think of each commit as a Polaroid picture of your entire project at that exact moment: +* If a file has not changed, Git does not duplicate it; it simply points back to the existing file in its internal object database. +* The commit SHA-1 hash is an immutable fingerprint of the tree plus the hash of the parent commit. This graph structure makes history durable and auditable. + +Once you see this, you understand what a **branch** really is. A branch is not a duplicated folder of code! In Git, a branch is merely a 41-byte text file under `.git/refs/heads/` that holds the 40-character commit hash it points to. Creating or deleting a branch is instantaneous and essentially free. + +--- + +## 2. Atomic Commits: Saving Yourself at 2 AM + +A common trap is waiting until the end of the sprint to dump all work into one giant commit: +```bash +git commit -m "fixed issues and updated design and bugfix" +``` +When a production regression strikes two days later, you cannot isolate or revert that single bug fix without undoing three other unrelated features. + +The rule of **atomic commits**: +* Each commit does one focused thing and does it completely. +* The test suite must pass on every individual commit. +* The commit message should explain **why** the change was made, not just describe the diff that `git show` already displays. + +Pro-tip: Instead of blindly running `git add .`, use `git add -p`. It steps through file chunks (hunks) interactively, letting you stage refactoring separately from a critical logic fix. + +--- + +## 3. Rebase vs. Merge: Practical Rules over Dogma + +Online discussions often turn rebase vs. merge into a religious debate. In real teams: + +* **Local Private Branches:** Use `git rebase -i` freely to squash typo fixes and draft commits before publishing your pull request. Clean history respects your team's review time. +* **Shared Public Branches:** **Never rebase a branch that other teammates are pulling from.** Rebase rewrites commit hashes, which creates divergent duplicate histories for anyone else collaborating on that branch. Use explicit merges for shared integration branches. + +--- + +## 4. Git Reflog: Almost Nothing is Ever Lost + +Accidentally ran `git reset --hard HEAD~3` and watched your unpushed commits disappear? Don't panic. +```bash +git reflog +``` +While `git log` only traverses the active branch's ancestry, `reflog` is Git's flight recorder. Every time `HEAD` moves (commit, checkout, rebase, reset), Git records the previous position. + +Find the commit hash right before your blunder, and restore it: +```bash +git branch rescue-branch HEAD@{1} +``` +As long as changes were committed at least once, Git keeps the loose objects around for weeks before garbage collection touches them. + +--- + +## 5. Small Branches, Rapid Feedback + +A branch that sits open for a month with 200 modified files is a ticking conflict bomb. The cognitive overhead of reviewing code scales exponentially with the diff size. Keep branches short-lived: split features into manageable pull requests, automate lint and tests in CI, and merge daily. + +Mastering Git is not about memorizing flags; it is about building a clear mental model so you can work fearlessly. +TEXT, + ], + [ 'title' => 'Test-Driven Development When It Actually Pays Off: Writing Testable Code Without the Dogma', 'slug' => 'tdd-writing-testable-code', @@ -2626,6 +2708,105 @@ A: روی مسیرهای حیاتی و حالات خاص تمرکز کنید، Q: برای نوشتن کد آزمون‌پذیر حتماً باید از توسعه آزمونمحور استفاده کنم؟ A: نه. توسعه آزمونمحور روشی است که معمولاً کد آزمون‌پذیر تولید می‌کند، اما می‌توانید به‌تدریج کد را به سمت آزمون‌پذیری ریفکتور کنید. هدف، کد آزمون‌پذیر با تست‌های سریع و معنادار است، نه رعایت دقیق همه قواعد. +TEXT, + ], + + [ + 'title' => 'گیت از نگاه یک برنامه‌نویس باتجربه: مدل ذهنی، زخم‌های کاری و عادت‌هایی که نجاتم دادند', + 'slug' => 'git-mental-model-and-battle-tested-habits-fa', + 'excerpt' => 'گیت یک ابزار جادویی یا یک سری دستور برای حفظ کردن در شب امتحان نیست. وقتی فهمیدم گیت تفاوت خطوط را ذخیره نمی‌کند بلکه از کل پروژه عکس می‌گیرد، ترسم ریخت. این مرور تجربیاتی است که در ده سال گذشته از خرابکاری‌ها، ریبیس‌های لعنتی و نجات کدهای پاک‌شده یاد گرفتم.', + 'category' => 'engineering-practices-fa', + 'tags' => ['git-fa', 'clean-code-fa'], + 'cover' => 'گیت و تجربیات واقعی یک برنامه‌نویس', + 'custom_cover' => 'https://parsaaghayi.ir/images/blog/posts/git-craft.png', + 'views' => 2850, + 'likes' => 214, + 'days_ago' => 1, + 'body' => <<<'TEXT' +# گیت از نگاه یک برنامه‌نویس باتجربه: مدل ذهنی، زخم‌های کاری و عادت‌هایی که نجاتم دادند + +سال‌های اول کارم، هر بار که ترمینال با ارور `CONFLICT (content): Merge conflict in...` قرمز می‌شد، ضربان قلبم می‌رفت روی ۱۲۰. پوشه پروژه را یک جا کپی می‌کردم، با شرمندگی `git stash` می‌زدم و دعا می‌کردم کدم دود نشود برود هوا. اگر شما هم در دوره‌ای از زندگی‌تان یک فولدر به اسم `project_final_v2_really_final` روی دسکتاپ داشته‌اید یا حس می‌کنید گیت یک جعبه سیاه ترسناک است، بدانید تنها نیستید. تقریباً همه ما از همین مسیر رد شده‌ایم. + +مشکل اینجاست که در اکثر آموزش‌ها، گیت را مثل ورد و جادو درس می‌دهند: «این دستور را بزن، بعد این را، بعد هم push کن». اما تا وقتی نفهمید زیر کاپوت چه خبر است، اولین ارور عجیب شما را فلج می‌کند. در این مقاله می‌خواهم بدون واژه‌های اتوکشیده و شعاری، مدل ذهنی واقعی گیت و آن چند عادت حیاتی را که در پروژه‌های تیمی نجاتم دادند با شما در میان بگذارم. + +--- + +## ۱. مدل ذهنی که همه‌چیز را تغییر داد: گیت دیف ذخیره نمی‌کند! + +بزرگ‌ترین لحظه روشن‌شدن موتور من با گیت روزی بود که فهمیدم: **گیت برخلاف سیستم‌های قدیمی مثل SVN، تفاوت فایل‌ها (Delta) را ذخیره نمی‌کند؛ بلکه از وضعیت فایل‌ها در لحظه عکس (Snapshot) می‌گیرد.** + +تصور کنید هر کامیت، یک عکس پولاروید از کل پرونده‌های پروژه شما در آن ثانیه است: +* اگر فایلی تغییر نکرده باشد، گیت عکس جدیدی نمی‌گیرد، فقط یک میانبر (Pointer) به همان عکس قبلی می‌زند تا هارد بیهوده پر نشود. +* هش SHA-1 هر کامیت (همان رشته طولانی چهل حرفی مثل `a1b2c3d...`)، اثر انگشت محتوای پروژه به‌علاوه شناسه عکس قبلی است. همین پیوند زنجیره‌ای است که تاریخچه را تخریب‌ناپذیر می‌کند. + +وقتی این را درک کنید، می‌فهمید **شاخه (Branch)** چیست. شاخه یک کپی سنگین از پروژه در یک پوشه دیگر نیست! شاخه در گیت فقط یک فایل متنی ساده و سبک در مسیر `.git/refs/heads/` است که ۴۰ کاراکتر دارد: هش آخرین کامیتی که روی آن هستید. همین! ساختن شاخه در گیت رایگان و لحظه‌ای است، چون تنها کاری که می‌کند جابه‌جا کردن یک نشانگر کوچک است. + +--- + +## ۲. کامیت‌های اتمیک: نجات‌دهنده شما در ساعت ۲ بامداد + +خیلی از بچه‌ها وقتی کارشان تمام می‌شود، یک‌باره می‌نویسند: +```bash +git add . +git commit -m "fixed issues and updated design and bugfix" +``` +این بدترین بلایی است که می‌توانید سر خودتان و هم‌تیمی‌هایتان بیاورید. چرا؟ +چون وقتی دو روز بعد در پروداکشن باگی بالا بیاید که تراکنش پرداخت را می‌شکند، شما نمی‌توانید این کامیت غول‌پیکر را بدون از دست دادن دیزاین جدید revert کنید. + +قانون طلایی من در این سال‌ها **کامیت‌های اتمیک (Atomic Commits)** بوده است: +* هر کامیت باید یک و فقط یک کار مشخص را انجام دهد. +* پروژه در هر کامیت باید بیلد شود و تست‌هایش سبز باشد. +* پیام کامیت نباید بگوید «چه خطوطی عوض شد» (چون `git diff` خودش این را داد می‌زند!)؛ پیام باید توضیح دهد **«چرا این تغییر لازم بود»**. + +یک تکنیک ساده: به جای `git add .` از دستور معجزه‌آسای `git add -p` استفاده کنید. این دستور تغییرات فایل را تکه‌تکه (Hunk) به شما نشان می‌دهد و می‌پرسد: «آیا این تغییر مربوط به همین کار است؟». این یعنی می‌توانید یک ریفکتور تمیز و یک رفع باگ را حتی اگر در یک فایل باشند، در دو کامیت مجزا و تمیز ثبت کنید. + +--- + +## ۳. جنگ مذهبی Rebase در برابر Merge: تجربه در دنیای واقعی + +بحث‌های زیادی در توییتر و ردیت می‌بینید که کدام بهتر است. حقیقت این است که هر کدام وظیفه مشخصی دارند: + +* **شاخه‌های لوکال (Local Work):** وقتی روی برنچ اختصاصی خودتان کار می‌کنید، کدهایتان شلخته است. ده بار کامیت می‌زنید: «تست ۱»، «رفع تایپو»، «الان کار میکنه». قبل از این که این برنچ را به خورد تیم بدهید، با `git rebase -i` (ریبیس تعاملی) این خرده‌کامیت‌ها را ادغام (Squash) کنید و یک پیام آبرومند بنویسید. +* **شاخه‌های عمومی (Shared Branches):** هرگز، تأکید می‌کنم **هرگز روی شاخه‌ای که بقیه هم‌تیمی‌ها رویش کد پوش می‌کنند rebase نزنید**. چون ریبیس تاریخچه و هش‌ها را بازنویسی می‌کند و باعث ایجاد تاریخچه‌های موازی و مغایر در سیستم بقیه می‌شود. برای ترکیب شاخه‌های عمومی، یک `merge` شفاف با حفظ تاریخچه همیشه امن‌ترین راه است. + +--- + +## ۴. جادوی Reflog: در گیت هیچ‌چیز واقعاً پاک نمی‌شود + +یکی از بزرگ‌ترین استرس‌های برنامه‌نویس‌ها این است: «نکند کدم پرید؟» +روزی در پروژه‌ای با یک دستور ناشیانه `git reset --hard HEAD~3` سه روز زحمت روی یک ماژول را پراندم. مانیتور را نگاه می‌کردم و عرق سرد روی پیشانیم نشسته بود. آنجا بود که با نجات‌دهنده‌ترین دستور دنیا آشنا شدم: +```bash +git reflog +``` +در حالی که `git log` فقط تاریخچه خطی شاخه فعلی را نشان می‌دهد، `reflog` دفترچه یادداشت مخفی گیت است. هر بار که `HEAD` در کامپیوتر شما حتی یک میلی‌متر تکان بخورد (با کامیت، چک‌اوت، ریست یا ریبیس)، گیت در reflog یک خط می‌اندازد. + +تنها کاری که باید بکنید این است: +۱. دستور `git reflog` را بزنید و کامیتی که قبل از خرابکاری در آن بودید را پیدا کنید (مثلاً `HEAD@{2}`). +۲. شاخه را به آن نقطه برگردانید: +```bash +git branch rescue-branch HEAD@{2} +``` +تمام کدهای شما دست‌نخورده برمی‌گردد. در گیت تا وقتی که یک تغییر حداقل یک بار کامیت شده باشد، حتی اگر برنچش را پاک کنید، تا هفته‌ها در دیتابیس لوکال شما زنده است. + +--- + +## ۵. گردش کار اصولی در تیم: شاخه‌های کوچک و بازبینی‌های سریع + +اگر برنچی دارید که ۳ هفته است باز مانده و ۲۰۰ فایل در آن دستکاری شده، شما برنچ نزده‌اید، بمب ساعتی کار گذاشته‌اید! + +تجربه ثابت کرده هزینه مرج کردن و بازبینی کد با اندازه دیف، به صورت نمایی رشد می‌کند. یک Pull Request کوچک با ۵۰ خط تغییر در ده دقیقه با دقت بالا توسط همکارتان بازبینی می‌شود؛ اما یک PR با ۱۲۰۰ خط تغییر، سرسری نگاه می‌شود، یک "LGTM" می‌خورد و سه تا باگ وحشتناک را با خود می‌برد روی پروداکشن. + +* کارها را به ریزوظایف بشکنید. +* برنچ را صبح باز کنید و تا بعدازظهر یا نهایتاً فردا صبح مرج کنید. +* بررسی‌های تکراری مثل فرمت کد، تست‌ها و تایپ‌چک را به CI بسپارید تا انسان‌ها فقط روی منطق بیزنس و معماری تمرکز کنند. + +--- + +## جمع‌بندی و کلام آخر + +گیت یک ابزار مجازات نیست؛ شبکه امن شماست. وقتی به جای ترسیدن از گیت، یاد بگیرید چطور با آن فکر کنید، با اعتمادبه‌نفس کد می‌زنید. می‌دانید که می‌توانید بزرگ‌ترین ریفکتورها را امتحان کنید، چون همیشه راهی برای برگشت به ده ثانیه قبل وجود دارد. + +شما تا حالا چه تجربه یا خرابکاری معروفی با گیت داشتید که هیچ‌وقت یادتان نمی‌رود؟ در بخش کامنت‌ها بنویسید تا بقیه هم از این زخم‌ها درس بگیرند. TEXT, ], ]; @@ -2655,6 +2836,15 @@ TEXT, ); } + // Dedicated custom cover image overrides if present in payload + foreach ($posts as $index => $post) { + if (!empty($post['custom_cover'])) { + Post::where('id', $postIds[$index])->update([ + 'featured_image' => $post['custom_cover'], + ]); + } + } + // Related posts: keep every "you might like" list inside the reader's // language. The blog is a strict English/Persian mirror, so a reader on // the Persian version must never see English cards and vice versa.