2

I am trying to learn git but I am confused about one part. Usually I use:

git pull origin branch_A

to fetch the remote branch. Usually my current local branch is branch_A the branch on the remote repository is also branch_A , so I will get the remote/branch_A -> local/branch_A. But what if I want to get remote/branch_B -> local/branch_A what should I do? What is the real meaning of the branch after origin. It means the remote branch or the local branch? Does this command mean fetch default remote branch to local branch_A or fetch remote branch_A to current local branch?

hidemyname
  • 3,791
  • 7
  • 27
  • 41

2 Answers2

3

In the command

git pull origin branch_A

the origin refers to the remote repository which you have configured in Git. So this will pull changes from the remote branch_A into the local branch which is tracking this remote. On the other hand,

git pull origin/branch_A

will pull the changes from the local version of the origin/branch_A branch which was cached last time you did a pull.

If you really want your local branch_A to track remote branch_B then the following command can do the trick:

git checkout -b branch_A origin/branch_B

If you already have a local branch_A tracking something else (such as remote branch_A) then you will have to kill the branch first and then recreate it.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • if I used branch_A to track origin/branch_A. But I renamed branch_A to branch_C and created a new branch_A. And I use git pull origin branch_A. Will it be pulled to current branch_C or current branch_A? – hidemyname Jul 22 '15 at 06:10
  • `git pull origin branch_A` will update `branch_C`, which is still tracking the remote `branch_A`. Your local `branch_A` won't be updated unless it is also tracking remote `branch_A`. – Tim Biegeleisen Jul 22 '15 at 06:17
  • You probably should not be messing around with the branch names. Please read [this SO article](http://stackoverflow.com/questions/1526794/rename-master-branch-for-both-local-and-remote-git-repositories) for more information on correctly renaming a Git branch locally and remotely. – Tim Biegeleisen Jul 22 '15 at 06:19
  • Thank you pretty much... One last question. Is the push the same with pull? Every time I use 'git push origin branch_A'. Are the commits pushed from default local tracking branch to the remote branch? – hidemyname Jul 22 '15 at 06:22
  • Yes, `git pull` and `git push` behave symmetrically this way. – Tim Biegeleisen Jul 22 '15 at 06:23
  • Hi I just tried. It is true that even after I renamed the 'pull' still goes to branch_C but the 'push' is still 'branch_A -> origin/branch_A' not 'branch_C -> origin/branch_A'. It seems that tracking rule of 'push' follows the name? – hidemyname Jul 22 '15 at 06:44
  • How did you create your `branch_A`? Was it setup to point to the remote branch with the same name? – Tim Biegeleisen Jul 22 '15 at 06:46
  • I checkout to the old version of branch_A and used that head to create a new branch temp. And renamed the branch_A to branch_C. Then renamed temp to branch_A........ – hidemyname Jul 22 '15 at 06:49
  • In this case, after your changes both `temp` and `branch_C` should still be pointing to the remote `branch_A`. Is this not the case? By the way, you should try to avoid playing around with names. I have been using Git for 5 years and I can't even count on 2 hands the number of times I have had the need to shuffle around branch names. – Tim Biegeleisen Jul 22 '15 at 06:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83943/discussion-between-deathlee-and-tim-biegeleisen). – hidemyname Jul 22 '15 at 08:04
0

The branch after the name of the repository is a remote branch.

It is what the doc says git-pull.

You allways pull over the branch that you are in your local repository.

However, there is another possibility, If you have a local branch tracked with a remote branch, then the command means that you will pull in your current branch what is in the remote branch that is pointed by your local branch tracked.

blashser
  • 921
  • 6
  • 13