管理许多 git 存储库
在 git 中设置项目很容易,因此即使对于小脚本,我也可以拥有单独的存储库。 现在的问题是如何管理它们。
我在多个地方使用这些存储库工作。 当我对某些存储库进行更改后,我希望能够更新其他位置的存储库。
所以我有一个目录,里面有很多存储库。
- 我怎样才能获取所有这些?
- 我如何检查其中是否有未提交的更改?
- 我如何检查其中是否有需要合并的更改?
如果能够用一个命令来完成这些工作那就太好了。
输出需要足够安静才能真正注意到要做的事情。
Setting up a project is easy in git and so I can have separate repository even for small script. Now the problem is how to manage them.
I work in multiple places with these repositories. When I have done changes to some repository, I want to be able to update the repositories in other places.
So I have a directory with many repositories in it.
- How can I fetch all of them?
- How can I check whether any of them have uncommitted changes?
- How can I check whether any of them have changes to merge?
And it would be nice to be able to do these with one command.
The output needs to be silent enough to actually notice the things to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(25)
我强烈推荐多存储库工具 mr。 我曾经有一段时间按照别人推荐的方式使用自定义 shell 脚本,但是使用 mr 对我来说有以下好处:
关于您有关静默输出的问题:可以使用命令行开关 -q 修改详细级别。 我更喜欢默认输出,它似乎很好地将输出统一为简短而清晰的摘要。
我对 mr 命令使用以下别名,以确保 mr 始终选择存储在 $HOME 中的默认项目列表,并使用 5 个并行线程:
I highly recommend the multiple repositories tool mr. I used to use a custom shell script as recommended by others for some time, but using mr has the following benefits for me:
Regarding to your question about silent output: The level of verbosity can be modified using the command line switch -q. I prefer the default output which appears to nicely unify the output in a short and clear summary.
I use the following alias for the mr command to ensure that mr always picks up my default project list stored in $HOME, and uses 5 parallel threads:
我必须说我从当前接受的答案开始(只是一堆迭代存储库的帮助器脚本),但总而言之,这对我来说是缺乏经验的。
因此,在尝试 mr、repo 和 git-submodules 后,我发现每个模块都以不同的方式存在缺陷,因此,我最终做了自己的变体: http://fabioz.github.io/mu-repo 这是一个成熟的工具——它的工作流程允许您:
请注意,它支持任何运行 Python 的操作系统;)
I must say I started with the currently accepted answer (just a bunch of helpers scripts that iterate over the repositories), but all in all, it was a lacking experience for me.
So, after trying mr, repo and git-submodules, I found each lacking in a different way, so, I ended up doing my own variant: http://fabioz.github.io/mu-repo which is a mature tool at this point -- it has workflows which allow you to:
Note that it supports any OS where Python runs ;)
gr (git-run) 扩展 mr 的功能(仅适用于
git
)。 我发现使用其标签系统组织多个 git 存储库更容易。 不过,gr
的代码维护得不好。 如果您使用的是 bash,请确保使用-t 标记
而不是#tag
形式。gr (git-run) extends mr's functionality (only for
git
). I find it easier to organize multiple git repos using its tag system. The code forgr
is not well maintained though. If you are using bash, make sure you use it with the-t tag
instead of#tag
form.我编写了一个名为 gita 的命令行工具来管理多个存储库。 它并排显示已注册存储库的状态,例如
它还可以从任何工作目录委托 git 命令/别名。
现在使用此工具回答您的问题:
gita ll
命令在分支名称旁边显示 3 个可能的符号,表示+
:已暂存的更改*
:未暂存的更改_< /code>:未跟踪的文件/文件夹
分支名称有 5 种颜色
要安装它,只需运行
I wrote a commandline tool called gita to manage multiple repos. It shows the status of registered repos side by side, for example
It can also delegate git commands/aliases from any working directory.
Now to answer your questions using this tool:
The
gita ll
command shows 3 possible symbols next to the branch name, which indicates+
: staged changes*
: unstaged changes_
: untracked files/foldersThe branch names are colored in 5 ways
To install it, simply run
我编写了一个名为“RepoZ”的工具,只要您克隆 git 存储库或更改其中的任何内容,它就会自动发现它们例如,他们喜欢切换分支。
一旦找到,存储库就会被“跟踪”。 这将简单地将它们显示在本地存储库列表中,包括受 posh-git 启发的密集状态信息。 这包含当前分支以及其他内容,例如文件编辑以及传入或传出提交的计数。
这有助于跟踪本地存储库以及要提交或推送的未完成工作。 此外,您还可以使用存储库列表作为导航,从一个存储库切换到另一个存储库。
“我怎样才能获取所有这些?”
Windows 版本提供了一个上下文菜单来获取或拉取存储库。 通过多选,您可以同时对多个存储库运行操作。
但是,您可能会发现另一个功能非常有用:
通过自动获取,您可以告诉 RepoZ 在后台定期获取所有 git 存储库的遥控器。 当然,这些获取不会与您的本地提交发生冲突。 没有像 git pull 那样的本地合并尝试。
I wrote a tool called "RepoZ" which automatically discovers git repositories as soon as you clone them or change anything in them like switching a branch, for example.
Once found, the repositories are "tracked". That will simply show them in a list of local repositories including a dense status information inspired by posh-git. This contains the current branch and further stuff like file edits and the count of incoming or outgoing commits.
This helps to keep track of your local repositories and unfinished work to commit or push. In addition, you can use the repository list as navigation to switch from one repository to another.
"How can I fetch all of them?"
The version for Windows offers a context menu to fetch or pull a repository. With multi-select you can run actions on multiple repositories at once.
However, you might find another feature very useful:
With Auto fetch, you can tell RepoZ to fetch the remotes of all your git repositories periodically in the background. These fetches won't collide with your local commits, of course. There are no local merge attempts like with
git pull
.您可以尝试将 repo 与自定义
manifest.xml
文件结合使用指定您的存储库所在的位置。 关于如何执行此操作,有更多文档。它是Android AOSP 项目的标准。
或者,您可以使用
git-submodule(1
)。You could try using repo with a custom
manifest.xml
file to specify where your repositories are. There is some more documentation on how to do this.It's the standard for Android AOSP project.
Alternatively you could use
git-submodule(1
).我创建了一个别名和一个函数来在目录中可用的所有存储库上运行任何 git 命令(递归地)。 您可以在这里找到它: https://github.com/jaguililla/dotfiles/git
这是代码:
希望有帮助:)
I've made an alias and a function to run any git command on all repositories available in a directory (recursively). You can find it here: https://github.com/jaguililla/dotfiles/git
This is the code:
Hope it helps :)
我使用此脚本轻松地在我的所有存储库中执行 git 命令。
默认情况下,脚本将在 ~/cm/src 中查找 git 存储库,但您可以通过根据自己的喜好设置 GITREPO 环境变量来覆盖它。
此脚本基于此脚本。
I use this script to easily execute git commands in all of my repositories.
By default the script will look for git repositories in ~/cm/src but you can override this by setting the GITREPO environment variable to your liking.
This script is based on this script.
gitslave 是一个工具,它可以通过在超级项目和子项目之间创建超级项目/子项目关系来在多个存储库上运行相同的命令潜艇。 这(默认情况下)提供输出摘要,以便您可以专注于提供唯一输出的存储库(对于 git status 有用,对于 git ls-files 不太有用)。
这通常用于需要将多个存储库组装在一起并同时将它们保存在同一分支或标签上的项目。 对于我的(裸)存储库目录,我只有一个小 makefile,它可以让我运行任意 git 命令,如您所见,我主要用于 fsck 和 gc:
gitslave is a tool which can run the same command over many repositories by creating a superproject/subproject relationship between the super and the subs. This (by default) provides output summarization so you can concentrate on the repositories which provide unique output (useful for git status, not so useful for git ls-files).
This is typically used for projects where you need to assemble several repositories together and keep them on the same branch or tag at the same time or whatever. For my directory of (bare) repositories I just have a little makefile which lets me run arbitrary git commands, which as you see I primarily use for fsck and gc:
您可以使用 git-status-all gem 来实现此目的:https:// /github.com/reednj/git-status-all
还有一个获取选项,可以在显示状态之前从源获取所有存储库:
You can use the
git-status-all
gem for this: https://github.com/reednj/git-status-allThere is also a fetch option that will fetch from origin for all the repositories before displaying the status:
为此,您可以使用
every
,它可以按照此模式在多个目录中运行 cli 命令every
安装:
npm i -g every-cli
使用示例:
每个 git 分支
For this purpose, you can use
every
which can run cli commands in multiple directories following this patternevery <command>
Install:
npm i -g every-cli
Usage example:
every git branch
如果有人仍在查看此线程,请查看我的名为 gitme 的 shell 脚本,
您将需要手动输入本地 git 存储库,但我每天都使用此工具在多台计算机上协作多个 git 项目。
你可以运行 git clone https://github.com/robbenz/gitme.git
脚本也发布在下面
我在我的 ~/.bash_profile 中设置了一个别名所以 alias gitme='sh /path/to/gitme.sh'
If anyone is still looking at this thread please checkout my shell script called gitme
you will need to manually input your local git repos but i use this tool everyday to collaborate multiple git projects on multiple computers.
you can run
git clone https://github.com/robbenz/gitme.git
also the script is posted below
I setup an alias in my ~/.bash_profile so
alias gitme='sh /path/to/gitme.sh'
我为此编写了一个 CLI 工具,请参阅 https://manicli.com/。 它还具有一些不错的附加功能:
I wrote a CLI tool for this, see https://manicli.com/. It also has some nice additional features:
我找到了一个很好的解决方案,可以从具有 .git 文件夹的所有子目录中提取当前分支,即使每个存储库签出了不同的分支。
该命令是一个单行命令,足够灵活,可以针对不同的 git 命令进行修改,并且可以使用别名。
只需复制并粘贴以下内容:
分解:
在当前目录 (find .) 中查找所有名为“.git”的目录 (-type d) (-name .git),最多查找两个目录深度 ( 2 而不是 1,因为我们正在 git repo 文件夹中查找 git 文件夹)。
运行以下 shell 命令 (exec sh -c)
将目录更改为第一个参数 (cd "$0"),然后将目录向上更改一级以离开 .git 文件夹 (cd ..),然后在指定分支时执行 git pull origin通过运行 git rev-parse... 来获取当前分支的 HEAD 提交。
“{}”是我们从初始 find 命令获得的结果相对路径。 这 ; 用于结束命令。
在 MacOS 10.14.6 上的 zsh 上进行了测试。 按原样,仅当远程和本地分支命名相同时才有效,据我所知。
您可以修改它来获取状态。 我希望您可以添加参数以进一步使其更加灵活,但我还没有尝试过。
I've found a great solution for pulling from the current branch from all sub-directories that have a .git folder, even if each repo has a different branch checked out.
The command is a one-liner, flexible enough to modify for different git commands, and can be aliased.
Just copy and paste the following:
Breaking it down:
Find all directories (-type d) in the current directory (find .) that have the name ".git" (-name .git), looking a maximum of two directories deep (2 rather than 1 because we're looking for the git folder within the git repo folder).
Run the following shell command (exec sh -c)
Change directory to the first argument (cd "$0"), then change directory one level up to leave the .git folder (cd ..) then do git pull origin while specifying the branch by running git rev-parse... for the current branch's HEAD commit.
The "{}" is the result relative path we get from the initial find command. The ; is used to end the command.
Tested on MacOS 10.14.6 on zsh. As-is, works only when remote and local branches are named the same, AFAIK.
You can modify this to get status. I expect you might be able to add arguments to further make this more flexible but I haven't tried yet.
您应该查看 CPAN 上的 rgit,它在目录树中的所有存储库上递归执行 git 命令。
来自文档:
You should check out rgit on the CPAN which recursively executes git commands on all repositories in a directory tree.
From the docs:
我刚刚创建了一个可以实现您想要的功能的工具!
gmaster fetch
gmaster status
gmaster status
它被称为 gitmaster:https://github.com/francoiscabrol/gitmaster
I have just created a tool that do what you want!
gmaster fetch
gmaster status
gmaster status
It is called gitmaster: https://github.com/francoiscabrol/gitmaster
我将这个简单的 bash 函数写入我的 .bash_profile 中,以便能够仅在所有 git 存储库中运行 git、maven、gradle 或任何其他 bash 命令:
这可以这样使用
I wrote this simple bash function into my .bash_profile to be able to run git, maven, gradle, or any other bash command in all git repositories only:
This can be used this way
免责声明:我正在 www.repoflow.com 上开发此工具
我正在研究这个,但这也是解决OP问题并帮助管理多个存储库,您可以使用UI或底层GraphQL API来创建您自己的脚本,请将其视为另外一个选择。
Disclaimer: I am working on this tool at www.repoflow.com
I am working on this, but this is also solving the OP questions and help manage multiple repositories in general, you can use the UI or the underlying GraphQL API to create your own scripts, please consider it as another option.
有一个方便的工具可以获取所有多个存储库。
git-pull-all 是一个命令行工具,可以在多个 git 存储库上异步执行。
安装
用法
假设您有以下文件和目录:
当您在
~/Projects
目录上运行 git-pull-all 命令时,它应该找到子 git 存储库(在上述情况下) cool-examples 和 super-express),然后对它们分别执行git pull
。GitHub 链接: https://github.com/tatsuyaoiw/git-pull-all
There's a handy tool to fetch all multiple repositories.
git-pull-all
is a command line tool to execute on multiple git repositories in asynchronously.Installation
Usage
Assume you have these files and directories:
When you run
git-pull-all
command on~/Projects
directory, it should find child git repositories (in the above case cool-examples and super-express) then executegit pull
on each of them.GitHub link: https://github.com/tatsuyaoiw/git-pull-all
我使用 Visual Studio 代码。 我们的代码库中有大约 20 个库,它们都是单独的 Git 存储库,Visual Studio Code 中的源代码控制选项卡非常适合“一目了然”地了解本地存储库落后或领先的程度。 还有一些设置可以定期获取以保持该信息最新,以及刷新按钮。
它不像脚本那么方便,但是当涉及到我的源代码控制时,我喜欢成为那个进行更改的人。 需要编写非常好的脚本才能小心,不要覆盖更改等。使用 VS Code 方法,您可以快速获得大量信息,以便自己采取行动。
下面是它的屏幕截图:
I use Visual Studio code. We have around 20 libraries in our codebase that are all separate Git repos, and the source control tab in Visual Studio Code is pretty good for seeing an "at a glance" view of how far behind or ahead local repos are. There are also settings to periodically fetch to keep that info up to date, as well as a refresh button.
Its not as convenient as a script, but when it comes to source control for me I like to be the one making changes. It takes a very well-written script to be careful to not overwrite changes, etc. With the VS Code approach, you get a good amount of info quickly that you can act on yourself.
Here is a screenshot of what it looks like:
扩展 https://stackoverflow.com/a/816674/1869000 (现在可以在 GitHub 上获取存储库)
我建议您检查此工具https://github.com/googleapis/github-repo-automation Google 使用它来管理多个存储库。
关于您关于奉献变更的问题,变更应该是 PR,以便您可以对其做出反应。
https://github.com/tj/git-extras 可以帮助您编写脚本你的场景。
我怎样才能获取所有这些?
Google 解决方案上的
repo 同步
< /p>您可以使用 lib 功能轻松编写逻辑
如何检查其中是否有需要合并的更改?
PR 应该是分支需要合并的最终指示,但如果你有其他逻辑 - 上面
检查 git extras 命令的想法请参阅 https://github.com/tj/git-extras/blob/master/Commands.md#git-show-unmerged-branches< /a>
Expanding on https://stackoverflow.com/a/816674/1869000 (Now repo is available on GitHub)
I suggest you check this tool https://github.com/googleapis/github-repo-automation used by Google to manage multiple repos.
About your question about dedicating changes, changes should be PRs so you can react to them.
https://github.com/tj/git-extras could help you write a script for your scenarios.
How can I fetch all of them?
repo sync
on the google solutionHow can I check whether any of them have uncommitted changes?
You can easily write your logic using the lib feature
How can I check whether any of them have changes to merge?
PR should be the ultimate indication that branches need to merge, but if you have another logic - the above
check git extras commands for ideas see https://github.com/tj/git-extras/blob/master/Commands.md#git-show-unmerged-branches
请尝试 gitas。
gitas status
标志:Please try gitas.
gitas status
flags:我使用代码编辑器(VS code)的多光标选择/多行编辑功能来创建批处理/shell 脚本并一次性执行它。 通过这个简单愚蠢的解决方案,我知道我在做什么以及执行这些命令可以期望什么。 无需猜测,无需学习,无需定制。 另一个原因是,每次我想在驻留在公共父目录中的不同存储库上执行命令时。
I use multi-cursor-select/multi-line editing feature of my code editor (VS code) to create batch/shell script and execute it in one go. With this simple stupid solution, I know what I'm doing and what I can expect from execution of those commands. No guessing, no learning, no customisation needed. Another reason is, every time I want to execute commands on different set of repos which reside in a common parent directory.
请按照下面提到的步骤从所有存储库中提取代码:
Please follow below-mentioned steps to pull code from all the repositories:
https://github.com/wwjamieson3/envisionTools 有一个名为 gitstat 的 bash 脚本,可以完成此操作以及整个操作更多。 它与 GNU 和 Mac 兼容。 如果需要,它可以从远程执行获取。 它显示未跟踪、修改、添加、删除和暂存的文件。 它与远程的不同之处在于显示远程上未合并的提交和未推送的本地提交。 它还可以以摘要或详细模式甚至 HTML 显示颜色编码结果。 它使用locate而不是find,因为它速度无限快,甚至会提示更新您的locate数据库(如果它太旧)。
https://github.com/wwjamieson3/envisionTools has a bash script called gitstat that does this and a whole lot more. It's GNU and mac compatible. It can perform fetches from remotes if asked. It shows untracked, modified, added, deleted and staged files. It differences with remotes by showing unmerged commits on remotes and unpushed local commits. It also can display color coded results in summary or detailed mode and even HTML. It uses locate instead of find because it's infinitely faster and will even prompt to update your locate database if it's getting too old.