本地 Git 远程仓库
Local Git Remotes

原始链接: https://cblgh.org/posts/local-git-remotes/

为了确保备份可靠并加快工作流程,你可以使用本地“裸”(bare)仓库来托管自己的 Git 远程仓库。 设置方法如下:进入家庭服务器上的某个目录,运行 `git clone --bare /path/to/project`,即可创建一个专用于托管的仓库。之后,你可以通过以下命令将其添加为本地机器的远程仓库:`git remote add local ssh://USER@MACHINE:/path/to/bare/repo.git`。配置完成后,你就可以像操作其他远程仓库一样推送和拉取代码了。 这种方法具有多重优势:它为可能不稳定的异地服务器提供了一个高可用性的替代方案,降低了延迟,并能让你实现“两全其美”的设置——既保留了用于即时访问的本地远程仓库,又拥有用于冗余备份的异地副本。通过自建基础设施,你可以在享受更可靠开发流程的同时,保持数据独立于大型科技平台之外。

这篇 Hacker News 帖子讨论了在 Git 中使用“本地远程仓库”(local remotes)的实用性及其概念本质。许多评论者起初对这种做法表示质疑,指出由于 Git 本身就是分布式的,仅使用本地分支或将更改保留在个人计算机上通常就已足够。一些参与者认为,将本地文件夹视为“远程”是不直观的,且可能多此一举。 然而,其他人强调了该技术的一些实用场景: * **协作与归档:** 使用共享网络驱动器或 Dropbox 文件夹进行团队访问或异地备份。 * **安全性:** 在同步到远程服务器之前推送到本地加密容器,以避免信任云服务提供商。 * **环境隔离:** 便于在难以直接同步的容器内进行开发。 * **多机同步:** 维护一个中央本地存储库,以便在不依赖第三方云托管的情况下同步多台机器。 讨论还触及了一个常见的误解,即 GitHub 等同于 Git;用户指出,许多开发人员常将该平台误认为是其背后的版本控制系统本身。
相关文章

原文

As part of working on cani I was also using a variety of git remotes. One of the remotes was hosted on a server I have at home. Here’s how I set that up.

Let’s say the server has a project in a folder called cani. This folder has the code and a .git/ directory:

/home/user/projects/cani

We can use the above folder to clone a bare repository (can be used as a remote without causing weird conflicts):

cd /home/user/bares
git clone --bare /home/user/projects/cani
# creates /home/user/bares/cani.git

To add this bare repository as a remote to push & pull from can look like a few different ways. Here’s the remote while on the same machine:

git remote add local /home/user/bares/cani.git

Here’s how you can set up the remote when pushing from another machine:

git remote add local ssh://USER@MACHINE:/home/user/bares/cani.git

We can associate the branch main as the default branch for remote local:

git remote set-branches local main

Now we’re ready to push to our local remote!

# without any config
git push ssh://USER@MACHINE:/home/user/bares/cani.git

# if remote configured with ssh://USER@ as above
git push local  

Pulling from the configured remote:

# without default branch
git pull local main

# with default branch branch `main`
git pull local

For what it’s worth, the ssh://USER@MACHINE syntax can be replaced with any ssh config you might have set up locally.

Anyway! I really liked working with a local remote, especially when working with offsite remotes with lower uptime. Setting up a local remote made it a much more relaxing to use a remote that is maybe not always available. In my own case, the offsite remote is a small community server that is getting massively hammered by big corporate scrapers. Now I have the best of both: a local remote I can push to with no delay and an offsite copy hosted by a friend in an online community. Notably, no big tech is involved!

联系我们 contact @ memedata.com