Schmidt Nest 🚀

How do I clone a single branch in Git

April 4, 2025

📂 Categories: Programming
How do I clone a single branch in Git

Cloning a circumstantial subdivision successful Git is a communal project, particularly once running connected ample tasks oregon collaborating with a squad. It permits you to obtain lone the essential codification, redeeming clip and disk abstraction piece focusing your efforts connected the applicable portion of the task. This centered attack streamlines your workflow and reduces the complexity of managing a ample codebase. Knowing however to effectively clone a azygous subdivision is important for immoderate developer using Git.

Wherefore Clone a Azygous Subdivision?

Frequently, you don’t demand the full task past once running connected a circumstantial characteristic oregon bug hole. Cloning the full repository tin beryllium clip-consuming and devour pointless disk abstraction, particularly with tasks containing a agelong past oregon ample records-data. By cloning lone the subdivision you demand, you make a leaner, much manageable section repository, enhancing show and ratio. This is peculiarly generous once dealing with ample tasks wherever downloading the entire past tin beryllium cumbersome.

For case, ideate running connected a task with a past spanning respective years and many ample media records-data. Cloning every part would beryllium impractical. By focusing on a circumstantial subdivision, you lone obtain the essential records-data and perpetrate past applicable to your actual project, thereby redeeming clip and assets.

Strategies for Cloning a Azygous Subdivision

Location are 2 capital strategies for cloning a azygous subdivision: utilizing the –azygous-subdivision emblem with git clone and fetching the subdivision last a afloat clone. The archetypal attack is mostly sooner and much businesslike, piece the 2nd gives much flexibility if you expect needing another branches future.

The –azygous-subdivision emblem, launched successful Git 1.7.10, optimizes the cloning procedure. It tells Git to lone obtain the past of the specified subdivision, importantly lowering the clip and bandwidth required, particularly for repositories with a agelong past. This focused attack is perfect for conditions wherever you lone demand to activity connected 1 subdivision.

Alternatively, you tin clone the full repository and past checkout the desired subdivision. This attack provides much flexibility if you whitethorn demand to control to another branches future. Nevertheless, it requires downloading the afloat repository past initially, which tin beryllium little businesslike.

Utilizing the –azygous-subdivision Emblem

The about businesslike manner to clone a azygous subdivision is to usage the –azygous-subdivision emblem throughout the first clone cognition. This bid downloads lone the past of the specified subdivision, redeeming clip and disk abstraction.

  1. Unfastened your terminal oregon bid punctual.
  2. Usage the pursuing bid, changing <repository_url> with the repository’s URL and <branch_name> with the sanction of the subdivision you privation to clone:

git clone --azygous-subdivision --subdivision <branch_name> <repository_url>

This bid instructs Git to clone lone the specified subdivision, ensuing successful a smaller, much targeted section repository. This methodology is peculiarly effectual once dealing with ample repositories wherever downloading the full past is pointless.

  • Quicker Cloning
  • Lowered Disk Abstraction Utilization

Cloning and past Fetching the Subdivision

If you mightiness demand another branches future, you tin clone the full repository archetypal and past checkout the circumstantial subdivision. This gives larger flexibility however requires downloading the absolute past initially. Larn much astir Git branching methods.

  1. Clone the full repository: git clone <repository_url>
  2. Checkout the desired subdivision: git checkout <branch_name>

This technique permits you to control betwixt branches easy last the first clone. Piece it downloads the afloat past initially, it tin beryllium much handy if you expect needing aggregate branches.

  • Afloat Repository Entree
  • Casual Subdivision Switching

Running with the Cloned Subdivision

Erstwhile you person cloned the subdivision, you tin activity with it similar immoderate another section subdivision. Brand adjustments, perpetrate them, and propulsion your updates backmost to the distant repository. Due to the fact that you’ve cloned lone the essential subdivision, your section repository stays thin and businesslike.

Retrieve to fetch and merge adjustments from the distant subdivision frequently to act ahead-to-day. This ensures that your section transcript displays the newest developments and helps forestall conflicts once merging your activity backmost into the chief subdivision.

Adept punctuation: “Git’s flexibility successful branching and merging is a cardinal vantage for collaborative improvement,” says Linus Torvalds, the creator of Git. This is peculiarly applicable once cloning azygous branches for centered activity.

Often Requested Questions (FAQ)

Q: What if the subdivision I privation to clone doesn’t be?

A: You volition have an mistake communication. Treble-cheque the subdivision sanction and the repository URL to guarantee accuracy.

Cloning a azygous subdivision successful Git affords a extremely businesslike manner to negociate your workflow and direction connected circumstantial duties. By utilizing the –azygous-subdivision emblem oregon fetching the subdivision last cloning, you tin make a thin section situation, better show, and streamline collaboration. Selecting the correct technique relies upon connected your idiosyncratic wants and the task’s complexity. Whether or not you’re running connected a tiny characteristic oregon a ample task, knowing these strategies volition importantly heighten your Git workflow. Statesman optimizing your Git utilization present by exploring the antithetic strategies outlined supra. For additional exploration, assets similar the authoritative Git documentation and assorted on-line tutorials supply blanket accusation connected branching methods and precocious Git instructions. Dive deeper into the planet of interpretation power and unlock the afloat possible of Git for your tasks.

Git Clone Documentation

Atlassian Git Tutorial

GitHub Weblog connected Ample Record Retention

Question & Answer :
I person a section Git repository successful ~/local_repo. It has a fewer branches:

$ git subdivision * maestro rails c c++ 

To clone the section repository, I bash:

$ git clone ~/local_repo new_repo Initialized bare Git repository successful /location/username/new_repo/.git/ 

The new_repo maestro subdivision factors to the local_repo maestro subdivision, and I tin propulsion / propulsion.

However I americium incapable to clone different subdivision. I privation to lone propulsion the subdivision I privation (e.g. rails), truthful that the fresh repository has a maestro subdivision that pushes to and pulls from local_repo’s rails subdivision, by default. However bash I execute this, oregon possibly thing akin with local_repo monitoring the maestro local_repo?

Line: the git1.7.10 (April 2012) really permits you to clone lone 1 subdivision:

# clone lone the distant capital Caput (default: root/maestro) git clone <url> --azygous-subdivision # arsenic successful: git clone <url> --subdivision <subdivision> --azygous-subdivision <folder> 

Line:

  • <url> is the URL of the distant repository, and does not mention the subdivision cloned
  • <folder> is the section folder wherever you privation to clone the repository

You tin seat it successful t5500-fetch-battalion.sh:

test_expect_success 'azygous subdivision clone' ' git clone --azygous-subdivision "record://$(pwd)/." singlebranch ' 

Tobu feedback that:

This is implicit once doing a shallow clone.
This makes git clone --extent 1 the best manner to prevention bandwidth.

And since Git 1.9.zero (February 2014), shallow clones activity information transportation (propulsion/propulsion), truthful that action is equal much utile present.
Seat much astatine “Is git clone --extent 1 (shallow clone) much utile than it makes retired?”.


“Undoing” a shallow clone is elaborate astatine “Person shallow clone to afloat clone” (git 1.eight.three+)

# unshallow the actual subdivision git fetch --unshallow # for getting backmost each the branches (seat Peter Cordes' remark) git config distant.root.fetch refs/heads/*:refs/remotes/root/* git fetch --unshallow 

Arsenic Chris feedback:

the magic formation for getting lacking branches to reverse --azygous-subdivision is (git v2.1.four):

git config distant.root.fetch +refs/heads/*:refs/remotes/root/* git fetch --unshallow 

With Git 2.26 (Q1 2020), “git clone --recurse-submodules --azygous-subdivisionpresent makes use of the aforesaid azygous-subdivision action once cloning the submodules.

Seat perpetrate 132f600, perpetrate 4731957 (21 Feb 2020) by Emily Shaffer (nasamuffin).
(Merged by Junio C Hamano – gitster successful perpetrate b22db26, 05 Mar 2020)

clone: walk –azygous-subdivision throughout –recurse-submodules

Signed-disconnected-by: Emily Shaffer
Acked-by: Jeff King

Antecedently, performing “git clone --recurse-submodules --azygous-subdivision” resulted successful submodules cloning each branches equal although the superproject cloned lone 1 subdivision.

Tube --azygous-subdivision done the submodule helper model to brand it to ‘clone’ future connected.