类似的博文,在前面的帖子里面也提到过,当时讲述的是一个入门级别的。其URL是ssh://username@repo-host-address/repo-path这种格式。

今天再说说如何创建类似GitHub那种以git@repo-host:/path/proj.git这种放个的远程仓库。这个是不是看起来很酷???

其实比较简单:

第一步:在安装git软件。源码安装或者yum等都行。我的版本信息如下

 [root@CloudGame tools]# git --version
git version 2.6.-rc1

第二步:创建远程仓库目录。看下面的操作就可以很清楚了。就是创建一个文件夹而已。

 [root@CloudGame home]# mkdir -p /data/git

第三步:创建git用户,并设置相关的组及安全。

 [root@CloudGame home]# useradd -r -d /data/git git  #添加git用户为系统用户,并指定其home目录为/data/git
 [root@CloudGame home]# chown -R git:git /data/git   #将git用户的家目录设置为git组,git用户
 [root@CloudGame home]# cd /data
[root@CloudGame data]# ll
total
drwxr-xr-x git git Jan : git
[root@CloudGame data]# cd git/
[root@CloudGame git]# ll                 #查看目录内容为空,说明目前里面什么也没有,的确,还没有做任何操作呢。
total

第四步:创建一个空的git仓库。并将仓库里面的所有的文件设置为git组,git用户

 [root@CloudGame git]# git init --bare mueas.git    #注意,这里最好带上--bare指定一个空仓库,否则客户端clone后,提交代码时,会遇到错误,要做系列配置
Initialized empty Git repository in /data/git/mueas.git/
[root@CloudGame git]# ll
total
drwxr-xr-x root root Jan : mueas.git
[root@CloudGame git]# ll -al
total
drwxr-xr-x git git Jan : .
drwxr-xr-x root root Jan : ..
drwxr-xr-x root root Jan : mueas.git
[root@CloudGame git]# cd mueas.git/
[root@CloudGame mueas.git]# ll
total
drwxr-xr-x root root Jan : branches
-rw-r--r-- root root Jan : config
-rw-r--r-- root root Jan : description
-rw-r--r-- root root Jan : HEAD
drwxr-xr-x root root Jan : hooks
drwxr-xr-x root root Jan : info
drwxr-xr-x root root Jan : objects
drwxr-xr-x root root Jan : refs
 [root@CloudGame git]# chown -R git.git /data/git/*
[root@CloudGame git]# ll
total 4
drwxr-xr-x 7 git git 4096 Jan 20 09:28 mueas.git
[root@CloudGame git]# cd mueas.git/
[root@CloudGame mueas.git]# ll
total 32
drwxr-xr-x 2 git git 4096 Jan 20 09:28 branches
-rw-r--r-- 1 git git 66 Jan 20 09:28 config
-rw-r--r-- 1 git git 73 Jan 20 09:28 description
-rw-r--r-- 1 git git 23 Jan 20 09:28 HEAD
drwxr-xr-x 2 git git 4096 Jan 20 09:28 hooks
drwxr-xr-x 2 git git 4096 Jan 20 09:28 info
drwxr-xr-x 4 git git 4096 Jan 20 09:28 objects
drwxr-xr-x 4 git git 4096 Jan 20 09:28 refs

第五步:设置git用户的安全策略,不允许其具有登录系统的权限。修改/etc/passwd文件,找到git用户行,如下红色行为修改后的内容。修改前,是/bin/bash。

 lighttpd:x::::/home/lighttpd:/sbin/nologin
dockerroot:x:::Docker User:/var/lib/docker:/sbin/nologin
stack:x::::/opt/stack:/bin/bash
chrony:x::::/var/lib/chrony:/sbin/nologin
git:x::::/data/git:/usr/bin/git-shell
"/etc/passwd" 56L, 2976C

第六步:为了能ssh链接,需要在这个远程仓库mueas.git的上一级目录下创建.ssh目录,并将客户端机器上的公钥存放在这个目录里面的authorized_keys文件里面。注意,若是多个客户端要访问,则需要将多个客户机的公钥追加到这个文件的后面。这里,我就在我自己的机器上测试,所以,我只需要将我自己机器的~/.ssh/id_rsa.pub文件内容copy到authorized_keys文件里面即可。当然,也可以是dsa格式的文件。【要是客户端用户目录下没有~/.ssh目录,可以通过ssh-keygen -t rsa或ssh-keygen -t dsa进行创建,同时会生成key文件,公私都有】

 [root@CloudGame git]# mkdir .ssh        #创建.ssh目录,注意目录的层次关系
[root@CloudGame git]# ll
total
drwxr-xr-x git git Jan : mueas.git
[root@CloudGame git]# ll -al
total
drwxr-xr-x git git Jan : .
drwxr-xr-x root root Jan : ..
drwxr-xr-x git git Jan : mueas.git
drwxr-xr-x root root Jan : .ssh
 [root@CloudGame .ssh]# cd ..          #在.ssh目录下创建authorized_keys文件
[root@CloudGame git]# cd .ssh
[root@CloudGame .ssh]# ll -al
total
drwxr-xr-x root root Jan : .
drwxr-xr-x git git Jan : ..
[root@CloudGame .ssh]# vim authorized_keys
 [root@CloudGame tools]# cat ~/.ssh/id_rsa.pub    #查看id_rsa.pub内容,并将其显示出来的内容(蓝色部分)copy到/data/git/.ssh/authorized_keys文件里面
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAutquVDcyjoxwXzbrgLcu/wlK9SkXykkd5mktSPqA4exUc6flDv5dYzT3sWMYaH4LP/fiT2mhAoPRU8HaejOfnU3+ALunjXBtxr8XDZQDNrHnZ31477IUSBJ6XRlEj+sDVBDujAxGhNpP41B4v/bSpbrkOJGuVhUtcl81V/nKrCwvhpX+mGRviuiIRsv7E8HEb3AZ7hLXibuDP7kSe3M5nO3JOnsE7e3h8Ob7WAmkxPU/bGqALAodrp0vUyyLsdUt1lynauUZmOgaowL9C+eTbEtFQvCrVrRbXz6GE0VfS7WUA7rxtMujIxuh2fdCWIH4J/wuA+ul3qPsKEDa1MiBSQ== root@CloudGame

到此,一个空的远程仓库就算创建好了。可以测试了。我在另外一个terminal下执行git clone这个mueas.git仓库,但是这个时候,比较常见的问题如下:

 [root@CloudGame tmp]# git clone git@109.105.5.108:/data/git/mueas.git
Cloning into 'mueas'...
Agent admitted failure to sign using the key.
git@109.105.5.108's password:
Permission denied, please try again.

上面这个问题,要求输入密码,不对的话,会再提示输入,一直到正确为止,真是扯淡,输入密码多费劲啊,我都提供了公钥了啊,呵呵,这里遗忘了一步,就是将当前用户的私钥添加到添加到ssh-agent的高速缓存中。看如下操作:

 [root@CloudGame tmp]# ssh-add
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)

我再试试,看是不是管用:

 [root@CloudGame tmp]# git clone git@109.105.5.108:/data/git/mueas.git
Cloning into 'mueas'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.

^_^,是不是搞定,可以正常的clone远端的仓库了。

下面,我是不是要试试,在本地仓库修改一下文件,能否push到远端仓库呢?

 [root@CloudGame mueas]# ll
total
-rw-r--r-- root root Jan : file.java
-rw-r--r-- root root Jan : test.txt
[root@CloudGame mueas]# git add file.java
[root@CloudGame mueas]# git commit -m "New file added"
[master 7f3f3b0] New file added
file changed, insertion(+)
create mode file.java
[root@CloudGame mueas]# git push
Counting objects: , done.
Delta compression using up to threads.
Compressing objects: % (/), done.
Writing objects: % (/), bytes | bytes/s, done.
Total (delta ), reused (delta )
To git@109.105.5.108:/data/git/mueas.git
..7f3f3b0 master -> master

是不是没有问题,爽吧,下面再看看换一个用户(shihuc)测试的结果。这里需要注意的是,要将shihuc用户目录下的pubkey放入/data/git/.ssh/authorized_keys文件里面哟。很简单,scp拷贝过去然后cat一下,append一下就ok了。不多说这个。

看看这步的clone和修改文件上传文件是否有问题:

 [shihuc@CloudGame Music]$ git clone git@109.105.5.108:/data/git/mueas.git
Cloning into 'mueas'...
remote: Counting objects: , done.
remote: Compressing objects: % (/), done.
remote: Total (delta ), reused (delta )
Receiving objects: % (/), done.
Checking connectivity... done.
[shihuc@CloudGame Music]$ ll
total
drwxrwxr-x shihuc shihuc Jan : mueas
[shihuc@CloudGame Music]$ cd mueas/
[shihuc@CloudGame mueas]$ ll
total
-rw-rw-r-- shihuc shihuc Jan : file.java
-rw-rw-r-- shihuc shihuc Jan : test.txt
[shihuc@CloudGame mueas]$ vim file.java
[shihuc@CloudGame mueas]$
[shihuc@CloudGame mueas]$
[shihuc@CloudGame mueas]$
[shihuc@CloudGame mueas]$ 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: file.java no changes added to commit (use "git add" and/or "git commit -a")
[shihuc@CloudGame mueas]$ git add file.java
[shihuc@CloudGame mueas]$ git commit -m "modify with another user" *** Please tell me who you are. Run git config --global user.email "you@example.com"
git config --global user.name "Your Name" to set your account's default identity.
Omit --global to set the identity only in this repository. fatal: unable to auto-detect email address (got 'shihuc@CloudGame.(none)')
[shihuc@CloudGame mueas]$ git config --global user.email "shihucx@126.com"
[shihuc@CloudGame mueas]$ git config --global user.name "shihuc"
[shihuc@CloudGame mueas]$
[shihuc@CloudGame mueas]$ git commit -m "modify with another user"
[master 41d96e1] modify with another user
file changed, insertions(+)
[shihuc@CloudGame mueas]$
[shihuc@CloudGame mueas]$ git push
warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use: git config --global push.default matching To squelch this message and adopt the new behavior now, use: git config --global push.default simple When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name. Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch. See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git) Counting objects: , done.
Delta compression using up to threads.
Compressing objects: % (/), done.
Writing objects: % (/), bytes | bytes/s, done.
Total (delta ), reused (delta )
To git@109.105.5.108:/data/git/mueas.git
7f3f3b0..41d96e1 master -> master

是不是也没有问题,当然中间有点配置git的问题,就不多说,因为这个用户很少用,所以环境没有怎么配置好。

git在本地创建远程仓库的更多相关文章

  1. git在本地向远程仓库创建分支

    在本地的仓库种,如果想给upstream创建新分支并关联,需要执行 git push -u/--set-upstream 远程仓库名 远程分支名

  2. git修改本地和远程仓库名称的解决方法

    说明:旧的仓库名称为mygit,新的仓库名称为Blog 1.修改远程仓库名称 在GitHub上进入要修改的仓库,找到settings,修改名称. 2.修改本地仓库名称 进入存放项目的目录,我的是/ho ...

  3. 【问题解决方案】git/github本地和远程仓库的重命名

    参考: CSDN博文:在Github上重命名仓库 背景: 偶然终于看到一条规范里写着: "通常(注意是通常,尤其是 Web 相关的项目) repo 的命名用小写英文,多个字母之间用连字符(比 ...

  4. Git 创建远程仓库并克隆到本地,创建本地仓库并推送到远程仓库

    配置用户信息 配置的是你个人的用户名称和电子邮件地址.这两条配置很重要,每次 Git 提交时都会引用这两条信息,说明是谁提交了更新,会随更新内容一起被永久纳入历史记录 git config --glo ...

  5. git 入门教程之本地和远程仓库的本质

    本地仓库和远程仓库在本质上没有太大区别,只不过一个是本地电脑,一个是远程电脑. 远程仓库不一定非得是 github 那种专门的"中央服务器",甚至局域网的另外一台电脑也可以充当&q ...

  6. git的使用(包括创建远程仓库到上传代码到git的详细步骤以及git的一些常用命令)

    A创建远程仓库到上传代码到git 1)登陆或这注册git账号 https://github.com 2)创建远程仓库 3)打开终端输入命令 cd到你的本地项目根目录下,执行如下git命令 git in ...

  7. 为git创建远程仓库

    首先生成ssh公钥: 将公钥添加到git: 测试秘钥是否通过: 然后就可以到web界面看到标注的地方被绿了: 但是我的没有绿,不知道为啥,难道没有女朋友的原因吗? rm -rf .ssh 重来好几遍都 ...

  8. 本地项目git初始化并提交远程仓库

    1.先在远程仓库(如github)创建项目,为了避免错误,不要初始化 README, license, 或者gitignore文件 . 2.打开Terminal终端 3.切换到你的本地项目目录 4.初 ...

  9. Git常用命令+本地连接远程仓库

    一.git命令整理 git config --global user.email "邮箱名":绑定GitHub邮箱 git config --global user.name &q ...

随机推荐

  1. MINIX3 进程通信分析

    MINIX3 进程通信分析 6.1MINIX3 进程通信概要 MINIX3 的进程通信是 MINIX3 内核部分最重要的一个部件,我个人认为其实这 是内核中的“内核”,怎么来理解这个概念呢?其实 MI ...

  2. 使用ngx_lua构建高并发应用(1)

    转自:http://blog.csdn.net/chosen0ne/article/details/7304192 一. 概述 Nginx是一个高性能,支持高并发的,轻量级的web服务器.目前,Apa ...

  3. val() 和attr() 取值的问题

  4. [转载] codeblocks快捷键

    ==日常编辑== • 按住Ctrl滚滚轮,代码的字体会随你心意变大变小. • 在编辑区按住右键可拖动代码,省去拉(尤其是横向)滚动条之麻烦:相关设置:Mouse Drag Scrolling. • C ...

  5. div排序 根据《input》

    jquery代码如下$(function(){//alert($("input").length); var arr=Array(); $("input").e ...

  6. HOG特征(Histogram of Gradient)总结(转载)

    整理一下我个人觉得比较好的HOG博文 博文1:OpenCV HOGDescriptor: 参数与图解 http://blog.csdn.NET/raodotcong/article/details/6 ...

  7. tyvj1012 P1012 - 火柴棒等式 ——暴力枚举

    题目链接:https://www.tyvj.cn/Problem_Show.aspx?id=1012 可以发现:最大的数字绝对不超过999,只要枚举出0-999所有数字需要的火柴数,然后再枚举C和A, ...

  8. Codeforces Round #149 (Div. 2)

    A. Heads or Tails 枚举. B. Big Segment \(L=min(l_i),\ R=max(R_i)\) 判断是否存在区间\([L,R]\). C. King's Path 单 ...

  9. UVa 1586 Molar mass --- 水题

    UVa 1586 题目大意:给出一种物质的分子式(不带括号),求分子量.本题中分子式只包含4种原子,分别为C.H.O.N, 原子量分别为12.01,1.008,16.00,14.01 解题思路:先实现 ...

  10. 深入理解html5系列-文本标签

    转:http://blog.csdn.net/lihui130135/article/details/45150501 文章简介:       关于html5相信大家早已经耳熟能详,但是他真正的意义在 ...