Update a local branch with the changes from a tracked remote branch

352,499

Solution 1

You have set the upstream of that branch

(see:

git branch -f --track my_local_branch origin/my_remote_branch
# OR (if my_local_branch is currently checked out):
$ git branch --set-upstream-to my_local_branch origin/my_remote_branch

(git branch -f --track won't work if the branch is checked out: use the second command git branch --set-upstream-to instead, or you would get "fatal: Cannot force update the current branch.")

That means your branch is already configured with:

branch.my_local_branch.remote origin
branch.my_local_branch.merge my_remote_branch

Git already has all the necessary information.
In that case:

# if you weren't already on my_local_branch branch:
git checkout my_local_branch 
# then:
git pull

is enough.


If you hadn't establish that upstream branch relationship when it came to push your 'my_local_branch', then a simple git push -u origin my_local_branch:my_remote_branch would have been enough to push and set the upstream branch.
After that, for the subsequent pulls/pushes, git pull or git push would, again, have been enough.

Solution 2

You don't use the : syntax - pull always modifies the currently checked-out branch. Thus:

git pull origin my_remote_branch

while you have my_local_branch checked out will do what you want.

Since you already have the tracking branch set, you don't even need to specify - you could just do...

git pull

while you have my_local_branch checked out, and it will update from the tracked branch.

Solution 3

for somebody accidently mess the local commits.

delete local dirty branch

git branch -D master

then rebuild a branch from remote

git checkout -b master origin/master

Solution 4

Note: I am a git novice.

When I do a "git pull", I usually see "error: Your local changes to the following files would be overwritten by merge:" "Please commit your changes or stash them before merge." (Because I've made minor temp changes that I don't really care about.)

I typically don't care about my changes if I am pulling from remote. I just want the latest that the team has pushed. (I have used "stash" on occasion to keep some changes.)

So, what I do to pull the latest from remote and wipe out any of my local changes:

git reset --hard (for current branch)

or

git reset --hard origin/master (for going back to master)

then:

git pull (pulls the current remote files to my local)

Share:
352,499
skyork
Author by

skyork

Updated on January 24, 2022

Comments

  • skyork
    skyork over 2 years

    I have a local branch named 'my_local_branch', which tracks a remote branch origin/my_remote_branch.

    Now, the remote branch has been updated, and I am on the 'my_local_branch' and want to pull in those changes. Should I just do:

    git pull origin my_remote_branch:my_local_branch
    

    Is this the correct way?

  • Amber
    Amber almost 12 years
    The OP mentions that they're already tracking the remote branch.
  • VonC
    VonC almost 12 years
    @Amber hence my answer: git pull is enough.
  • Mark Kramer
    Mark Kramer almost 7 years
    The first command git branch -f --track master origin/master returns an error: fatal: Cannot force update the current branch.
  • VonC
    VonC almost 7 years
    @MarkKramer Yes, I have edited the answer to make it clearer the second command is to be used if the local branch is currently checked out.
  • Mark Kramer
    Mark Kramer almost 7 years
    You should also change it to --set-upstream-to, --set-upstream is deprecated and going to be removed.
  • VonC
    VonC almost 7 years
    @MarkKramer I didn't realize this answer was so old, thank you. I should have known (stackoverflow.com/a/40575748/6309)
  • m4l490n
    m4l490n almost 4 years
    This should be the correct answer. It's as simple as that.
  • benhorgen
    benhorgen over 2 years
    Using git stash to preserve the uncommitted changes would be a wise first step if the user did care about the changes.
  • Muzzamil
    Muzzamil over 2 years
    Thanks, It worked for me. Indeed, This is a correct answer.
  • questionto42standswithUkraine
    questionto42standswithUkraine about 2 years
    git branch -d master gives me: error: Cannot delete branch 'main' checked out at MY_BRANCH.