def create_branch(new_branch, base_rev, force=False, no_track=False):
    """Try creating a new branch which tracks the given remote
            if such a branch does not exist then branch off a local branch
        """
    repo = _get_repo()

    # Already exists
    if new_branch in repo.branches:
        if not force:
            raise GitError("branch %s already exists\n use --force to overwrite anyway" % new_branch)

    # fork with new sha
    new_ref = repo._format_ref_branch(new_branch)
    base_sha = find_revision_sha(repo, base_rev)
    repo.repo.refs[new_ref] = base_sha

    #handle tracking, only if this was a remote
    tracking, remote_branch = (['origin'] + base_rev.split('/'))[-2:]  #branch-> origin/branch.  remote/branch stays as is
    qualified_remote_branch = os.path.sep.join([tracking, remote_branch])
    if qualified_remote_branch in repo.remote_branches and base_rev not in repo.branches:
        if not no_track:
            add_tracking(new_branch, tracking, remote_branch)
        else:
            remove_tracking(new_branch)

    #todo reflog
    return new_ref
Try creating a new branch which tracks the given remote
            if such a branch does not exist then branch off a local branch
        
lib/git/git-branch.py