建立远程 Git 仓库

前言

我有一种需求,在我的树莓派上运行一个git远程仓库,并且还能看到仓库中的文件。

上网搜集了一些资料,就有了这个教程。

安装 git

这很简单,直接用包管理器装就好了,这里就不再赘述了。

创建远程仓库

为了方便管理,我先在树莓派上创建了一个叫 git 的新用户:

1
2
sudo useradd -d /home/git -m -s /usr/bin/bash git
sudo passwd git #设置密码

然后用 git 用户在家目录下创建一个远程的仓库(不是裸仓库)

1
2
3
$ mkdir remote_repo
$ cd remote_repo
$ git init

创建本地仓库

如果有现成的仓库,继续用就行了,如果没有,就新建一个:

1
2
3
$ mkdir local_repo
$ cd local_repo
$ git init

然后在这个仓库里加入你想加入的各种文件或目录。

我的仓库里现在有了以下内容:

1
2
3
~/local_repo $ ls -1
file1
file2

先把它们提交一下:

1
2
~/local_repo $ git add .
~/local_repo $ git commit -m "add 2 files"

关联远程仓库

关联远程仓库很容易:

1
~/local_repo $ git remote add origin git@192.168.0.200:remote_repo

然后push一下看看?

~/local_repo $ git push --set-upstream origin master
git@192.168.0.200's password:
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 203 bytes | 203.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.
remote:
remote: You can set the 'receive.denyCurrentBranch' configuration variable
remote: to 'ignore' or 'warn' in the remote repository to allow pushing into
remote: its current branch; however, this is not recommended unless you
remote: arranged to update its work tree to match what you pushed in some
remote: other way.
remote:
remote: To squelch this message and still keep the default behaviour, set
remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To 192.168.0.200:remote_repo
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '192.168.0.200:remote_repo'

很遗憾提交被拒绝了!下面继续操作。

配置接受提交

根据报错,在远程仓库目录下执行以下命令就能接受提交了:

1
~/remote_repo $ git config receive.denyCurrentBranch ignore

再push一下

~/local_repo $ git push --set-upstream origin master
git@192.168.0.200's password:
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 203 bytes | 203.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To 192.168.0.200:remote_repo
 * [new branch]      master -> master
branch 'master' set up to track 'origin/master'.

提交成功了!

让文件可见

做完之前的工作之后,当你查看远程仓库的目录时,你发现什么也没有:

1
2
~/remote_repo $ ls
~/remote_repo $ 

下面才是重点。

当你查看 .git 目录时,你会看到这些文件:

1
2
3
4
5
6
7
8
~/remote_repo $ ls .git/hooks/
applypatch-msg.sample      pre-push.sample
commit-msg.sample          pre-rebase.sample
fsmonitor-watchman.sample  pre-receive.sample
post-update.sample         prepare-commit-msg.sample
pre-applypatch.sample      push-to-checkout.sample
pre-commit.sample          update.sample
pre-merge-commit.sample

我们需要的是 post-update.sample ,把它改名为 post-update

1
~/remote_repo $ mv .git/hooks/post-update.sample .git/hooks/post-update

然后编辑 post-update 文件,更换为以下内容:

1
2
3
4
#!/bin/sh
unset GIT_DIR #这个一定要unset!
cd ..
git checkout -f

在本地仓库提交些更改后push一下:

1
2
3
4
~/local_repo $ touch file3
~/local_repo $ git add .
~/local_repo $ git commit -m "add 1 file"
~/local_repo $ git push

看下远程仓库的目录:

1
2
3
4
~/remote_repo $ ls -1
file1
file2
file3

文件都出现了!

愉快地使用 git

接下来的各种玩法就看你自己了!

updatedupdated2022-12-162022-12-16