Git Workflow Masterclass
Master professional Git workflows including branching strategies, rebasing, cherry-picking, and team collaboration patterns.
DevOpsIntermediate20 min read
AI Summary
Quick context for Git Workflow Masterclass
Git Workflow Masterclass is a intermediate guide in DevOps. Master professional Git workflows including branching strategies, rebasing, cherry-picking, and team collaboration patterns.. Key topics: branching, collaboration, git, version-control.
## Why Git Workflows Matter
A well-defined Git workflow keeps your team productive, your history clean, and your deployments predictable. Without one, repositories descend into merge conflicts, lost changes, and confusion about which branch contains production-ready code.
This guide covers the workflows used by high-performing engineering teams, from branch naming conventions to advanced techniques like interactive rebase and bisect.
## Branching Strategies
### Git Flow
Git Flow is a strict branching model designed around project releases. It uses long-lived branches for `main`, `develop`, and short-lived branches for features, releases, and hotfixes.
```bash
# Start a new feature
git checkout develop
git checkout -b feature/user-authentication
# Finish the feature
git checkout develop
git merge --no-ff feature/user-authentication
git branch -d feature/user-authentication
```
Git Flow works well for projects with scheduled releases but adds overhead for continuous deployment workflows.
### GitHub Flow
GitHub Flow is a lightweight alternative. It has one long-lived branch (`main`) and short-lived feature branches that merge via pull requests.
```bash
# Create a feature branch
git checkout main
git pull origin main
git checkout -b add-login-page
# Work on the branch, commit, and push
git add .
git commit -m "Add login page component"
git push origin add-login-page
# Open a pull request, get review, merge to main
```
This is the preferred workflow for teams practicing continuous deployment.
### Trunk-Based Development
Trunk-based development keeps everyone working on `main` (the trunk) with very short-lived feature branches. Feature flags gate incomplete work.
```bash
# Short-lived branch
git checkout main
git pull origin main
git checkout -b fix-header-alignment
# Make changes, commit, push, PR, merge — all within hours
git add .
git commit -m "Fix header alignment on mobile"
git push origin fix-header-alignment
```
This requires strong CI/CD and feature flag infrastructure but minimizes merge conflicts.
## Interactive Rebase
Interactive rebase lets you rewrite commit history before merging. Clean up "work in progress" commits into meaningful units.
```bash
# Rebase the last 3 commits
git rebase -i HEAD~3
# In the editor:
pick abc1234 Add user model
squash def5678 Fix typo in user model
squash ghi9012 Add validation logic
```
This creates a clean, readable history that reviewers can follow. Use it on feature branches before merging — never on shared branches.
## Cherry-Picking
Cherry-pick applies a specific commit from one branch to another. Useful for hotfixes.
```bash
# Apply a specific commit to the current branch
git cherry-pick abc1234
# Apply without committing (stage the changes)
git cherry-pick --no-commit abc1234
```
Use cherry-pick sparingly. Prefer merging or rebasing to keep history consistent.
## Stashing Changes
Stash uncommitted changes when you need to switch context quickly.
```bash
# Stash current changes
git stash push -m "WIP: refactor auth module"
# List stashes
git stash list
# Apply the most recent stash
git stash pop
# Apply a specific stash
git stash apply stash@{1}
```
## Bisect for Bug Hunting
`git bisect` uses binary search to find the commit that introduced a bug.
```bash
# Start bisecting
git bisect start
git bisect bad # Current commit is broken
git bisect good abc1234 # This commit was working
# Git checks out a middle commit. Test it, then mark it:
git bisect good # or git bisect bad
# Repeat until Git identifies the first bad commit
# When done:
git bisect reset
```
## Practical Tips
- **Write meaningful commit messages.** Use the imperative mood: "Add feature" not "Added feature."
- **Keep commits atomic.** Each commit should do one thing.
- **Never force-push to shared branches.** Use `--force-with-lease` if you must force-push a personal branch.
- **Tag releases.** Use annotated tags for version numbers.
- **Use `.gitignore` properly.** Exclude build artifacts, dependencies, and environment files from the start.
## Verifying Your Setup
Run these commands to ensure your Git configuration is production-ready:
```bash
git config --global user.name
git config --global user.email
git config --global core.autocrlf # Should be 'input' on Linux/Mac
```
Good Git hygiene compounds over time. The habits you build today will save your team hours every week.
Continue with related content
Suggested next reads and tools from the knowledge graph.