terryp
terryp

Reputation: 249

Approaches to Working With Remote Branches in Git

Here's a random git question. I was looking at all of the ways to look @ remote branches. I feel like there are three ways ... maybe more.

  1. git switch --create < local branch name > origin/< remote branch name >
  2. git checkout -b < local branch name > origin/< remote branch name >
  3. git checkout -t origin/< remote branch name >

Two questions:

  1. Are these the three primary ways to work on a remote branch?
  2. Is there any benefit to these approaches or is it just TMTOWTDI (there's more than one way to do it)?

Upvotes: 1

Views: 38

Answers (1)

torek
torek

Reputation: 489083

Technically, none of these cause you to work on a remote-tracking name1 like origin/master or origin/feature/short. They just create a (regular, i.e., local) branch name like master or feature/short that has origin/master or origin/feature/short set as its upstream.

In this case, it's a massive set of TMTOWTDI, since you can also do:

git checkout feature/short

which will "DWIM" into git checkout -t origin/feature/short, and/or you can use git branch to create the branch-name at any starting commit you like, then use git branch --set-upstream-to to set the upstream of the name you have created.

Once you have used git checkout or git switch to be on the (local) branch (name), new commits you make will update that name in the usual way. And as eftshift0 commented, you can use a detached HEAD to view any particular commit, including those identified by remote-tracking names.


1I prefer this term, remote-tracking name, over the official Git terms, remote-tracking branch name and the like. These names do track other Gits' branch names, so "remote-tracking-branch-name" (all as one big phrase) is appropriate, but it's really easy to shorten this to "remote-tracking-branch" and then convert that to "remote-tracking branch" and then think that you could be on one of these in the same way you can be on a (real / local) branch name, and you can't.

(That, and the poor word branch gets beaten well past the point of its death. We already do this to too many words, like tree; we can be kind and just omit the word branch entirely, with no loss of meaning.)

Upvotes: 2

Related Questions