DEV Community

Kingsley Akindele
Kingsley Akindele

Posted on • Edited on

Git Best Practices: Advanced Techniques for Clean Code and Team Collaboration

Git is the backbone of modern collaborative software development, yet many developers barely scratch the surface of its capabilities. Beyond basic commit, pull, and push, Git provides a robust set of tools for rewriting history, managing parallel feature development, and keeping your project's timeline clean. This post walks through advanced Git techniques that can level up your workflow, reduce merge conflicts, and foster cleaner collaboration in teams.

Rewriting History With rebase vs merge

While git merge is safe and non-destructive, it often leads to noisy commit histories, especially when many feature branches are at play. git rebase, on the other hand, rewrites commit history to make it linear.

Let’s say you’re working on feature-1 and want to bring in the latest changes from main:

git checkout feature-1
git fetch origin
git rebase origin/main
Enter fullscreen mode Exit fullscreen mode

This makes it appear like your feature was developed from the latest main branch. It avoids unnecessary merge commits and makes git log cleaner and more digestible.

Use merge when collaboration is active on a branch and history needs to be preserved. Use rebase when you're working solo and prefer a tidy history.

Interactive Rebase: Squashing and Editing Commits

You don't need ten commits for a three-line change. Enter:

git rebase -i HEAD~5
Enter fullscreen mode Exit fullscreen mode

This opens an editor where you can squash, reorder, or rename commits.

pick e4b1 commit A
squash 29a3 commit B
reword 3f42 commit C
Enter fullscreen mode Exit fullscreen mode

This is ideal before opening a pull request — it keeps your PRs concise and easy to review.

Stashing: Pause Work Without Losing Progress

Ever found yourself halfway through a bug fix when a critical production issue pops up? git stash lets you save your current state without committing:

git stash
git checkout main
# fix the emergency
git checkout feature-1
git stash pop
Enter fullscreen mode Exit fullscreen mode

You can even name stashes:

git stash save "WIP: new login page"
Enter fullscreen mode Exit fullscreen mode

Or apply a specific stash later:

git stash list
git stash apply stash@{2}
Enter fullscreen mode Exit fullscreen mode

Cherry-Picking: Apply Specific Commits Across Branches

Sometimes you need to apply a single commit from another branch without merging everything. That’s where cherry-pick comes in:

git checkout hotfix
git cherry-pick a1b2c3d
Enter fullscreen mode Exit fullscreen mode

Great for backporting bug fixes to older versions.

Bisecting: Find the Commit That Introduced a Bug

If a bug appears and you’re unsure when it was introduced, use git bisect:

git bisect start
git bisect bad           # current commit is bad
git bisect good a1b2c3d  # known good commit
Enter fullscreen mode Exit fullscreen mode

Git will walk you through a binary search of commits until you find the exact one that introduced the issue.

Hooks: Automate Quality Checks

Git hooks let you enforce rules automatically. For example, add a pre-commit hook in .git/hooks/pre-commit:

#!/bin/sh
npm run lint
Enter fullscreen mode Exit fullscreen mode

Use tools like Husky to manage hooks in cross-platform teams and repositories.

Tracking Upstream Changes with Forks

If you’ve forked a repo and want to stay in sync with the original, add the upstream remote:

git remote add upstream https://github.com/original/repo.git
git fetch upstream
git rebase upstream/main
Enter fullscreen mode Exit fullscreen mode

This ensures you get the latest changes from upstream without polluting history with merge commits.

Clean Branch Management

Delete stale branches:

git branch -d feature/old
Enter fullscreen mode Exit fullscreen mode

And delete remote branches too:

git push origin --delete feature/old
Enter fullscreen mode Exit fullscreen mode

Use git branch --merged to find branches that have already been merged into your current branch — ideal for cleanup.

Conclusion

Advanced Git workflows can feel intimidating, but they’re powerful tools that enable cleaner commits, faster debugging, and easier collaboration. Whether you're solo or on a large team, mastering these tools can make your version control effortless, efficient, and even elegant.

Let me know your best git commands approach. Drop it in the comments—I’d love to hear your approach!


Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.