GithubHelp home page GithubHelp logo

d-e-s-o / git-foreach Goto Github PK

View Code? Open in Web Editor NEW
1.0 4.0 0.0 22 KB

Perform a git command on multiple repositories.

License: GNU General Public License v3.0

Shell 100.00%
git-foreach git-command git shell-script

git-foreach's Introduction

git-foreach

Purpose

git-foreach is a script performing git commands on a number of repositories and, thus, allowing for a more flexible management of multiple repositories. The script works by finding all repositories below the current working directory and invoking the supplied command there. By default, if no custom command is specified, each repository's status will be displayed.

Usage

A sample invocation can look like this:

$ git foreach
/dir/repo3
 M modified-file.py
/repo2
D  deleted-file.asm
/repo1
A  newly-added-file.c

In order for batch operations to be able to reference the repository a command is invoked in, git-foreach provides a special variable, REPO. The variable contains the name of the very repository the command is invoked in (i.e., the name of the directory the repository is contained in).

An example incorporating this variable looks as follows:

$ git foreach remote add github '[email protected]:d-e-s-o/${REPO}.git'
$ git foreach remote -v
/dir/repo3
github  [email protected]:d-e-s-o/repo3.git (fetch)
github  [email protected]:d-e-s-o/repo3.git (push)
/repo2
github  [email protected]:d-e-s-o/repo2.git (fetch)
github  [email protected]:d-e-s-o/repo2.git (push)
/repo1
github  [email protected]:d-e-s-o/repo1.git (fetch)
github  [email protected]:d-e-s-o/repo1.git (push)

It is important to properly escape the variable reference in order to avoid any substitution by the currently running shell. In the example above, the correct behavior is achieved by putting the string containing the variable reference in single quotes.

In case one is unsure about the proper escaping or just wants to double check the to-be-invoked command, the -n/--dry-run option can be used:

$ git foreach --dry-run remote add github '[email protected]:d-e-s-o/${REPO}.git'
./dir/repo3
git remote add github [email protected]:d-e-s-o/repo3.git
./repo2
git remote add github [email protected]:d-e-s-o/repo2.git
./repo1
git remote add github [email protected]:d-e-s-o/repo1.git

Using this option, the command that is about to be executed is shown for every repository. No action aside from that is actually performed.

As was apparent from the examples shown so far, git-foreach by default prints the directory an action is performed (or is to be performed) in before the result of the command is displayed. Using the -q/--quiet option, the script can be silenced:

git foreach --dry-run --quiet remote add github '[email protected]:d-e-s-o/${REPO}.git'
git remote add github [email protected]:d-e-s-o/repo3.git
git remote add github [email protected]:d-e-s-o/repo2.git
git remote add github [email protected]:d-e-s-o/repo1.git

If you want to pass additional command line options to the git command to run in each repository, you should separate the command along with its options so that those options are not interpreted by git-foreach. E.g.,

git foreach -- diff -U10

Installation

Being a simple shell script, installation is straight forward: The script needs to be made executable and included in the command search path (in general represented by the PATH variable).

Due to the script's name, git will recognize the command as well, so it can be invoked as git foreach but also via git-foreach.

git-foreach's People

Contributors

d-e-s-o avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.