Paul
Paul

Reputation: 4486

Git connect a new branch remotely

enter image description here

I have a similar situation to the one seen in the image in my project.

Let's consider the case that a colleague creates a new branch already pushed, which has no commit linked to the test branch.

So they are two different branches, which have nothing in common.

How can I download the new branch, but not have it locally but in remote/origin?

Do you know if it is possible to do it?

Upvotes: 0

Views: 39

Answers (1)

torek
torek

Reputation: 489858

Git doesn't have branches.1 Git has commits.

You have branches to help you (and Git) find the commits. But all you get are the commits. Get the commits and you're good; don't get the commits, and you're not good.

Let Git get all the commits, as it always does by default, by letting it copy all of the other Git repository's branch names to your remote-tracking names. You will then have remotes/origin/* names by which to find the commits. If you then want to add your own commits to those commits, create a (local) branch name to help you and Git find your commits that you've added to their commits.

If you just want to use their commits, use git switch --detach origin/test to get their latest commit. You don't need a branch name to do this!


1This is, of course, an overstatement. The point of this claim is to shock the reader out of the obsession some people have with "branches". Git overuses the word "branch" to the point of meaninglessness, and whatever you, the reader, think of in your head when you say "branch" has at best a small chance of being right unless you're already so steeped in Git that you automatically correct "branch" to the thing you need in Git.

It's a bit like going to a party where all the people are named Paul, all the snacks are Pauls, the beer is Paul-brand beer, the house is Paul House, and so on. "I'm going to Paul to meet Paul for Paul and Paul..."

Upvotes: 1

Related Questions