Currently Available: Need a skilled Software Developer for your next project?
Categories
Git

Git: Pulling changes from the main/master branch into your feature branch

Pulling changes from the main branch into your feature branch is a common task before pushing changes and creating a pull request/merge request.

Note: This guide uses "main" as the primary branch name, which is common on GitHub. Some repositories or other hosting services might use "master" instead. Adjust commands as necessary for your specific setup.

Updating Your Feature Branch

Prerequisites

  • You're working on an existing feature branch
  • You want to incorporate the latest changes from the main branch

Steps

  1. Save your current work:

    git stash save "Work in progress"
  2. Fetch the latest changes:

    git fetch origin
  3. Update the main branch locally:

    git checkout main
    git pull origin main
  4. Switch back to your feature branch:

    git checkout feature/your-branch-name
  5. Merge or rebase with main:

    Option A - Merge (simpler, preserves history):

    git merge main

    Option B - Rebase (cleaner history, requires force push if branch is shared):

    git rebase main
  6. Resolve any conflicts (if they occur):

    • Use git status to see conflicting files
    • Edit files to resolve conflicts
    • git add the resolved files
    • Continue the process:
      • For merge: git merge --continue
      • For rebase: git rebase --continue
  7. Reapply your stashed changes:

    git stash pop
  8. Test your changes thoroughly

  9. Push your updated feature branch:

    git push origin feature/your-branch-name

    (Use --force-with-lease if you rebased and the branch was already pushed)

Best Practices

  • Update your feature branch with main regularly to minimize conflicts
  • Always test after merging or rebasing
What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Subscribe to my newsletter

Get new posts when I publish them.

I respect your privacy. Unsubscribe at any time.

Leave a Reply

Your email address will not be published. Required fields are marked *