Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT: Any way to commit on multiple repos?

Tags:

git

I know this isn't recommended, but I do have folders with multiple projects that I like to keep up to date.

Is there a command that will look for each git repository in a folder and send the following commands..

git add -u 
git add .
git commit -m 'Latest'

So I could just cd into some folder, then run a command that gets them all updated?

This is not a submodules question

like image 632
Kirk Strobeck Avatar asked Oct 20 '11 19:10

Kirk Strobeck


People also ask

Can you have multiple repositories in Git?

With Git, using multiple repositories is the only way to work efficiently. This enables each team to work independently, and do their work faster. You can also make sure that developers only have access to the repositories they need access to (thus making Git more secure.)

How do I commit to another repo?

To push the commit from the local repo to your remote repositories, run git push -u remote-name branch-name where remote-name is the nickname the local repo uses for the remote repositories and branch-name is the name of the branch to push to the repository. You only have to use the -u option the first time you push.

Can Git have multiple remotes?

You can add multiple remotes by using git remote or git config commands or editing the config file.


2 Answers

I use a bash script (in my .bashrc) to run multiple time the same git command on several repos. The bash hardcodes the repo location but it could easily be changed. One of the issues with running multiple times the same command is correctly passing the quoted " arguments, typically -m " commit message"

I run those command like this:

mgit commit -a -m "my commit message" where mgit is a alias in my .bashrc.

This works for any git commant (I least the one I tested and used on a daily basis), eg:

mgit status -uno, mgit pull

function parsing the git arguments

This function ensure any quoted argument (after the -m are correctly handled)

parse_git_args()
{
    git_args=""
    end_comment=""
    for var in "$@"
    do
        if [[ $var == '-m' ]];
        then
            git_args+=" -m \""
            end_comment="\"" 
        else
            git_args+=" $var"
        fi
    done
    git_args+=$end_comment

    echo $git_args
}

function running the same command on several repos

multiple_git_func()
{
    start=$(pwd)
    git_args=$(parse_git_args $@)
    root=`git rev-parse --show-toplevel` #find the repo from which the command was run

    # go to root apply command and then go to sub repos (hardcoded location wrt root)
    cd $root
    echo "=============================="
    echo "issuing git $git_args on $root"
    eval "git $git_args"


    cd repo2 # repo2 is installed in root, could be anywhere else relative to root
    echo "=============================="
    echo "issuing git $git_args on repo2"
    eval "git $git_args"
   
    #go back to initial folder
    cd $start
}

alias in .bashrc

alias mgit=multiple_git_func

like image 60
Jean-Marc Volle Avatar answered Nov 25 '22 14:11

Jean-Marc Volle


why don't you use something like this:

#!/bin/bash
for DIR in `ls`;
do
    if [ -d $DIR/.git ];
    then
            echo "updating location: " $DIR;
            cd $DIR
            # your commands here...
            git add -u 
            git add .
            git commit -m 'Latest'

            cd ..
    fi
done
like image 24
maiklos Avatar answered Nov 25 '22 12:11

maiklos