Update a local branch with the changes from a tracked remote branch
Asked Answered
R

5

165

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?

Rights answered 30/6, 2012 at 23:38 Comment(0)
C
92

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.

Cryptic answered 30/6, 2012 at 23:58 Comment(6)
The OP mentions that they're already tracking the remote branch.Condominium
@Condominium hence my answer: git pull is enough.Cryptic
The first command git branch -f --track master origin/master returns an error: fatal: Cannot force update the current branch.Ideograph
@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.Cryptic
You should also change it to --set-upstream-to, --set-upstream is deprecated and going to be removed.Ideograph
@MarkKramer I didn't realize this answer was so old, thank you. I should have known (https://mcmap.net/q/151565/-how-to-push-a-new-branch-non-existing-on-the-remote-server-without-set-upstream)Cryptic
C
180

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.

Condominium answered 1/7, 2012 at 0:1 Comment(0)
C
92

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.

Cryptic answered 30/6, 2012 at 23:58 Comment(6)
The OP mentions that they're already tracking the remote branch.Condominium
@Condominium hence my answer: git pull is enough.Cryptic
The first command git branch -f --track master origin/master returns an error: fatal: Cannot force update the current branch.Ideograph
@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.Cryptic
You should also change it to --set-upstream-to, --set-upstream is deprecated and going to be removed.Ideograph
@MarkKramer I didn't realize this answer was so old, thank you. I should have known (https://mcmap.net/q/151565/-how-to-push-a-new-branch-non-existing-on-the-remote-server-without-set-upstream)Cryptic
M
8

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

Moccasin answered 24/1, 2022 at 7:49 Comment(1)
git branch -d master gives me: error: Cannot delete branch 'main' checked out at MY_BRANCH.Repute
Z
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)

Zonation answered 19/8, 2021 at 16:20 Comment(1)
Using git stash to preserve the uncommitted changes would be a wise first step if the user did care about the changes.Cinchonidine
I
1

As you said your local branch tracked remote upstream so we can use following command:

git checkout -B <new-branch> [<start-point>]

git checkout -B my_local_branch origin/my_remote_branch

If -B is given, <new-branch> is created if it doesn’t exist; otherwise, it is reset.

Inedited answered 18/2, 2023 at 18:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.