0

I have 16 branches in my project and I wanted to clone only 4 of then named Dev, UAT, Preprod, Prod, how do i do?

If I clone this will clones all the branches.

I know how to clone single branch.

poke
  • 369,085
  • 72
  • 557
  • 602
Deva
  • 1,851
  • 21
  • 22

2 Answers2

2

clone is a convenience shortcut that addresses some common use cases. It does not directly support your use case, though you can still use it to simplify the process if you want.

What clone Does

Clone initializes a repo, sets the default remote (origin), sets a refspec, fetches, and (unless you tell it otherwise) checks out the default branch.

What --single-branch Does

The single-branch option mostly just changes the refspec so that fetch, by default, will only copy the one branch.

This has trade-offs. It means your repo will silently ignore things like new branches. Those cons may be small for a given use case, but at the same time the pros of limiting what you fetch will also almost always be small. (If they aren't, you might want to consider whether there are other solvable problems affecting your repo, such as large files in the history that should be managed by LFS.)

How to Get Four Branches

You can still use clone with --single-branch to get things started. Then you just need to modify the default refspec for the remote so that it will fetch all four branches. You do this by setting remote.origin.fetch (assuming the remote is called origin); see the git config docs at https://git-scm.com/docs/git-config and search for remoate.<name>.fetch

# having cloned with single branch Dev...
git config --add remtoe.origin.fetch UAT refs/remotes/origin/UAT
git config --add remtoe.origin.fetch Prepod refs/remotes/origin/Preprod
git config --add remtoe.origin.fetch Prod refs/remotes/origin/Prod
Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
-1

Try to clone a single branch first and then fetch the rest.

git clone -b Dev --single-branch <url> -- foo
cd foo
git fetch origin UAT Preprod Prod
git checkout UAT
git checkout Preprod
git checkout Prod
ElpieKay
  • 27,194
  • 6
  • 32
  • 53