使用 git 作为实时镜像

发布于 2024-09-19 12:18:39 字数 559 浏览 3 评论 0原文

我有本地计算机 (A)、测试服务器 (B) 和存储库服务器 (C)。
我有以下工作流程:

  1. 在 A 上编写一些内容
  2. 镜像更改为测试机器 B
  3. 如果运行良好,则从 B 提交到 C

现在,我使用 rsync 进行镜像,但由于存储库增长,需要一些时间(约 10 秒)才能获取文件列表我想使用 Git 而不是 rsync,因为它会更快,而且我将拥有本地历史记录以及存储库 C。

问题是我还没有找到任何方法来进行实时操作 -使用 git 进行镜像。我可以

git add . && git commit -m "mirroring" && git push

在本地计算机上执行,但是服务器呢?

每隔几秒执行一次 cronjob 是

git checkout | awk '{print $2;}' | git checkout

正确的方法吗?

PS:我是 git 新手,也许有更适合这项工作的工具。

I have local machine (A), testing server (B) and repository server (C).
I have following workflow:

  1. Code something on A
  2. Mirror changes to test machine B
  3. If it works well commit from B to C

For now, I use rsync for mirroring, but since repository grows it takes some time (~10 sec) to get file list from B. I want to use Git instead of rsync, because it would be much faster and I would have local history along with repository C.

The problem is I haven't found any way to do live-mirroring with git. I can do

git add . && git commit -m "mirroring" && git push

on local machine, but what about server?

Is cronjob for every few second

git checkout | awk '{print $2;}' | git checkout

a right way?

P. S.: I new in git and maybe there is more appropriate tools for this job.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戏剧牡丹亭 2024-09-26 12:18:59

我认为镜像工具不是必需的,git 似乎非常适合。
正如 fseto 所说,我还建议在 A 上提交。将更改复制到 C 应该使用 git push 来完成。然后,你需要明白的是,既然 B 有一个工作树,那么推送它绝对不是正确的方法。您可以(至少应该)只推送到存储库(没有工作树的存储库:您应该在服务器C上拥有什么)。

相反,在 B 上,您可以从 A 获取更改(B 是 A 的 git 克隆)。当然,这可以在 cronjob 上设置。这几乎是一个 git pull ,除了您希望始终反映来自服务器 A 的 HEAD ,即使您在 A 上的分支之间切换、删除提交、分支等因此,在 B 上,我会执行以下命令:

git fetch origin ; git checkout origin/HEAD

该命令会警告您,因为它将创建“分离的 HEAD”状态,但这很好,因为您不想在 B 上提交。

现在最后是服务器部分。 :

  1. 在 C 上:通过 git init --bare 创建服务器存储库
  2. 在 B(或 A?)上:将服务器添加为远程服务器:git remote add server< /code>
  3. 在 B(或 A?)上:如果一切正常,将更改推送到服务器,例如:git push server HEAD:master。我写 HEAD 是为了通用,但你可能想使用分支。

希望我的解释可以理解......:)

I don't think a mirroring tool should be necessary, git seems perfect for that.
As fseto said, I also recommend commiting on A. Copying your changes to C should be done using git push. Then, what you need to understand is that since B has a working tree, pushing to it is definitely not the way to go. You can (at least, should) only push to bare repositories (a repository without a working tree: what you should have on your server C).

Instead, on B, you can fetch the changes from A (B is a git clone of A). This can be set up on a cronjob, of course. This will be almost a git pull, except that you want to always reflect the HEAD from server A, even if you switch between branches on A, remove commits, branches, etc. So, on B, I would go for the following command:

git fetch origin ; git checkout origin/HEAD

This command will warn you because it will create a 'detached HEAD' state, but that's fine since you don't want to commit on B.

Now finally, the server part:

  1. On C: Create your server repo by git init --bare
  2. On B (or A ?): add your server as a remote: git remote add server <repo-C>
  3. On B (or A ?): If all works well, push your changes to the server, for example: git push server HEAD:master. I'm writing HEAD for being general, but you may want to use a branch instead.

Hoping that my explanations are understandable... :)

毁梦 2024-09-26 12:18:56

使用正确的工具完成正确的工作。我会使用 unison 或类似的东西来进行镜像。

我也会从 A 提交,而不是 B。所以你可以跳过在测试环境上镜像 .git 目录,这会让速度更快。然后,您可以留下更有意义的提交消息,说明更改的内容和原因......而不是看到无休止的“镜像”消息列表。

Use the right tool for the right job. I would use unison or something like that to do the mirroring.

I would also commit from A, instead of B. So you can skip mirroring the .git directory on your testing environment, this would make it faster. You can then leave more meaningful commit messages as to what was changed and why... instead of seeing a endless list of "mirroring" message.

不羁少年 2024-09-26 12:18:51

使用 Git 进行实时镜像可以通过 git hooks 来完成。

您可以设置update钩子,它可以在分支更新后执行操作。在服务器 B 上设置此挂钩。每次您向服务器推送更改时,该挂钩都会在 B 的站点启动。在这个钩子中,您可以将更改推送到C(您应该为其创建一个远程)。钩子看起来像这样:

#!/bin/bash
git push remote-of-c $3:$1
[ $? -ne 0 ] && { 
  echo 'Mirroring failed, check server settings and try again.'
  exit 1
}

所以每次更新 B 时,C 也会更新。如果推送期间出现问题,您将在控制台中看到相关消息,并且您的初始推送将不会成功。这就是所谓的“镜像”,不是吗?

Live-mirroring with Git can be done via git hooks.

You can set up update hook, which can execute actions rigt after a branch was updated. Set this hook up at server B. The hook will be launched at B's site each time you push changes to it. Inside this hook, you may just push changes forward to C (for which you should create a remote). The hook would look like this:

#!/bin/bash
git push remote-of-c $3:$1
[ $? -ne 0 ] && { 
  echo 'Mirroring failed, check server settings and try again.'
  exit 1
}

So each time you update B, C will also get updated. If something failed during that push, you'll see the relevant messages in console, and your initial push won't succeed. That's what's called "mirroring", isn't it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文