Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run git gc on multiple repositories [duplicate]

Tags:

git

bash

I have a git repository root at /git

There are different depth of paths, such as:

/git/project/project1/module1.git
/git/project/project1/module2.git
/git/project/project2/dev/module1.git
/git/library/libgit2.git
/git/library/jquery/jquery.git

How to I run git gc recursively in all repos inside /git?

I would prefer to use a shell script to iterate over the repositories: If that directory is not a valid git repository, do not run git gc.

like image 840
linquize Avatar asked Aug 08 '12 01:08

linquize


People also ask

When should you not run git gc `? *?

auto below for how to disable this behavior. Running git gc manually should only be needed when adding objects to a repository without regularly running such porcelain commands, to do a one-off repository optimization, or e.g. to clean up a suboptimal mass-import.

What is gc prune?

gc.pruneExpire. Optional variable that defaults to "2 weeks ago". It sets how long a inaccessible object will be preserved before pruning. gc.worktreePruneExpire. Optional variable that defaults to "3 months ago".

Can a git project have multiple repositories?

If you are working on a big project, then it is inevitable that you need to work with multiple repositories. That's why you need to sync your local code base with multiple Git remote repositories.

How do I update multiple git repositories?

to automatically update all git repositories in that directory. You can mix and match bookmarks and command arguments: gitup --add ~/repos/foo ~/repos/bar gitup ~/repos/baz # update 'baz' only gitup # update 'foo' and 'bar' only gitup ~/repos/baz --update # update all three!


2 Answers

You could try something like:

find /git -name '*.git' -execdir sh -c 'cd {} && git gc' \;

This will find every directory matching *.git under /git, cd into it, and run git gc.

like image 141
Bob the Angry Coder Avatar answered Sep 29 '22 01:09

Bob the Angry Coder


On some systems (ie. OSX) the for loop treats spaces as delimiters, breaking directory names that contain spaces. Set IFS to only use linebreaks \n for this to work properly, and reset to default when done:

oldIFS=$IFS; IFS=$'\n'; for line in $(find /git -name '*.git'); do (echo $line; cd $line; git gc); done; IFS=$oldIFS
like image 24
Ducktales Avatar answered Sep 29 '22 00:09

Ducktales