Understanding Git Branches: Local vs Remote Production
Git makes collaboration easy, but small workflow mistakes can create unnecessary pull request conflicts. One of the most common mistakes is creating a feature branch from an outdated local branch instead of the latest remote branch.
The Problem
Suppose you create a new feature branch using your local production branch:
git checkout -b XST2-123-my-feature production
This command creates a new branch from whatever commit your local production branch currently points to.
If your local branch is not synchronized with the remote repository, your new feature branch will also start from outdated code.
Why This Becomes a Problem
When you open a Pull Request on GitHub, GitHub compares your feature branch against the latest origin/production.
If your feature branch started from an old commit, GitHub may display:
- Unrelated file changes
- Missing commits
- Unexpected merge conflicts
- A confusing commit history
This makes code reviews harder and increases the chance of merge issues.
The Safer Approach
Instead of creating the branch from your local production, create it directly from the remote branch.
git fetch origin
git checkout -b XST2-123-my-feature origin/production
This guarantees that your feature branch starts from the latest production code.
Understanding the Difference
Local Branch
production
- Exists only on your computer
- May be outdated
- Requires manual synchronization
Remote Branch
origin/production
- Represents the latest branch on GitHub
- Updated whenever you fetch from the remote repository
- Used by GitHub when comparing Pull Requests
Recommended Workflow
Always start a new task with:
git fetch origin
git checkout -b XST2-123-my-feature origin/production
This workflow ensures your feature branch is based on the latest production code.
What If You Already Created the Branch?
If you’ve already started working from an outdated branch, you can rebase onto the latest production branch.
git fetch origin
git checkout XST2-123-my-feature
git rebase origin/production
If conflicts occur, resolve them, then continue the rebase.
git add .
git rebase --continue
Finally, update the remote branch.
git push --force-with-lease
Using --force-with-lease is recommended because it prevents accidentally overwriting changes pushed by someone else.
Best Practices
- Always run
git fetch originbefore creating a feature branch. - Prefer
origin/productionover the localproductionbranch. - Keep feature branches small and focused.
- Rebase regularly if the production branch changes frequently.
- Avoid unnecessary merge commits in feature branches.
Conclusion
Creating feature branches directly from origin/production helps keep your Git history clean, reduces merge conflicts, and ensures that your Pull Requests contain only the changes related to your work.
Although creating branches from a local production branch can work when it is fully synchronized, using the remote branch is a safer and more reliable workflow for teams.