163

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?

skyork
  • 7,113
  • 18
  • 63
  • 103

5 Answers5

174

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.

Amber
  • 507,862
  • 82
  • 626
  • 550
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.

SexxLuthor
  • 4,460
  • 3
  • 18
  • 25
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • The OP mentions that they're already tracking the remote branch. – Amber Jul 01 '12 at 00:03
  • 11
    @Amber hence my answer: `git pull` is enough. – VonC Jul 01 '12 at 00:03
  • The first command `git branch -f --track master origin/master` returns an error: `fatal: Cannot force update the current branch.` – Mark Kramer Jun 22 '17 at 15:54
  • @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. – VonC Jun 22 '17 at 16:05
  • You should also change it to `--set-upstream-to`, `--set-upstream` is deprecated and going to be removed. – Mark Kramer Jun 22 '17 at 16:06
  • @MarkKramer I didn't realize this answer was so old, thank you. I should have known (https://stackoverflow.com/a/40575748/6309) – VonC Jun 22 '17 at 16:11
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

hx_hxl
  • 95
  • 2
  • 2
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)

HJunk
  • 41
  • 2
  • 1
    Using `git stash` to preserve the uncommitted changes would be a wise first step if the user did care about the changes. – benhorgen Aug 20 '21 at 20:15
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.

alireza
  • 58
  • 5