|
| 1 | +--- |
| 2 | +title: "Advanced Git Concepts" |
| 3 | +description: "Master advanced Git concepts including rebase, cherry-pick, tagging, submodules, squashing, and reflog to handle complex version control workflows like a pro." |
| 4 | +tags: ["git advanced", "rebase", "cherry-pick", "git hooks", "submodules", "squash commits"] |
| 5 | +keywords: ["advanced git tutorial", "git rebase", "git cherry pick", "git submodules", "git hooks", "git reflog"] |
| 6 | +sidebar_position: 6 |
| 7 | +--- |
| 8 | + |
| 9 | +Welcome to the **Advanced Git Concepts** tutorial — where you’ll move beyond basic commands and learn how professionals handle complex workflows, cleaner histories, and recovery techniques. By the end of this section, you'll confidently use commands like `rebase`, `cherry-pick`, `tag`, `reflog`, and more to manage any situation in Git. |
| 10 | + |
| 11 | +## 1. Rebasing — Cleaner Commit History |
| 12 | + |
| 13 | +**Rebase** helps you *rewrite commit history* by placing your commits on top of another branch’s latest changes. |
| 14 | + |
| 15 | +### Example: |
| 16 | + |
| 17 | +```bash |
| 18 | +# Switch to your feature branch |
| 19 | +git checkout feature-branch |
| 20 | + |
| 21 | +# Rebase it with the main branch |
| 22 | +git rebase main |
| 23 | +``` |
| 24 | + |
| 25 | +> Instead of merging (which creates an extra merge commit), rebasing keeps history linear — perfect for clean commit timelines. |
| 26 | +
|
| 27 | +<br /> |
| 28 | + |
| 29 | +:::warning |
| 30 | +Never rebase public branches that others are working on — it rewrites commit history. |
| 31 | +::: |
| 32 | + |
| 33 | +## 2. Cherry-Picking Commits |
| 34 | + |
| 35 | +**Cherry-pick** allows you to *apply specific commits* from one branch to another without merging everything. |
| 36 | + |
| 37 | +### Example: |
| 38 | + |
| 39 | +```bash |
| 40 | +# Copy a commit from another branch |
| 41 | +git cherry-pick <commit-hash> |
| 42 | +``` |
| 43 | + |
| 44 | +Use this when you want to move a single fix or feature to another branch without merging unrelated changes. |
| 45 | + |
| 46 | +## 3. Tagging Versions (Releases) |
| 47 | + |
| 48 | +**Tags** are references that mark specific commits — often used for version releases. |
| 49 | + |
| 50 | +### Create a lightweight tag: |
| 51 | + |
| 52 | +```bash |
| 53 | +git tag v1.0.0 |
| 54 | +``` |
| 55 | + |
| 56 | +### Create an annotated tag: |
| 57 | + |
| 58 | +```bash |
| 59 | +git tag -a v1.0.0 -m "Initial stable release" |
| 60 | +``` |
| 61 | + |
| 62 | +### Push tags to remote: |
| 63 | + |
| 64 | +```bash |
| 65 | +git push origin --tags |
| 66 | +``` |
| 67 | + |
| 68 | +> Tags are ideal for marking milestones in projects or generating release versions on GitHub. |
| 69 | +
|
| 70 | +## 4. Submodules — Managing Nested Repositories |
| 71 | + |
| 72 | +Submodules allow you to **embed one Git repository inside another** — useful for managing libraries or dependencies. |
| 73 | + |
| 74 | +### Example: |
| 75 | + |
| 76 | +```bash |
| 77 | +# Add a submodule |
| 78 | +git submodule add https://github.com/user/library.git lib/ |
| 79 | + |
| 80 | +# Initialize and update |
| 81 | +git submodule init |
| 82 | +git submodule update |
| 83 | +``` |
| 84 | + |
| 85 | +> Use submodules when you need an external repository but want to control its version manually. |
| 86 | +
|
| 87 | + |
| 88 | +## 5. Squashing Commits |
| 89 | + |
| 90 | +**Squash** combines multiple commits into one — keeping your history neat and readable before merging. |
| 91 | + |
| 92 | +### Example (Interactive Rebase): |
| 93 | + |
| 94 | +```bash |
| 95 | +git rebase -i HEAD~3 |
| 96 | +``` |
| 97 | + |
| 98 | +This opens an editor — change `pick` to `squash` for the commits you want to merge. |
| 99 | + |
| 100 | +:::tip |
| 101 | +Ideal for cleaning up “WIP” commits before submitting a pull request. |
| 102 | +::: |
| 103 | + |
| 104 | +## 6. Git Hooks — Automate Actions |
| 105 | + |
| 106 | +**Git Hooks** are scripts that automatically run when certain Git events occur (like committing or pushing). |
| 107 | + |
| 108 | +### Example: |
| 109 | + |
| 110 | +* `.git/hooks/pre-commit` — runs before each commit. |
| 111 | +* `.git/hooks/post-merge` — runs after a merge. |
| 112 | + |
| 113 | +You can use hooks to: |
| 114 | + |
| 115 | +* Run tests before committing |
| 116 | +* Format code automatically |
| 117 | +* Enforce commit message rules |
| 118 | + |
| 119 | +> Try tools like **Husky** to manage Git hooks in JavaScript projects easily. |
| 120 | +
|
| 121 | +## 7. Reflog — Recover Lost Commits |
| 122 | + |
| 123 | +**Reflog** records every change to your HEAD — perfect for recovering commits after resets or mistakes. |
| 124 | + |
| 125 | +### Example: |
| 126 | + |
| 127 | +```bash |
| 128 | +git reflog |
| 129 | +``` |
| 130 | + |
| 131 | +Find the commit hash and restore it: |
| 132 | + |
| 133 | +```bash |
| 134 | +git checkout <commit-hash> |
| 135 | +``` |
| 136 | + |
| 137 | +<br /> |
| 138 | + |
| 139 | +:::tip Lifesaver |
| 140 | +Even if you accidentally delete branches or commits, `reflog` can bring them back. |
| 141 | +::: |
| 142 | + |
| 143 | +## 8. Bisect — Debugging with Binary Search |
| 144 | + |
| 145 | +Use **`git bisect`** to find which commit introduced a bug. |
| 146 | + |
| 147 | +### Example: |
| 148 | + |
| 149 | +```bash |
| 150 | +git bisect start |
| 151 | +git bisect bad |
| 152 | +git bisect good <commit-hash> |
| 153 | +``` |
| 154 | + |
| 155 | +Git checks out commits one by one until it identifies the problematic one. |
| 156 | + |
| 157 | +> Perfect for debugging large projects efficiently. |
| 158 | +
|
| 159 | +## 9. Amend Last Commit |
| 160 | + |
| 161 | +Fix small mistakes (like typos or missed files) without creating a new commit. |
| 162 | + |
| 163 | +```bash |
| 164 | +git add . |
| 165 | +git commit --amend -m "Updated commit message" |
| 166 | +``` |
| 167 | + |
| 168 | +> Keep commit history clean without unnecessary “fix” commits. |
| 169 | +
|
| 170 | +## 10. Worktrees — Multiple Working Directories |
| 171 | + |
| 172 | +Worktrees allow you to work on multiple branches **simultaneously** without switching context. |
| 173 | + |
| 174 | +```bash |
| 175 | +git worktree add ../feature feature-branch |
| 176 | +``` |
| 177 | + |
| 178 | +Now you can work on `feature-branch` in another folder while keeping `main` active elsewhere. |
| 179 | + |
| 180 | +## Advanced Git Flow Tips |
| 181 | + |
| 182 | +| Technique | Use Case | |
| 183 | +| --------------- | ------------------------------- | |
| 184 | +| **Rebase** | Clean, linear history | |
| 185 | +| **Cherry-pick** | Move specific commits | |
| 186 | +| **Squash** | Combine multiple commits | |
| 187 | +| **Hooks** | Automate tasks | |
| 188 | +| **Reflog** | Recover deleted commits | |
| 189 | +| **Worktree** | Manage multiple branches easily | |
| 190 | + |
| 191 | +## Example Workflow |
| 192 | + |
| 193 | +Here’s how professionals maintain a clean project history: |
| 194 | + |
| 195 | +1. Create feature branch |
| 196 | + |
| 197 | + ```bash |
| 198 | + git checkout -b feature/login |
| 199 | + ``` |
| 200 | +2. Make several commits |
| 201 | +3. Squash and rebase before merging |
| 202 | + |
| 203 | + ```bash |
| 204 | + git rebase -i main |
| 205 | + ``` |
| 206 | +4. Push clean history to remote |
| 207 | + |
| 208 | + ```bash |
| 209 | + git push origin feature/login |
| 210 | + ``` |
| 211 | + |
| 212 | +:::tip Git Tips |
| 213 | + |
| 214 | +* Use **Git aliases** for faster workflows: |
| 215 | + |
| 216 | + ```bash |
| 217 | + git config --global alias.st status |
| 218 | + git config --global alias.co checkout |
| 219 | + git config --global alias.br branch |
| 220 | + git config --global alias.cm "commit -m" |
| 221 | + ``` |
| 222 | + |
| 223 | +* Add color output for better readability: |
| 224 | + |
| 225 | + ```bash |
| 226 | + git config --global color.ui auto |
| 227 | + ``` |
| 228 | + |
| 229 | +* Use `git log --oneline --graph` to visualize your history beautifully. |
| 230 | + |
| 231 | +::: |
| 232 | + |
| 233 | +## Coming Next |
| 234 | + |
| 235 | +Now that you’ve mastered the advanced Git toolkit, move on to: 👉 [GitHub Actions — Automate Your Workflows](#) |
| 236 | + |
| 237 | +## 📚 Additional Resources |
| 238 | + |
| 239 | +* [Pro Git Book — Chapter 7: Advanced Git](https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging) |
| 240 | +* [Git Documentation: Rebase](https://git-scm.com/docs/git-rebase) |
| 241 | +* [Git Hooks Guide](https://git-scm.com/docs/githooks) |
| 242 | +* [Git Submodules Explained](https://git-scm.com/book/en/v2/Git-Tools-Submodules) |
| 243 | +* [Learn Git Branching (Advanced)](https://learngitbranching.js.org/?demo) |
| 244 | + |
| 245 | +### Summary |
| 246 | + |
| 247 | +| Concept | Description | |
| 248 | +| --------------- | ------------------------------------------- | |
| 249 | +| **Rebase** | Reapply commits for clean history. | |
| 250 | +| **Cherry-pick** | Apply specific commits from another branch. | |
| 251 | +| **Squash** | Combine multiple commits. | |
| 252 | +| **Submodules** | Manage external repositories. | |
| 253 | +| **Hooks** | Automate Git actions. | |
| 254 | +| **Reflog** | Recover lost commits. | |
| 255 | +| **Worktrees** | Work on multiple branches at once. | |
| 256 | + |
| 257 | +--- |
| 258 | + |
| 259 | +💙 *This tutorial is part of the CodeHarborHub Git & GitHub learning series — helping you build professional version control skills used in real-world development.* |
0 commit comments