- Git Workflow: Update Your Branch Before Creating a Pull Request
If main changed before you open a PR
- You finish your feature, but before opening the PR,
mainhas moved forward a bit.
Best Practice
Before opening a PR:
- finish your feature
- update local
main - merge
maininto your feature branch - fix conflicts if needed
- push your branch
- open PR
Recommended workflow
1. Make sure your work is committed on your feature branch
For example:
git status
git add .
git commit -m "Finish feature X"2. Update your local main
git checkout main
git pull origin mainorigin = Github repository
main = local main branch
This gets the newest version of main from GitHub.
3. Go back to your feature branch
git checkout alan/explore-repo4. Bring the latest main into your branch
Safer / easier option: merge
git merge mainIf there are no conflicts, great.
If there are conflicts:
- open the conflicted files
- fix them
- save them
- then run:
git add .
git commitThat completes the merge.
5. Push your feature branch
git push origin alan/explore-repo- Take my local branch
alan/explore-repoand upload it to GitHub.
6. Open the Pull Request on GitHub
Open a PR from:
alan/explore-repo → mainFull command flow
git checkout alan/explore-repo
git add .
git commit -m "Finish feature X"
git checkout main
git pull origin main
git checkout alan/explore-repo
git merge main
git push origin alan/explore-repoThen open the PR.
Why do this before PR?
Because if main changed, your branch may be:
- behind
main - harder to review
- more likely to conflict later
So before pushing PR, it is good to sync your branch with the latest main.
What if I already pushed my branch before?
That is okay too.
You can still do:
git checkout main
git pull origin main
git checkout alan/explore-repo
git merge main
git push origin alan/explore-repoYour PR will update automatically.
Can I use rebase instead?
Yes, but for now I would suggest merge.
Rebase version
git checkout main
git pull origin main
git checkout alan/explore-repo
git rebase main
git push --force-with-lease origin alan/explore-repo
This gives cleaner history, but because rebase rewrites commits, you usually need:
git push --force-with-leaseThat is why merge is easier for now.