How does one clone a git repository with only a non-master branch?

In cases where you don’t want to download unneeded files.

  • jenkins deletes existing work space ,when trying to clone new git repo from git scm plugin
  • How can i run git commands from java?
  • Git - Cleanup squashed branches?
  • Get Git commit hash from commit message
  • Passing ssh options to git clone
  • How to search and replace a string in the lines added in the last X commits?
  • GitHub for Windows does not see changes in repository
  • What does “warning: unable to unlink website: Operation not permitted” mean when checking out a Git branch?
  • GIT clone over http fails (curl result = 22, http_code 401)
  • How to login Gerrit as Administrator
  • php run git got “ssh Permission denied”
  • Maintaining a Lite and Pro Version of Python Project with Git/PyCharm
  • 2 Solutions collect form web for “How does one clone a git repository with only a non-master branch?”

    git clone always clones the complete repository unless you specify the --depth <n> parameter which limits the repository to the latest n revisions (a so-called “shallow clone”).

    However, you can create a local repository and use git fetch to only fetch parts of the remote repository.

    cd /path/foo
    git init
    git remote add origin <some url>
    git fetch origin <some branch>
    

    This will duplicate most of what git clone does but restrict it to the branch(es) you specify on the command line. (I’m not sure about further details such as tracking branches and tags and the like.)

    Your question title and summary are kind of asking two different questions because, as Bombe noted, a clone always gets all content unless use use the --depth option.

    I’m not sure what you really want but another option is to clone with the --no-checkout (or -n) flag. By default, git will checkout the default branch for the repository (which is determined by the HEAD ref in the remote repository — it’s not always master). If you use the -n flag git will not checkout a branch for you so you can just checkout what you want:

    git clone -n <some url> foo
    cd foo
    git checkout <some branch>
    
    Git Baby is a git and github fan, let's start git clone.