1

Let's say I have a git remote named alice. This remote has hundreds of branches, but I only fetched a few branches from it, using git fetch alice some-branch, git fetch alice another-branch etc.

Now, I want to sync from alice only the branches that I already have - I don't want to fetch all of them. How do I do it?

jakub.g
  • 38,512
  • 12
  • 92
  • 130

2 Answers2

1

I would rather set up specific fetch refspecs in the local config of my repo, rather than relying of bash magic parsing.

git config remote.alice.fetch 'refs/heads/branch1/*:refs/remotes/origin/branch1/*'
git config --add remote.alice.fetch 'refs/heads/branch2/*:refs/remotes/origin/branch2/*'
git config --add remote.alice.fetch 'refs/heads/branch3/*:refs/remotes/origin/branch3/*'

That way, git fetch alice would only fetch the specified branches.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

I didn't find a simple solution but this one works for me:

git branch -ar | sed 's|^[ \t]*||' | grep -e '^alice/' | sed 's|alice/||' | xargs git fetch alice

Explanation:

  • list remote branches
  • remove leading whitespace produced by git branch
  • only keep branches starting from "alice/", and remove that prefix from each branch name
  • pass the names of the branches to git fetch

Or, as a general bash function that can be added to your .bashrc:

# Usage:
#  git-sync-remote <remote-name>
# Example:
#  git-sync-remote alice
git-sync-remote(){
  REMOTE_NAME="$1"
  if [ -z "${REMOTE_NAME}" ] ; then
    echo 'You need to provide remote name'
    return
  fi
  git branch -ar | sed 's|^[ \t]*||' | grep -e "^${REMOTE_NAME}/" | sed "s|${REMOTE_NAME}/||" | xargs git fetch "${REMOTE_NAME}"
}
jakub.g
  • 38,512
  • 12
  • 92
  • 130