Search code examples
gitremote-branch

How can I convert all the remote branches in a local git repo into local tracking branches


How can I convert all the remote branches in a local git repo into local tracking branches, without one by one checking each one out.

One reason why you might want to do this (the reason I want to do this) is so that you can take a clone of the local repo and have in that new clone all the branches from the original remote origin.

Because "clone" only clones local branches.

Edit: a couple of scripted answers have been provided (for which - thanks!) ... I was really hoping for an in-git way, so that it is completely portable (I have users who are "windows only", and so far have survived without having to use a bash (git-bash or otherwise)).


Solution

  • The best way to do this probably is with a script:

    #!/bin/bash
    IFS=$'\n'
    for branch in `git branch -r`; do
        if [[ ${branch} =~ ^\ *(.+)/(.+)$ ]]; then
            git show-branch "${BASH_REMATCH[2]}" > /dev/null 2>&1
            if [ $? -ne 0 ]; then
                git branch ${BASH_REMATCH[2]} ${BASH_REMATCH[1]}/${BASH_REMATCH[2]}
            fi
        fi
    done