jibran
jibran

Reputation: 111

Git: How to checkout newly created remote branch?

A new minor version branch for Drupal 8 is created every 6 months but the default branch remains the previous one. For example, right now the default branch is 8.6.x and the latest branch is 8.7.x.

git clone --depth 1 [email protected]:project/drupal.git

The above command clones the default branch i.e. 8.6.x but I want to clone the newly created remote branch i.e. 8.7.x

git clone --depth 1 --branch $NEWEST_BRANCH [email protected]:project/drupal.git

Is there a way to dynamically set the NEWEST_BRANCH to the newly created remote branch?

Upvotes: 0

Views: 150

Answers (1)

torek
torek

Reputation: 487755

Is there a way to dynamically set the NEWEST_BRANCH to the newly created remote branch?

Not within Git itself, no—there's no notion of "newly created". Moreover, while some may use some variant of semantic versioning, not everyone does (and there are valid arguments against the concept in the first place). Essentially, it's hard even to know what "newest" means.

(Side note: the phrase "remote branch" is not a very good one either, if only because Git muddies the waters by having local entities that are often called "remote branches" in lieu of the more-accurate remote-tracking branch names, or my preferred phrase, remote-tracking names. In this case you just mean "latest" or "newest" on the specified other-Git-repository.)

What I might suggest for this particular issue is using either of the following:

  1. Run git ls-remote, and use that to select a particular branch. You can then use git clone -b <name> --depth 1.

  2. Run git clone --depth 1 --no-single-branch: you will get many depth-1 remote-tracking names, one for each branch on the remote. You can then peruse them and git checkout whichever one you prefer.

Method 1 is more labor-intensive, but I timed cloning 8.6.x (about 10 seconds real time for this particular test) vs --no-single-branch (about 30 seconds real time) so you might prefer it for some sort of automated thing.

Upvotes: 1

Related Questions