Monday, March 11, 2013

Managing git remote branches

When working with remote repositories with many branches, sometimes git bash responds a bit sluggishly. 

Fortunately there are few steps which can help to overcome problems with huge branches.

Clone with --single-branch

             git clone --single-branch [remote URL] [local folder]

With this git will not try to get references of all remote branches. As such getting all remote references is not an issue as git brings just references. However when checking for branches and inspecting remote, output gets cluttered with lot of references which are hardly of interest. More over with '--single-branch' remote spec is configured for default branch(master) only.  Further 'git remote update' will not try to check all references.
And to get to check all branches on remote repository 'git ls-remote [origin]' can be used. This just gets a view of all remote references rather than populating them.

However this comes with a slight discomfort. To be able to get a another branch, it is required to update refspec manually in config. Say to checkout remote branch b1, that branch should be added in config under remote.

            [remote "origin"]
            url = [remote url]
            fetch = +refs/heads/master:refs/remotes/origin/master
            fetch = +refs/heads/b1:refs/remotes/origin/b1

Then 'git remote udpate' updates b1. And it can be checkout as usual with 'git checkout b1'.

Similarly when remote branches are not of use but can not be removed from host repository they can be deleted locally

            git branch -r -d origin/b1

And pull, push and tracking for b1 goes normally.