0

Hello situation is the following:

I have two remote branches: master and dev

dev has more commits than masters (git log).

On my local repository, I have cloned only master. So I'm positioned on master branch.

From this situation I'd like to pull dev branch without affecting the master.

What I did is:

 $(master) : git pull origin dev:dev

But the result was:

I have now my dev branch updated, but also the master branch has been merged to dev branch (unexpected)

So my question is how should I pull from remote a specific branch to a local specific branch (even if I'm not positioned on the target branch)?

I thought the

$git pull origin specific_remotebranch:specific_localbranch

wasn't going to affect my current branch... is it (always), ?

koalaok
  • 5,075
  • 11
  • 47
  • 91
  • What are you asking here? – Tim Biegeleisen Sep 05 '16 at 09:14
  • 1. you clone a repo - 2. on the remote repo has been added a new dev branch - 3. I want to pull this remote branch on my local repo. Question: Is it possible to do so from the current branch (master), without having the current branch being merged to this new remote branch (dev). Is this something so unusual? Another similar issue could be: I have two remote branches and two local branches A & B, how can I update only B branch even if I'm positioned locally on A branch? (i.e. from branch A git pull origin B, without affecting A's history?) – koalaok Sep 05 '16 at 09:20
  • It is completely unclear what you are trying to do, why you want to do this, etc. It _is_ unusual to have to go outside of the standard workflow tools which Git's interface exposes. You are certainly trying hard to do this, but I suspect it is not needed. To be clear, if you pull `dev`, your local `master` will NOT be merged with anything. Nothing happens locally unless you allow it. – Tim Biegeleisen Sep 05 '16 at 09:22
  • well I have dev branch with a different git log (newer commits) form master. I've checkout to master. If I perform (from master) git pull origin dev. The result is that I have master merged to dev. – koalaok Sep 05 '16 at 09:28
  • Can you clearly articulate what you want to do with the `dev` branch? Do you want to merge `master` into `dev`? Do you want to rebase `master` on `dev`, or vice-versa? – Tim Biegeleisen Sep 05 '16 at 09:33
  • My title question is : How to Git pull to a new branch without affecting the current branch? then... is it possible to git pull a remote branch (dev) to a local branch ("dev" : existing or not existing) being positioned on another branch (master) without the risk that current branch (master) history is going to be affected? – koalaok Sep 05 '16 at 09:37
  • Yes, but _why_ do you need to do this? Why not just finish your work on `master`, then switch branches? This is not clear, this is why no one is touching your question. – Tim Biegeleisen Sep 05 '16 at 09:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122647/discussion-between-koalaok-and-tim-biegeleisen). – koalaok Sep 05 '16 at 09:41

2 Answers2

2

I found out what I was looking for:

I was trying to "pull" instead what I really wanted is a fetch + "check out" action

See here: How do I check out a remote Git branch?

//let's make available all remote branches

$git fetch

//let's create a new branch from the remote just fetched

$git checkout dev

It seems there's no a unique pull action to perform this.

Community
  • 1
  • 1
koalaok
  • 5,075
  • 11
  • 47
  • 91
  • I ran into the same issue and this got me fixed. Thanks! I realised that it's our mental model that's wrong: `pull` is [to fetch and **integrate** from another repository](https://git-scm.com/docs/git-pull), while in this case there's no integration (merge or rebase) needed; simply switching to that branch would do. Hence a `checkout` is the right thing to do. – legends2k Mar 30 '17 at 03:20
0

On my local repo I have cloned only master. SO i'm positioned on master branch.

You might be only checking out the master branch, but unless you do something really unusual, you always clone the full repo, including all branches.

You can typically just git fetch to point your local head of dev to the remote head of dev.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • yes I agree with you, but unfortunately dev branch, as it happened to me, has been created after while. The point of my question is how to pull a rbranch to a lbranch without affecting the current one? The syntax I've posted and believed it was the correct one to affect the specific branch is not going to work. Even If I have all branches locally, If I git pull from another branch, the current branch is going to be dramatically merged... – koalaok Sep 05 '16 at 08:48
  • so your answer is my answer? – Marcus Müller Sep 05 '16 at 18:30