码迷,mamicode.com
首页 > 其他好文 > 详细

服务器搭建私人Git

时间:2018-04-23 00:05:00      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:而不是   tree   历史记录   repos   git init   adduser   默认   包含   服务端   

环境是CentOS 7.4 64位

在服务器上搭建 Git

0. 预备

  • 安装git yum install git

1. 开发者-生成个人SSH公钥

p.s. 书中的4.3节是【生成个人的SSH公钥】,网站版本却是【服务器上的 Git - 生成 SSH 公钥】,因为是用户的公钥,所以还是书中名字较合适

在本地win10上测试,没有装Git则下载安装Git for windows

打开git Bash, ssh-keygen 回车几次就生成了(默认在/c/Users/用户名/.ssh/id_rsa)
(*nix用户直接使用ssh-keygen)

id_rsa.pub或者id_dsa.pub发给管理员,需放置到服务器上。

2. 服务端-服务器上的Git,设置服务器

$ sudo adduser git
$ su git
$ cd
$ mkdir .ssh && chmod 700 .ssh
$ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys

将开发者*.pub文件内容,追加进~/.ssh/authorized_keys中。

/opt/git下建立裸仓库,

  • 将现有仓库导出为一个新的裸仓库 git clone --bare https://github.com/onionc/Laravel-Messages.git messages.git

  • 或者git init --bare初始化一个目录

    cd /opt/git
    mkdir messages.git
    cd messages.git
    git init --bare

看一下目录,用现有仓库的话配置文件里面多了几行,有远程分支url

[root@x messages.git]# ll
total 36
drwxr-xr-x 2 root root 4096 Apr 22 15:26 branches
-rw-r--r-- 1 root root  138 Apr 22 15:26 config
-rw-r--r-- 1 root root   73 Apr 22 15:26 description
-rw-r--r-- 1 root root   23 Apr 22 15:27 HEAD
drwxr-xr-x 2 root root 4096 Apr 22 15:26 hooks
drwxr-xr-x 2 root root 4096 Apr 22 15:36 info
drwxr-xr-x 4 root root 4096 Apr 22 15:26 objects
-rw-r--r-- 1 root root   98 Apr 22 15:27 packed-refs
drwxr-xr-x 4 root root 4096 Apr 22 15:26 refs
[root@x my_project.git]# cat config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = true
[remote "origin"]
    url = https://github.com/onionc/Laravel-Messages.git

这样就创建了(舍去工作目录的)一个新的目录。

3. 将自己的代码推上去

由于我本地的已经有一个github远程库,所以用git remote add添加一个远程仓库。可参考 2.5 Git 基础 - 远程仓库的使用

建远程仓库(需确保opt/git目录权限为git)

 git remote add tx-origin git@111.111.111.111:/opt/git/messages.git

推数据

git push tx-origin master

4. 克隆

换个目录克隆一下

$ git clone git@111.111.111.111:/opt/git/messages.git
Cloning into ‘messages‘...
git@111.111.111.111‘s password:
remote: Counting objects: 200, done.
remote: Compressing objects: 100% (155/155), done.
remote: Total 200 (delta 25), reused 196 (delta 24)
Receiving objects: 100% (200/200), 1.17 MiB | 122.00 KiB/s, done.
Resolving deltas: 100% (25/25), done.

成功。试着修改一个文件,提交成功


$ git status
On branch master
Your branch is up to date with ‘origin/master‘.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
        modified:   README.md
no changes added to commit (use "git add" and/or "git commit -a")


$ git add README.md

$ git commit -m ‘2‘
[master 10c25ba] 2
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git push
git@111.111.111.111‘s password:
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 266 bytes | 266.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To 111.111.111.111:/opt/git/messages.git
   2fa76cf..10c25ba  master -> master

p.s. 服务器ip 111.111.111.111等隐私均为杜撰。

0. 疑问:文件存到哪去了?

服务器的messages.git目录下均未找到项目文件

[git@x messages.git]$ ll
total 32
drwxrwxr-x 2 git git 4096 Apr 22 20:57 branches
-rw-rw-r-- 1 git git   66 Apr 22 20:57 config
-rw-rw-r-- 1 git git   73 Apr 22 20:57 description
-rw-rw-r-- 1 git git   23 Apr 22 20:57 HEAD
drwxrwxr-x 2 git git 4096 Apr 22 20:57 hooks
drwxrwxr-x 2 git git 4096 Apr 22 20:57 info
drwxrwxr-x 7 git git 4096 Apr 22 21:28 objects
drwxrwxr-x 4 git git 4096 Apr 22 20:57 refs

那么项目文件推送到后存储到哪里去了呢?

找到一个类似的问题:自己搭建git服务器,本地文件为什么push不到服务器上?的答案是:git服务器上那个目录project001.git 这只是一个仓库. 你需要找个目录克隆出来, 才会显示你push上去的内容的

可是,到底存到哪里了?( 鸽子为什么这么大?

所有服务端的工作都由hooks目录下的update(update.sample)脚本文件来完成 - 精通Git Second Edition, 8.4.1

用我这战五渣英语搜出来一个:
Git Push worked, but files are not on server
回答里说

The files are there, you just don‘t see them because they are embedded into the Git database. This is the difference between initializing a repository with --bare or without

文件在那里,你只是看不到它们,因为它们被嵌入到Git数据库中。这是初始化一个l裸存储库的不同之处。

这样啊,放到数据库中我就不深究了,我这菜逼八成也看不懂了,以后再研究。

回答里留了个参考 What is a bare git repository?
找几句重要的:

Repositories created with git init --bare are called bare repos. They are structured a bit differently from working directories. First off, they contain no working or checked out copy of your source files. And second, bare repos store git revision history of your repo in the root folder of your repository instead of in a .git subfolder.

A bare repository created with git init --bare is for… sharing.

Because git is a distributed version control system, no one will directly edit files in the shared centralized repository.

Because no one ever makes edits directly to files in the shared bare repo, a working tree is not needed. In fact the working tree would just get in way and cause conflicts as users push code to the repository. This is why bare repositories exist and have no working tree.

使用git init --bare创建的存储库称为裸存储库。它们的结构与工作目录稍有不同。首先,它们不包含任何工作或检查源文件的副本。其次,在存储库的根文件夹中,而不是在一个.git子文件夹中,仅使用repos存储您的repo的历史记录。

使用git init --bare创建的一个裸存储库用于…共享。

因为git是一个分布式版本控制系统,所以没有人会直接在共享的集中存储库中编辑文件。

因为没有人直接对共享的裸回文件进行编辑,所以不需要工作树。实际上,当用户将代码推到存储库中时,工作树会导致冲突。这就是为什么裸存储库存在且没有工作树的原因。

服务器搭建私人Git

标签:而不是   tree   历史记录   repos   git init   adduser   默认   包含   服务端   

原文地址:https://www.cnblogs.com/warcraft/p/8910210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!