1. Interactive Rebase for Clean History
Use git rebase -i to squash, reorder, or edit commits before pushing.
# Rebase the last 3 commits interactively
git rebase -i HEAD~3In the editor:
pickkeeps a commit.squash(ors) merges it into the previous commit.editlets you amend the commit message or content.
Tip: Rebase only local commits. Never rebase commits that are already pushed to a shared branch.
2. Git Hooks for Automation
Git hooks run scripts at specific points in the workflow.
# Pre-commit hook (runs before every commit)
#!/bin/sh
git diff --cached --name-only | grep '\.py$' | xargs pylintPlace the script in .git/hooks/pre-commit. Make it executable with chmod +x .git/hooks/pre-commit.
Best Practice: Keep hooks simple and repository‑agnostic. Use a package manager (e.g., pre-commit) to standardize across developers.
3. Advanced Branching Strategies
- Feature branching:
git checkout -b feature/awesome-feature - Release branching:
git checkout -b release/v1.2
Use git merge --no-ff for a merge commit that preserves branch boundaries.
git merge --no-ff feature/awesome-feature -m "Add awesome feature"Tip: Tag release branches with git tag -a v1.2 -m "Release 1.2" for easy rollbacks.
4. Bisecting Bugs Quickly
git bisect automates binary search between a bad and a good commit.
git bisect start
git bisect bad # current commit
git bisect good v1.1.0
# Git checks out a commit; test it
# Mark as good/bad
git bisect good
git bisect bad
# Repeat until the culprit is found
git bisect resetKey Tip: Automate tests inside git bisect run "./test.sh" to speed up the process.
5. Submodule Management
Add a submodule for shared libraries.
git submodule add https://github.com/owner/lib.git libs/lib
git submodule update --init --recursiveKeep submodule URLs in .gitmodules and commit the state (SHA) to ensure reproducibility.
6. Quick‑Access Tips
git reflog→ view recent HEAD movements.git cherry-pick <hash>→ apply a single commit to another branch.git reset --soft HEAD~1→ undo last commit but keep changes staged.git stash push -m "WIP"→ save work in progress.
Takeaway: Mastering these advanced Git techniques lets you maintain a clean history, automate quality checks, and debug efficiently. Apply them in your workflow to boost collaboration and reliability.