0

I've been using Git for a little while, but can't seem to find a simple answer for the question "When I clone a Git repository do I get the whole thing?"

I know you can clone a repository into a directory, but I got the impression that clones the main branch. What about all the other branches?

I know there are ways using very simple script lines on most platforms to get a copy every remote branch (How to clone all remote branches in Git?).

So as the documentation for Git (https://git-scm.com/book/en/v2/Git-Basics-Getting-a-Git-Repository) describes distributed repositories as being complete backups of each other (up to however in sync they are), is my cloned repository on the development branch a complete copy, or am I missing information from the release/staging branches for example?

Apologies in advance if this has been asked elsewhere or in a different way, but I couldn't find anything with a simple definition.

edit: Please provide any link to any official documentation if possible to backup any statements. The question is relating to getting a whole copy of a repository with all the information such as which commits all the branches point to.

Community
  • 1
  • 1

5 Answers5

1

The answer is: Yes you get the whole thing. As stated in git clone documentation

Clones a repository into a newly created directory, creates remote-tracking branches for each branch in the cloned repository (visible using git branch -r), and creates and checks out an initial branch that is forked from the cloned repository’s currently active branch.

If you want to clone single branch you do as in here

Community
  • 1
  • 1
Lemonov
  • 476
  • 4
  • 17
  • Are you able to provide a link to any official documentation or evidence of this? It was my feeling, but I didn't have any proof to back it up. I know that cloning a single branch is possible. – Daniel Abbatt Sep 01 '15 at 07:43
  • Easy way to demonstrate it: Clone a repo, disconnect from the network, switch branch (or do other stuff with your repo). Observe if it works or not. – Sysyphus Sep 01 '15 at 07:45
  • Yeah I figured this, but was hoping there would be documentation to support it. Looks like really just a question of understanding further what it means. I always thought that the remote branch references were information about branches that were on the remote, I didn't realise that there was actually local meta information being stored too so that the branches could be switched to. – Daniel Abbatt Sep 01 '15 at 07:53
1

The short answer is yes. You can see what branches are available after the clone by running: git branch

Documentation here: http://git-scm.com/docs/git-clone. You can see that branches are checked out under the description: "Clones a repository into a newly created directory, creates remote-tracking branches for each branch in the cloned repository"

You should also note that if your repository has submodules you will have to add the --recursive flag to pull in these modules. Hope this helps.

kindagonzo
  • 1,468
  • 2
  • 10
  • 8
1

Actually there's a subtlety which most answers miss. A clone apparently does copy all branches BUT you cannot see them with 'git branch -a'. All you will see is the local master branch which is created from the currently active remote branch. I just ran into this cloning a forked repo, and what we wanted was the non-master branch called 'dev', which was not currently active in the remote. So 'git branch -a' only showed the local copy of the remote master branch and all the remote branches - the local 'dev' branch had no reference and appeared to be missing.

git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master
  remotes/upstream/dev
  remotes/upstream/master

In fact it appears (by testing, not documentation - please show me if you know of any) that 'dev' did get cloned but is not accessible until you create it locally with tracking to its remote branch, with (in my case for 'dev' branch):

git branch --track dev origin/dev

after which we have:

git branch -a
* dev
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master
  remotes/upstream/dev
  remotes/upstream/master

and in fact the new 'dev' branch is not empty but is already populated with the correct files from the remote origin/dev branch, without first fetching, so I believe the branch was lurking there in the clone all along, just not accessible.

See https://git-scm.com/docs/git-clone which hints at this but does not explicitly and clearly state it.

bboyes
  • 11
  • 3
0

When you clone the repository you get everything from the central repository. All the branches and not just the master branch.You can test using git branch -a.

harshitpthk
  • 4,058
  • 2
  • 24
  • 32
0

In some cases, when you clone a local repository, Git uses hardlinks to save disk space and time. If you want clone all of objects you must use :

git clone --no-hardlinks

In the doc (http://git-scm.com/docs/git-clone) you will find an explanation of this argument.

Samuel Dauzon
  • 10,744
  • 13
  • 61
  • 94