Advanced Git Concepts
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.
1. Rebasing β Cleaner Commit Historyβ
Rebase helps you rewrite commit history by placing your commits on top of another branchβs latest changes.
Example:β
# Switch to your feature branch
git checkout feature-branch
# Rebase it with the main branch
git rebase main
Instead of merging (which creates an extra merge commit), rebasing keeps history linear β perfect for clean commit timelines.
Never rebase public branches that others are working on β it rewrites commit history.
2. Cherry-Picking Commitsβ
Cherry-pick allows you to apply specific commits from one branch to another without merging everything.
Example:β
# Copy a commit from another branch
git cherry-pick <commit-hash>
Use this when you want to move a single fix or feature to another branch without merging unrelated changes.
3. Tagging Versions (Releases)β
Tags are references that mark specific commits β often used for version releases.
Create a lightweight tag:β
git tag v1.0.0
Create an annotated tag:β
git tag -a v1.0.0 -m "Initial stable release"
Push tags to remote:β
git push origin --tags
Tags are ideal for marking milestones in projects or generating release versions on GitHub.
4. Submodules β Managing Nested Repositoriesβ
Submodules allow you to embed one Git repository inside another β useful for managing libraries or dependencies.
Example:β
# Add a submodule
git submodule add https://github.com/user/library.git lib/
# Initialize and update
git submodule init
git submodule update
Use submodules when you need an external repository but want to control its version manually.
5. Squashing Commitsβ
Squash combines multiple commits into one β keeping your history neat and readable before merging.
Example (Interactive Rebase):β
git rebase -i HEAD~3
This opens an editor β change pick to squash for the commits you want to merge.
Ideal for cleaning up βWIPβ commits before submitting a pull request.
6. Git Hooks β Automate Actionsβ
Git Hooks are scripts that automatically run when certain Git events occur (like committing or pushing).
Example:β
.git/hooks/pre-commitβ runs before each commit..git/hooks/post-mergeβ runs after a merge.
You can use hooks to:
- Run tests before committing
- Format code automatically
- Enforce commit message rules
Try tools like Husky to manage Git hooks in JavaScript projects easily.
7. Reflog β Recover Lost Commitsβ
Reflog records every change to your HEAD β perfect for recovering commits after resets or mistakes.
Example:β
git reflog
Find the commit hash and restore it:
git checkout <commit-hash>
Even if you accidentally delete branches or commits, reflog can bring them back.
8. Bisect β Debugging with Binary Searchβ
Use git bisect to find which commit introduced a bug.
Example:β
git bisect start
git bisect bad
git bisect good <commit-hash>
Git checks out commits one by one until it identifies the problematic one.
Perfect for debugging large projects efficiently.
9. Amend Last Commitβ
Fix small mistakes (like typos or missed files) without creating a new commit.
git add .
git commit --amend -m "Updated commit message"
Keep commit history clean without unnecessary βfixβ commits.
10. Worktrees β Multiple Working Directoriesβ
Worktrees allow you to work on multiple branches simultaneously without switching context.
git worktree add ../feature feature-branch
Now you can work on feature-branch in another folder while keeping main active elsewhere.
Advanced Git Flow Tipsβ
| Technique | Use Case |
|---|---|
| Rebase | Clean, linear history |
| Cherry-pick | Move specific commits |
| Squash | Combine multiple commits |
| Hooks | Automate tasks |
| Reflog | Recover deleted commits |
| Worktree | Manage multiple branches easily |
Example Workflowβ
Hereβs how professionals maintain a clean project history:
-
Create feature branch
git checkout -b feature/login -
Make several commits
-
Squash and rebase before merging
git rebase -i main -
Push clean history to remote
git push origin feature/login
-
Use Git aliases for faster workflows:
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm "commit -m" -
Add color output for better readability:
git config --global color.ui auto -
Use
git log --oneline --graphto visualize your history beautifully.
Coming Nextβ
Now that youβve mastered the advanced Git toolkit, move on to: π GitHub Actions β Automate Your Workflows
π Additional Resourcesβ
- Pro Git Book β Chapter 7: Advanced Git
- Git Documentation: Rebase
- Git Hooks Guide
- Git Submodules Explained
- Learn Git Branching (Advanced)
Summaryβ
| Concept | Description |
|---|---|
| Rebase | Reapply commits for clean history. |
| Cherry-pick | Apply specific commits from another branch. |
| Squash | Combine multiple commits. |
| Submodules | Manage external repositories. |
| Hooks | Automate Git actions. |
| Reflog | Recover lost commits. |
| Worktrees | Work on multiple branches at once. |
π This tutorial is part of the CodeHarborHub Git & GitHub learning series β helping you build professional version control skills used in real-world development.