• 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, main has moved forward a bit.

Best Practice

Before opening a PR:

  1. finish your feature
  2. update local main
  3. merge main into your feature branch
  4. fix conflicts if needed
  5. push your branch
  6. 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 main

origin = 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-repo

4. Bring the latest main into your branch

Safer / easier option: merge

git merge main

If there are no conflicts, great.

If there are conflicts:

  • open the conflicted files
  • fix them
  • save them
  • then run:
git add .  
git commit

That completes the merge.

5. Push your feature branch

git push origin alan/explore-repo
  • Take my local branch alan/explore-repo and upload it to GitHub.

6. Open the Pull Request on GitHub

Open a PR from:

alan/explore-repo  →  main

Full 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-repo

Then 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-repo

Your 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-lease

That is why merge is easier for now.