|
| 1 | +# 🧭 Git Branch Synchronization & Recovery Guide |
| 2 | + |
| 3 | +This documentation explains **how to keep your `testing` branch up to date with your `main` branch**, |
| 4 | +and how to **restore the `main` branch** to a previous working snapshot if anything goes wrong. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## 📌 Overview |
| 9 | + |
| 10 | +You are maintaining two branches in your GitHub repository: |
| 11 | +- **`main`** → The active development branch (updated daily) |
| 12 | +- **`testing`** → A stable snapshot branch (used as a secure backup point) |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## 🧩 Workflow Summary |
| 17 | + |
| 18 | +```mermaid |
| 19 | +flowchart TD |
| 20 | + A[Start: main & testing branches exist] --> B[Work daily on main branch] |
| 21 | + B --> C[Periodically update testing branch with main's latest code] |
| 22 | + C --> D[testing acts as backup snapshot] |
| 23 | + D --> E[If main breaks, restore it from testing] |
| 24 | + E --> F[main and testing are in sync again] |
| 25 | +```` |
| 26 | +
|
| 27 | +--- |
| 28 | +
|
| 29 | +## ⚙️ 1. Update `testing` branch from `main` |
| 30 | +
|
| 31 | +### ✅ Safe Steps |
| 32 | +
|
| 33 | +```bash |
| 34 | +# Fetch the latest updates from GitHub |
| 35 | +git fetch origin |
| 36 | +
|
| 37 | +# Switch to testing branch |
| 38 | +git checkout testing |
| 39 | +
|
| 40 | +# Merge latest main changes into testing |
| 41 | +git merge origin/main |
| 42 | +
|
| 43 | +# Resolve any merge conflicts (if they appear), then: |
| 44 | +git commit |
| 45 | +
|
| 46 | +# Push updated testing branch to GitHub |
| 47 | +git push origin testing |
| 48 | +``` |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +### ⚠️ Alternative: Force overwrite testing to be *exactly* like main |
| 53 | + |
| 54 | +If you don’t need testing’s previous commits: |
| 55 | + |
| 56 | +```bash |
| 57 | +git checkout testing |
| 58 | +git reset --hard origin/main |
| 59 | +git push origin testing --force |
| 60 | +``` |
| 61 | + |
| 62 | +🧠 **Use this only if you want testing = main exactly.** |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +## 💾 2. Use testing as a recovery snapshot |
| 67 | + |
| 68 | +If something goes wrong in `main`, you can restore it from `testing`. |
| 69 | + |
| 70 | +### 🩹 Option 1 — Hard reset (overwrite main completely) |
| 71 | + |
| 72 | +```bash |
| 73 | +git checkout main |
| 74 | +git fetch origin |
| 75 | +git reset --hard origin/testing |
| 76 | +git push origin main --force |
| 77 | +``` |
| 78 | + |
| 79 | +* Main becomes **identical** to testing. |
| 80 | +* ⚠️ This **rewrites history**, so warn teammates before force-pushing. |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +### 🛡️ Option 2 — Safe revert (no history rewrite) |
| 85 | + |
| 86 | +```bash |
| 87 | +git checkout main |
| 88 | +git revert --no-commit origin/testing..HEAD |
| 89 | +git commit -m "Revert main back to testing snapshot" |
| 90 | +git push origin main |
| 91 | +``` |
| 92 | + |
| 93 | +* This creates a **new commit** undoing all changes since testing. |
| 94 | +* Safe for shared/team repos. |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +### 🧰 Option 3 — Merge overwrite (preserve history but overwrite content) |
| 99 | + |
| 100 | +```bash |
| 101 | +git checkout main |
| 102 | +git merge -s ours origin/testing -m "Reset main to testing state" |
| 103 | +git push origin main |
| 104 | +``` |
| 105 | + |
| 106 | +* Keeps commit history intact. |
| 107 | +* Commonly used in production environments. |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +## 🏷️ 3. Tag stable snapshots for easy rollback |
| 112 | + |
| 113 | +Before merging or resetting branches, create a tag for a known good version: |
| 114 | + |
| 115 | +```bash |
| 116 | +git checkout testing |
| 117 | +git tag stable-2025-10-13 |
| 118 | +git push origin stable-2025-10-13 |
| 119 | +``` |
| 120 | + |
| 121 | +To restore from a tagged snapshot later: |
| 122 | + |
| 123 | +```bash |
| 124 | +git checkout main |
| 125 | +git reset --hard stable-2025-10-13 |
| 126 | +git push origin main --force |
| 127 | +``` |
| 128 | + |
| 129 | +--- |
| 130 | + |
| 131 | +## 🧭 Visual Guide: Recovery Flow |
| 132 | + |
| 133 | +```mermaid |
| 134 | +graph LR |
| 135 | + A[Main branch breaks] --> B{Do you want full overwrite?} |
| 136 | + B -- Yes --> C[git reset --hard origin/testing] |
| 137 | + B -- No --> D[git revert --no-commit origin/testing..HEAD] |
| 138 | + C --> E[Push with --force] |
| 139 | + D --> F[Commit revert and push normally] |
| 140 | + E --> G[Main restored successfully] |
| 141 | + F --> G[Main restored successfully] |
| 142 | +``` |
| 143 | + |
| 144 | +--- |
| 145 | + |
| 146 | +## 🧩 Summary Table |
| 147 | + |
| 148 | +| Purpose | Command | Safe for Team? | |
| 149 | +| ------------------------------------- | --------------------------------------------- | -------------- | |
| 150 | +| Merge main → testing | `git merge origin/main` | ✅ | |
| 151 | +| Force testing = main | `git reset --hard origin/main` | ⚠️ | |
| 152 | +| Restore main from testing (overwrite) | `git reset --hard origin/testing` | ⚠️ | |
| 153 | +| Restore main safely (revert) | `git revert --no-commit origin/testing..HEAD` | ✅ | |
| 154 | +| Tag stable snapshot | `git tag stable-YYYY-MM-DD` | ✅ | |
| 155 | + |
| 156 | +--- |
| 157 | + |
| 158 | +## 🪶 Author Notes |
| 159 | + |
| 160 | +* This guide is designed for developers using **VS Code connected to GitHub**. |
| 161 | +* You can perform all of these steps from the **VS Code terminal** or the **Source Control** panel. |
| 162 | +* Keep your `testing` branch as your last stable checkpoint to ensure a safety net. |
| 163 | + |
| 164 | +--- |
| 165 | + |
| 166 | +**📘 Last Updated:** October 13, 2025 |
| 167 | +**Repository Purpose:** Safe Git workflow between `main` and `testing` |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +Would you like me to: |
| 172 | +- 📄 generate this as a downloadable `README.md` file, or |
| 173 | +- 💻 open it in a VS Code–ready markdown canvas so you can edit it interactively? |
| 174 | +``` |
0 commit comments