termux搭建随身 git 服务器

2 篇文章 0 订阅
1 篇文章 0 订阅

参考链接

通过git在 CentOS Linux 服务器上搭建远程仓库

需求

我有一台淘汰的空闲安卓手机,想要将它作为一个随身的 git 服务器,上传和下载一些笔记和文件的时候只需要连接局域网,或者通过手机局域热点。

差异

和在完整版Linux系统搭建git服务不同。

  1. termux不能通过useradd命令创建用户,所以只能在termux默认用户下搭建git服务
  2. 因为termux只有一个用户的前提下,还需要通过这个用户来进行ssh登录,所以无法改用git-shell

操作实录

将原本在linux上搭建好的git bare仓库,复制到termux上

然后什么都不用做。

借着原有的ssh,就可以进行远程仓库操作。

远程仓库地址修改

remote.origin.url=ssh://u0_a174@192.168.31.140:8022/data/data/com.termux/files/home/git-repo/knx-gz-libra-docs.git

说明:

  1. ssh::通过ssh协议连接
  2. u0_a174:termux用户名
  3. 192.168.31.140:8022:是host以及ssh服务端口号
  4. /data/data/com.termux/files/home/git-repo/knx-gz-libra-docs.git则是bare仓库的具体路径了

创建一个新仓库的步骤

1. 在termux上创建空仓库

# 创建仓库目录(带.git方便人眼识别)
mkdir spring-kafka-evo.git

# 进入仓库目录
cd spring-kafka-evo.git

# 初始化裸仓库
git init --bare

输出日志:

hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint: 	git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint: 	git branch -m <name>
Initialized empty Git repository in /data/data/com.termux/files/home/git-repo/pangea/evolution/spring-kafka-evo.git/

2. 在termux工作区创建工作目录并设置远程仓库地址

假设工作区是在:/data/data/com.termux/files/home/my-git-repo

那么进入该工作区中合适的目录下,拉取上面新建的仓库

# 拉取仓库,同一个设备下,只需要一个目录路径
git clone /data/data/com.termux/files/home/git-repo/pangea/evolution/spring-kafka-evo.git

输出日志:

Cloning into 'spring-kafka-evo'...
warning: You appear to have cloned an empty repository.
done.
# 进入仓库目录
cd spring-kafka-evo

# 验证git仓库
git status

此时termux抛异常:

fatal: unsafe repository ('/storage/emulated/0/git-repo/pangea-evolution/spring-kafka-evo' is owned by someone else)
To add an exception for this directory, call:

	git config --global --add safe.directory /storage/emulated/0/git-repo/pangea-evolution/spring-kafka-evo

由于目录归属用户问题,git服务会判定当前仓库不安全,需要执行提示的命令注册为安全目录

# 注册为安全目录
git config --global --add safe.directory /storage/emulated/0/git-repo/pangea-evolution/spring-kafka-evo

# 再次验证 git 仓库
git status

此时 termux 正常输出 git status 命令的日志

On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

往工作仓库中填充内容,例如 README.md 文件,还有其他源码目录和文件等。

# 往空仓库中填充内容
touch README.md

# 提交和推送
git add -A && git commit -m "init the repository" && git push

输出日志:

[master (root-commit) 223bb96] init the repository
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 214 bytes | 214.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To /data/data/com.termux/files/home/git-repo/pangea/evolution/spring-kafka-evo.git
 * [new branch]      master -> master

3. 在外部设备工作区场景工作目录并设置远程仓库

例如在一个 windows 系统主机上,创建仓库的工作目录,就创建一个同名的文件夹:spring-kafka-evo

然后进入该文件夹,并执行下面的命令初始化为一个git仓库

# 初始化为 git 仓库
git init

# 尝试把目录中已有的内容添加的暂存区
git add -A

设置远程仓库

# 检查是否已经设置过远程(origin)仓库(如果严格按照上面步骤下来,没有设置过)
git remote -v

# 添加远程仓库
git remote add origin ssh://u0_a174@termux-honor:8022/data/data/com.termux/files/home/git-repo/pangea/evolution/spring-kafka-evo.git

# 或者,如果远程仓库已经设置过的,需要修改则使用下面这个命令
git remote set-url origin ssh://u0_a174@termux-honor:8022/data/data/com.termux/files/home/git-repo/pangea/evolution/spring-kafka-evo.git

说明:
termux-honor:命令中这一段是一个写在 hosts 文件中的域名映射,相当于一个 ip 地址,即 temux 设备的 ssh 服务 ip 地址。

# 尝试拉取远程仓库的内容
git pull

抛出异常

remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 194 bytes | 64.00 KiB/s, done.
From ssh://termux-honor:8022/data/data/com.termux/files/home/git-repo/pangea/evolution/spring-kafka-evo
 * [new branch]      master     -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> master

远程仓库和本地仓库的相同 master 分支没有关联起来,使用下面的提示命令修复

git branch --set-upstream-to=origin/master master
# 输出日志:Branch 'master' set up to track remote branch 'master' from 'origin'.

# 再次尝试拉取
git pull
# 输出日志:Already up to date.
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值