Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clone just the stable and one other branch in git?

Tags:

git

git-clone

I'm just getting started with git and I have a question. My app has 10 other developers working on it, each one having their own branch like dev_XXXXX. So if I do a clone of the repository, do all of their code gets copied to my machine? In that case I dont want that. Suppose my branch is dev_swamy, how do I then clone just the stable branch and dev_swamy? Thanks.

like image 433
Swamy g Avatar asked Oct 23 '09 19:10

Swamy g


People also ask

Can I git clone a specific branch?

There are two ways to clone a specific branch. You can either: Clone the repository, fetch all branches, and checkout to a specific branch immediately. Clone the repository and fetch only a single branch.

How do I clone down a specific branch?

In order to clone a specific branch, you have to execute “git branch” with the “-b” and specify the branch you want to clone. $ git clone -b dev https://github.com/username/project.git Cloning into 'project'... remote: Enumerating objects: 813, done.

Does cloning repo clone all branches?

The idea is to use the git-clone to clone the repository. This will automatically fetch all the branches and tags in the cloned repository. To check out the specific branch, you can use the git-checkout command to create a local tracking branch.


1 Answers

By default git clone would fetch all branches, but those branches would be stored as remote-tracking branches: for example branch 'dev_XXXXX' would be stored as 'origin/dev_XXXXX' (with 'refs/remotes/origin/dev_XXXXX' as full name). Those remote-tracking branches wouldn't be visible in git branch output: you would need git branch -r to list remote-tracking branches (or git branch -a to list all branches). If those branches do not diverge too much from mainline, they wouldn't take too much disk space in repository. Therefore I don't see why you want to clone only selected branches.

Nevertheless if you want to have a clone with only two selected branches, you can do it like this:

  1. First, create new empty repository

    $ mkdir repoclone
    $ cd repoclone/
    $ git init
    Initialized empty Git repository in /home/user/repoclone/.git/
    
  2. Then add your repository under the name 'origin' (just like "git clone" would name it), requesting tracking of only two branches: 'master' and 'dev_swamy', using "git remote" command. Check that it was added correctly.

    $ git remote add -t master -t dev_swamy origin [email protected]:repo.git
    $ git remote 
    origin
    $ git remote show origin
    * remote origin
      Fetch URL: [email protected]:repo.git
      Push  URL: [email protected]:repo.git
      HEAD branch: master
      Remote branches:
        master          new (next fetch will store in remotes/origin)
        dev_swamy new (next fetch will store in remotes/origin)
    

    If the stable branch is called 'stable' rather than 'master', you would have of course to modify above example. Also there is -m <branch> option if you want specified branch to be default branch in remote.

  3. Fetch from 'origin' (you could do this also by using -f option to "git remote add" above):

    $ git fetch
    remote: Counting objects: 282, done.
    remote: Compressing objects: 100% (193/193), done.
    remote: Total 282 (delta 82), reused 0 (delta 0)
    Receiving objects: 100% (282/282), 81.30 KiB | 135 KiB/s, done.
    Resolving deltas: 100% (82/82), done.
    From [email protected]:repo.git
     * [new branch]      master     -> origin/master
     * [new branch]      dev_swamy -> origin/dev_swamy
    From [email protected]:repo.git
     * [new tag]         v1.0       -> v1.0
     * [new tag]         v1.0.1    -> v1.0.1
     * [new tag]         v1.1       -> v1.1
    
  4. Set up local branch 'master' (where you would do your work) to follow 'origin/master' (to have 'origin/master' as upstream), just like "git clone" would do:

    $ git checkout -t origin/master
    Branch master set up to track remote branch master from origin.
    Already on 'master'
    

    You can repeat this for branch 'dev_swamy'.

  5. Now you can see how config file looks like. You can get exactly the same result by editing .git/config file to look like the following, and then doing "git fetch".

    $ cat .git/config  # or just open this file in your editor
    [core]
            repositoryformatversion = 0
            filemode = true
            bare = false
            logallrefupdates = true
    [remote "origin"]
            url = [email protected]:repo.git
            fetch = +refs/heads/master:refs/remotes/origin/master
            fetch = +refs/heads/dev_swamy:refs/remotes/origin/dev_swamy
    [branch "master"]
            remote = origin
            merge = refs/heads/master
    

Don't forget to introduce yourself to Git before starting work on repository (i.e. set 'user.name' and 'user.email' config variables; usually in per-user config file)!

like image 145
Jakub Narębski Avatar answered Nov 09 '22 15:11

Jakub Narębski