1

I would like to create a git alias that clones all branches.

We have the bash script, thanks to this post: How to clone all remote branches in Git?

Here is the bash script (multi-line version):

    #!/bin/bash
    for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do
        git branch --track "${branch##*/}" "$branch"
    done

(one line version):

    git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs

Let's call the git alias git cloneallbranches

I have tried setting both one-line and multi-line versions using:

$ git config --global alias.cloneallbranches '...'

and tried to paste both versions into my .gitconfig file unsuccessfully (I have other git aliases, but none are bash scripts).

Can someone help me alter a bash script, so that I can paste it into my .gitconfig file, to make the git alias works properly?

Thank you.


ANSWER:

Running a separate bash script as a "git alias" answer in reply below.

However, for those who want a quick way to add a git alias, $ git clone-all-branches here is an answer:

Create a "git alias" which will run a script:

$ git config --global alias.clone-all-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'

Now you can run (from any directory that has a git repo):

$ git clone-all-branches

Community
  • 1
  • 1
Kobi
  • 4,003
  • 4
  • 18
  • 23

2 Answers2

1

Thanks to @g-sliepen suggestion, I did the following:

For Ubuntu:

#1 Go to home directory:

`$ cd ~`

Note: Should be same directory as .gitconfig and .bashrc files, which you will need in a shortly.

#2 Create a new file, a bash script, that we will execute later (make sure you are in the home directory):

`$ touch .gitcloneallbranches.sh`

Open the file:

`$ subl .gitcloneallbranches.sh`

Paste and save the following inside your file:

# Bash script that we will execute as an "alias" either as a "git alias" or a "bash alias"
#!/bin/bash
git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs`

#3 Give that file the permissions it needs to be executed:

`$ chmod u+x .gitcloneallbranches.sh`

Use script as #4 a "git alias" -OR- as #5 a "bash alias"

#4 Create a "git alias" which will allow you to run this script as a git command:

  • [Skip to #5 if you prefer a bash alias]

PICK ONE of A -OR- B ways to create the git alias (the "bash alias" is #5):

(A: via terminal)

$ git config --global alias.clone-all-branches '!sh ~/.gitcloneallbranches.sh'

(OR, B: or, add alias directly in .gitconfig file:)

In home directory, open .gitconfig:

$ subl .gitconfig

Under the [alias] section, paste the following: [alias] clone-all-branches = !sh ~/.gitcloneallbranches.sh

Try our git alias!

Go to directory where your git repo resides.

  • (Note: If a repo does not already exist, do a normal "git clone git@..." command, which will clone the master branch first, before our new "git clone-all-branches" command will work.)

View the current branches:

$ git branch // Should be the current branches available

Try our new git alias:

$ git clone-all-branches

View branches again:

$ git branch // All branches now available!

#5 Alternatively, create a "bash alias" to run our .gitcloneallbranches.sh script:

  • [Instead of #4, open .bashrc (or .bash_profile or .profile)]:

$ subl .bashrc

Inside that file, add an "alias" by adding the following:

# Adds ability to clone all branches of a repo, by typing 'clone-all-branches' in any directory with a git repo: alias clone-all-branches='source ~/.gitcloneallbranches.sh'

Try our bash alias!

Go to directory where your git repo resides.

  • (Note: If a repo does not already exist, do a normal "git clone git@..." command, which will clone the master branch first, before our new "clone-all-branches" command will work.)

View the current branches:

$ git branch // Should be the current branches available

Try our new git alias:

$ clone-all-branches

View branches again:

$ git branch // All branches now available!

Kobi
  • 4,003
  • 4
  • 18
  • 23
  • 1
    Just a word of warning: the `chomp` function in the perl script will remove all leading words that end in a slash, so if your branch names include a slash (eg. feat/some-feature), it well end up being some-feature locally. – Josiah Nunemaker Feb 24 '21 at 19:21
0

There is an alternative way: just create a script called git-cloneallbranches that lives somewhere in your $PATH (for example, if you have $HOME/bin in your $PATH, that might be a good place). When you run git cloneallbranches, it will then automatically run your script.

G. Sliepen
  • 7,637
  • 1
  • 15
  • 31