14

I have following branches in remote origin.

ft_d_feature_abc
ft_d_feature_xyz
ft_d_feature_lam
ft_d_feature_ton
ft_m_feature_mak
ft_m_feature_echo
ft_m_feature_laa
ft_m_feature_pol

I want to fetch branches which are starting with ft_d. How can I achieve this with git fetch? My Git version is 1.7.9.5.

3 Answers 3

17

Since Git version 2.6, you can specify partial substrings (with simple glob-style matching) in a fetch or push refspec. Hence:

git fetch origin 'refs/heads/ft_d*:refs/remotes/origin/ft_d*'

would do what you are asking for. Generalized regular expressions are not available, nor generalized globs: only the one specific case of * matching everything between two fixed strings is allowed. (Some fixed strings here may be empty. Usually the back half is, i.e., we usually fetch refs/heads/*, not refs/heads/*x to get just branches whose name ends in x.)

If your Git is older than 2.6, there is no simple way to do this in one fetch. You will need multiple fetches, in a loop, and you will need to do your own name-matching, perhaps using the output from git ls-remote.

(Of course, git fetch origin will normally just bring everything over, as specified in the remote.origin.fetch configuration entry or entries, and in most cases there's little point in constraining git fetch this way. Are you sure you want to bother?)

Sign up to request clarification or add additional context in comments.

2 Comments

My Git version is 1.7.9.5. I am going to update my git and will check this.
This works. As I am new to git I was not able to understand this completely. I had to read git-scm.com/book/en/v2/Git-Internals-The-Refspec to understand this solution.
1

I'm not sure Git supports regexes for fetching a branch, however, you can create a simple script that does it for you:

for i in {1..3};
do
    git fetch origin ft_d_feature$i
done

1 Comment

for simplicity I wrote feature1,2,3. There is no sequence in my branch names. I have updated my question.
1

Another way is list your remote branches:

PREFIX='ft_d_feature_'

for BRANCH in $(git branch | cut -c 3-); do
    if [[ $BRANCH = $PREFIX* ]] ; then
        echo $BRANCH;
    fi
done

Edit

To actually call git fetch for each branch:

PREFIX='ft_d_feature_'

for BRANCH in $(git branch | cut -c 3-); do
    if [[ $BRANCH = $PREFIX* ]] ; then
        git fetch origin $BRANCH
    fi
done

Edit2

Another way is creating a git alias, you can add it in your ~/.gitconfig file.

[alias]
     fetch-prefix = "!f(){ for BRANCH in $(git branch | cut -c 3-); do case $BRANCH in $1*) git fetch origin $BRANCH; esac; done; }; f"

Usage: git fetch-prefix ft_d_feature_

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.