0

At my place of work we have multiple repos for our project and every sprint we create a new release branch we work from and it is getting really old checking them out one at a time. Is there a Git command or script I can use to checkout a particular branch, say 9.5, on every repo at one time?

  • What do you mean that you have multiple repos? Are you saying you cloned the same repo multiple times on your machine? Or do you have multiple code bases each for separate projects that all follow the same versioning scheme and timeline? – Code-Apprentice Jul 26 '18 at 17:28
  • Possible duplicate of [Is it possible to manage multiple repositories coherently?](https://stackoverflow.com/questions/9136085/is-it-possible-to-manage-multiple-repositories-coherently) – phd Jul 26 '18 at 18:07

3 Answers3

5

At work, I keep all the company repositories in one directory that contains a script called forall:

#!/bin/bash
for repo in */ ; do
    (   cd "$repo"
        "$@"
    )
done

And I can cd to the directory and run commands like

./forall git fetch upstream
./forall git checkout relase9.5

Before doing so, you can verify that

./forall git status

reports no local changes. Also, before running pull or reset, use

./forall git branch | grep \*

to check that all the repositories are on the same branch.

choroba
  • 231,213
  • 25
  • 204
  • 289
0

No, git does not have any commands that interact with multiple repos in the way you describe here. You will need to write a .bat (Windows) or .sh (Linux or Git Bash) script to do this.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

If you want you can iterate trough all submodules doing something like:

 function subcheckout() {
  branch=${1:-develop}
  default=${2}

  if [[ ! -d .git ]]; then
        echo "not a git repo"
        return 1
    fi;

    if ! [[ `git submodule status` ]]; then
        echo 'not submodule'
        return 1
    fi

    submodules=($(git config --file .gitmodules --get-regexp path | awk '{ print $2 }'))

    currentDirectory=$(pwd)

    for submodule in "${submodules[@]}"
    do
        printf "\n\nEntering '$submodule'\n"
        cd "$currentDirectory/$submodule"

        if [[ $(git rev-parse --verify --quiet ${branch}) ]]; then
            git checkout ${branch}
        elif ! [[ -z $default ]] && [[ $(git rev-parse --verify --quiet ${default}) ]]; then
            git checkout ${default}
        fi
    done

  cd "$currentDirectory"
}
Adif_Sgaid
  • 79
  • 10