bash Mac 上多个存储库的 Git 状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2765253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:03:23  来源:igfitidea点击:

Git Status Across Multiple Repositories on a Mac

gitbashmacosshell

提问by eapen

I have been searching for a solution to this for a while and have not found quite what I need.

一段时间以来,我一直在寻找解决方案,但还没有找到我所需要的。

I have several Git Repositories in a folder on my Mac (OSX 10.6) and would like a script or tool that will loop through all the repositories and let me know if any of them needs commiting.

我的 Mac (OSX 10.6) 上的一个文件夹中有几个 Git 存储库,并且想要一个脚本或工具来遍历所有存储库,并让我知道其中是否有任何需要commiting。

This is my structure

这是我的结构

Sites
  /project1
  /project2
  /project3

I want the tool to do a git statusin Sites/project1, Sites/project2, Sites/project3 and let me know if any of them have changes or new files that need to be staged or committed.

我希望该工具git status在 Sites/project1、Sites/project2、Sites/project3 中执行操作,并让我知道它们中是否有任何需要暂存或提交的更改或新文件。

The closest script I found that might be hackable is here,but even that script wouldn't run, and I get an error:

我发现可能可以破解的最接近的脚本在这里,但即使该脚本也无法运行,并且出现错误:

"syntax error near unexpected token `do"

“意外标记`do附近的语法错误”

which might have been written for *nix.

这可能是为 *nix 编写的。

采纳答案by RyanWilcox

There's a Python based program, uncommittedthat sounds like it would do exactly what you want. There's no git support for it yet (just hg and Subversion), but you may be able to help the author implement git support in his app, or take his ideas as how to implement your stuff (he documents his finding method on the project page I linked to).

有一个基于 Python 的程序,未提交,听起来就像你想要的那样。目前还没有 git 支持(只有 hg 和 Subversion),但是您可以帮助作者在他的应用程序中实现 git 支持,或者将他的想法作为如何实现您的东西(他在项目页面上记录了他的查找方法)我链接到)。

回答by Woody2143

It seems that the question has been answered fine, but I wanted to throw in my two cents after working on the same thing.

似乎这个问题已经得到很好的回答,但我想在做同样的事情后投入我的两分钱。

I went closer to jcordasc's answer by using a simple bash script. I just did one thing a little different. Looking at the help docs for git you can set the git directory and working directory. This eliminates the need to 'cd' to the directories. Not that it really makes that much difference...

我通过使用一个简单的 bash 脚本更接近 jcordasc 的答案。我只是做了一件有点不同的事情。查看 git 的帮助文档,您可以设置 git 目录和工作目录。这消除了“cd”到目录的需要。并不是说它真的有那么大的不同......

#!/bin/bash

for gitdir in `find ./ -name .git`; 
    do 
        workdir=${gitdir%/*}; 
        echo; 
        echo $workdir; 
        git --git-dir=$gitdir --work-tree=$workdir status; 
    done

Obviously his is more advanced/cleaner for how it shows the status'...

显然,他的状态显示方式更先进/更清晰......

回答by Andrew Roberts

This is an older question, but I went and updated jcordasc's answer so it works with git 1.7.7.5, and I thought I may as well contribute it here:

这是一个较旧的问题,但我去更新了 jcordasc 的答案,因此它适用于 git 1.7.7.5,我想我也可以在这里做出贡献:

https://gist.github.com/3018100

https://gist.github.com/3018100

This version takes any path or paths as argument, and gives jcordasc's output for any git repository it finds anywhere in the file trees represented by the arguments. It also supports detection of unpushed and unmerged commits.

此版本将任何路径或路径作为参数,并为它在由参数表示的文件树中的任何位置找到的任何 git 存储库提供 jcordasc 的输出。它还支持检测未推送和未合并的提交。

回答by Sergey Sokolov

Multiple repo status checking with jsut a single shell alias. No installation is required. All credits to: https://coderwall.com/p/grmruq/git-status-on-all-repos-in-folder

使用单个 shell 别名检查多个 repo 状态。无需安装。所有学分:https: //coderwall.com/p/grmruq/git-status-on-all-repos-in-folder

For bash:

对于 bash:

alias gitstat='find . -maxdepth 1 -mindepth 1 -type d -exec sh -c "(echo {} && cd {} && git status -s && echo)" \;'
alias gitstatfull='find . -maxdepth 1 -mindepth 1 -type d -exec sh -c "(echo {} && cd {} && git status && echo)" \;'

or for Cshell

或用于 Cshell

alias gitstat           'find . -maxdepth 1 -mindepth 1 -type d -exec sh -c "(echo {} && cd {} && git status -s && echo)" \;'
alias gitstatfull       'find . -maxdepth 1 -mindepth 1 -type d -exec sh -c "(echo {} && cd {} && git status && echo)" \;'

回答by J. Cordasco

If you just want the statusof files in local repos, something like this: http://gist.github.com/389478should do the trick. You'll need to modify the pattern in the forloop to pickup whatever directories you want.

如果你只想要本地存储库中文件的状态,像这样:http: //gist.github.com/389478应该可以解决问题。您需要修改for循环中的模式以选取您想要的任何目录。

回答by Ferry Boender

Very late to the party, but I wrote a tool to do this for me. It's a simple shell script, and it can be found on Github.

聚会很晚,但我写了一个工具来为我做这件事。这是一个简单的 shell 脚本,可以在 Github上找到

It looks like this:

它看起来像这样:

Projects/fboender/multi-git-status: Uncommitted changes; Projects/fboender/jerrybuild.tst: Needs push (doc, master); Projects/fboender/multi-git-status.bak: Needs push (master); Projects/fboender/tempita: ok; Projects/fboender/dirigent: Uncommitted changes Untracked files; Projects/flusso/boxes: ok; Projects/flusso/docker: ok; Projects/flusso/deployments: ok; Projects/flusso/backup: ok;

项目/fboender/multi-git-status:未提交的更改; Projects/fboender/jerrybuild.tst:需要推送(doc,master); Projects/fboender/multi-git-status.bak:需要推送(master); 项目/fboender/tempita:好的; Projects/fboender/dirigent:未提交的更改未跟踪的文件; 项目/flusso/盒子:好的; 项目/flusso/docker:好的; 项目/flusso/部署:好的; 项目/flusso/备份:好的;

回答by Jeremy Ferguson

It took me a bit of time to get the code from @Andrew Roberts to work with an array of folders .. If you're having trouble with that too, check my fork at https://gist.github.com/3666392

我花了一些时间来得到@Andrew罗伯茨工作代码文件夹的数组。如果您遇到麻烦太多,检查我的叉子 https://gist.github.com/3666392

Thanks for the great script @Andrew Roberts and @jcordasc .. just what I needed.

感谢@Andrew Roberts 和@jcordasc 的精彩剧本……正是我所需要的。

回答by oxtay

The same question also occurred to me today and other than this page, that I found very useful and with very good tips, I also came across the following blog post. It introduces a python script that does exactly that:

今天我也遇到了同样的问题,除了这个页面,我发现它非常有用并且有很好的提示,我还看到了以下博客文章。它引入了一个 python 脚本,它可以做到这一点:

Usage: show_status [options]

Show Status is awesome. If you tell it a directory to look in, it'll scan
through all the sub dirs looking for a .git directory. When it finds one it'll
look to see if there are any changes and let you know. It can also push and
pull to/from a remote location (like github.com) (but only if there are no
changes.) Contact [email protected] for any support.

Options:
  -h, --help            show this help message and exit
  -d DIRNAME, --dir=DIRNAME
                        The directory to parse sub dirs from
  -v, --verbose         Show the full detail of git status
  -r REMOTE, --remote=REMOTE
                        Push to the master (remotename:branchname)
  -p PULL, --pull=PULL  Pull from the master (remotename:branchname)

The whole thing is located on this git repo.

整个事情都在这个 git repo 上

回答by Gabriel Petrovay

This is an easy way of doing such things in in a one-liner. I am missing one last step that I will add once I find it out (unless someone else know it).

这是在单行中做这些事情的一种简单方法。我遗漏了最后一步,一旦我发现它就会添加(除非其他人知道)。

For a git statusyou can do:

对于git status你可以这样做:

find ./Sites -name .git | awk '{ print "git --git-dir="  " --work-tree " substr(,1,length() - 4) " status" }'

then copy the output and execute it.

然后复制输出并执行它。

For a complete diff, you can run:

对于完整的差异,您可以运行:

find ./Sites -name .git | awk '{ print "git --git-dir="  " --work-tree " substr(,1,length() - 4) " --no-pager diff" }'

The copy-paste things that you must do annoys me. It should be using also xargsin there, but somehow, git complains:

你必须做的复制粘贴的事情让我很恼火。它也应该xargs在那里使用,但不知何故,git 抱怨:

find ./Sites -name .git | awk '{ print "--git-dir="  " --work-tree " substr(,1,length() - 4) " --no-pager diff" }' | xargs git

If anyone knows how to fix the git complaint in this last command, please edit this answer.

如果有人知道如何解决最后一条命令中的 git 投诉,请编辑此答案。

回答by Nathan

There is also a ruby gem for this that works as a git sub command: https://github.com/reednj/git-status-all

还有一个 ruby​​ gem 可以用作 git 子命令:https: //github.com/reednj/git-status-all

# install the gem    
gem install git-status-all

# use the status-all subcommand to scan the directory
git status-all 

git status all

git 状态全部