GitVersion Control

Updating your Local Branch from a Specific Remote Branch in Git

githubgit

Whether you’re collaborating on a feature or just keeping your work in sync, pulling changes to a non-main branch is a daily task for most developers. Here is how to do it cleanly.

#The Standard Approach

If you are already working on your local branch (let’s call it feature-login), You can pull changes directly from the remote version of that branch.

# 1. Switch to your feature branch
git checkout feature-login

# 2. Pull changes from the remote 'origin' into your current branch
git pull origin feature-login

What happens behind the scenes?
The git pull command is actually a combination of two actions:

  1. git fetch: It downloads the new data from the remote repository.
  2. git merge: It immediately tries to combine those changes into your local files.

#The Cleanest Method: Rebase

If you have local commits that haven’t been pushed yet, a standard pull creates a “merge commit.” To keep your history linear and easy to read, use the rebase flag.

# Pull and replay your local work on top of the incoming changes
git pull --rebase origin feature-login

#The Pro Workflow: Step-by-Step

If you want total control and want to see what’s coming before you merge it, follow this three-step flow:

1. Update your local “view” of the remote

git fetch origin

This updates origin/feature-login in your local database, but doesn't touch your code yet.

2. Compare the differences

git log HEAD..origin/feature-login --oneline

This shows you exactly which commits are on the server that you don’t have yet.

3. Bring the changes in

git merge origin/feature-login

Common Gotchas

  • Merge Conflicts: If you and a teammate edited the same line, Git will pause. You’ll need to open the files, pick the correct code, and run git commit to finish.
  • Wrong Branch: Always run git branch before pulling to ensure you aren't accidentally pulling feature code into your main branch.

Updating your Local Branch from a Specific Remote Branch in Git was originally published in The Owl on Medium, where people are continuing the conversation by highlighting and responding to this story.


Originally published on Medium.