git本地创建 push仓库_git在本地创建远程仓库

本文详细介绍了如何在Linux服务器上创建一个以git@repo-host:/path/proj.git格式的Git远程仓库。步骤包括安装Git,创建仓库目录,设置git用户,初始化裸仓库,调整权限,配置SSH免密登录。通过这些步骤,可以实现本地Git仓库与远程仓库之间的正常推送和克隆操作。
摘要由CSDN通过智能技术生成

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

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

其实比较简单:

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

1 [root@CloudGame tools]# git --version2 git version 2.6.0-rc1

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

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

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

1 [root@CloudGame home]# useradd -r -d /data/git git  #添加git用户为系统用户,并指定其home目录为/data/git

1 [root@CloudGame home]# chown -R git:git /data/git   #将git用户的家目录设置为git组,git用户

1 [root@CloudGame home]# cd /data2 [root@CloudGame data]# ll3 total 4

4 drwxr-xr-x 2 git git 4096 Jan 20 09:25git5 [root@CloudGame data]# cd git/

6 [root@CloudGame git]# ll                 #查看目录内容为空,说明目前里面什么也没有,的确,还没有做任何操作呢。7 total 0

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

1 [root@CloudGame git]# git init --bare mueas.git    #注意,这里最好带上--bare指定一个空仓库,否则客户端clone后,提交代码时,会遇到错误,要做系列配置2 Initialized empty Git repository in /data/git/mueas.git/

3 [root@CloudGame git]# ll4 total 4

5 drwxr-xr-x 7 root root 4096 Jan 20 09:28mueas.git6 [root@CloudGame git]# ll -al7 total 12

8 drwxr-xr-x 3 git git 4096 Jan 20 09:28.9 drwxr-xr-x 3 root root 4096 Jan 20 09:25..10 drwxr-xr-x 7 root root 4096 Jan 20 09:28mueas.git11 [root@CloudGame git]# cd mueas.git/

12 [root@CloudGame mueas.git]# ll13 total 32

14 drwxr-xr-x 2 root root 4096 Jan 20 09:28branches15 -rw-r--r-- 1 root root 66 Jan 20 09:28config16 -rw-r--r-- 1 root root 73 Jan 20 09:28description17 -rw-r--r-- 1 root root 23 Jan 20 09:28HEAD18 drwxr-xr-x 2 root root 4096 Jan 20 09:28hooks19 drwxr-xr-x 2 root root 4096 Jan 20 09:28 info

20 drwxr-xr-x 4 root root 4096 Jan 20 09:28objects21 drwxr-xr-x 4 root root 4096 Jan 20 09:28 refs

1 [root@CloudGame git]# chown -R git.git /data/git/*

2 [root@CloudGame git]# ll3 total 44 drwxr-xr-x 7 git git 4096 Jan 20 09:28 mueas.git5 [root@CloudGame git]# cd mueas.git/6 [root@CloudGame mueas.git]# ll7 total 328 drwxr-xr-x 2 git git 4096 Jan 20 09:28 branches9 -rw-r--r-- 1 git git 66 Jan 20 09:28 config10 -rw-r--r-- 1 git git 73 Jan 20 09:28 description11 -rw-r--r-- 1 git git 23 Jan 20 09:28 HEAD12 drwxr-xr-x 2 git git 4096 Jan 20 09:28 hooks13 drwxr-xr-x 2 git git 4096 Jan 20 09:28 info14 drwxr-xr-x 4 git git 4096 Jan 20 09:28 objects15 drwxr-xr-x 4 git git 4096 Jan 20 09:28 refs

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

1 lighttpd:x:501:501::/home/lighttpd:/sbin/nologin2 dockerroot:x:494:488:Docker User:/var/lib/docker:/sbin/nologin3 stack:x:502:502::/opt/stack:/bin/bash4 chrony:x:493:487::/var/lib/chrony:/sbin/nologin5 git:x:492:486::/data/git:/usr/bin/git-shell6 "/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文件,公私都有】

1 [root@CloudGame git]# mkdir .ssh        #创建.ssh目录,注意目录的层次关系

2 [root@CloudGame git]# ll3 total 4

4 drwxr-xr-x 7 git git 4096 Jan 20 09:28mueas.git5 [root@CloudGame git]# ll -al6 total 16

7 drwxr-xr-x 4 git git 4096 Jan 20 09:32.8 drwxr-xr-x 3 root root 4096 Jan 20 09:25..9 drwxr-xr-x 7 git git 4096 Jan 20 09:28mueas.git10 drwxr-xr-x 2 root root 4096 Jan 20 09:33 .ssh

1 [root@CloudGame .ssh]# cd ..          #在.ssh目录下创建authorized_keys文件2 [root@CloudGame git]# cd .ssh

3 [root@CloudGame .ssh]# ll -al4 total 8

5 drwxr-xr-x 2 root root 4096 Jan 20 10:16.6 drwxr-xr-x 4 git git 4096 Jan 20 10:16..7 [root@CloudGame .ssh]# vim authorized_keys

1 [root@CloudGame tools]# cat ~/.ssh/id_rsa.pub    #查看id_rsa.pub内容,并将其显示出来的内容(蓝色部分)copy到/data/git/.ssh/authorized_keys文件里面2 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAutquVDcyjoxwXzbrgLcu/wlK9SkXykkd5mktSPqA4exUc6flDv5dYzT3sWMYaH4LP/fiT2mhAoPRU8HaejOfnU3+ALunjXBtxr8XDZQDNrHnZ31477IUSBJ6XRlEj+sDVBDujAxGhNpP41B4v/bSpbrkOJGuVhUtcl81V/nKrCwvhpX+mGRviuiIRsv7E8HEb3AZ7hLXibuDP7kSe3M5nO3JOnsE7e3h8Ob7WAmkxPU/bGqALAodrp0vUyyLsdUt1lynauUZmOgaowL9C+eTbEtFQvCrVrRbXz6GE0VfS7WUA7rxtMujIxuh2fdCWIH4J/wuA+ul3qPsKEDa1MiBSQ== root@CloudGame

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

1 [root@CloudGame tmp]# git clone git@109.105.5.108:/data/git/mueas.git2 Cloning into 'mueas'...3 Agent admitted failure to sign using the key.4 git@109.105.5.108's password:

5 Permission denied, please try again.

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

1 [root@CloudGame tmp]# ssh-add

2 Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)

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

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

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

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

1 [root@CloudGame mueas]# ll2 total 8

3 -rw-r--r-- 1 root root 14 Jan 20 10:57 file.java4 -rw-r--r-- 1 root root 6 Jan 20 10:31test.txt5 [root@CloudGame mueas]# git add file.java6 [root@CloudGame mueas]# git commit -m "New file added"

7 [master 7f3f3b0] New fileadded8 1 file changed, 1 insertion(+)9 create mode 100644 file.java10 [root@CloudGame mueas]# git push11 Counting objects: 3, done.12 Delta compression using up to 4threads.13 Compressing objects: 100% (2/2), done.14 Writing objects: 100% (3/3), 291 bytes | 0 bytes/s, done.15 Total 3 (delta 0), reused 0 (delta 0)16 To git@109.105.5.108:/data/git/mueas.git17 5837025..7f3f3b0 master -> master

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

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

1 [shihuc@CloudGame Music]$ git clone git@109.105.5.108:/data/git/mueas.git2 Cloning into 'mueas'...3 remote: Counting objects: 6, done.4 remote: Compressing objects: 100% (3/3), done.5 remote: Total 6 (delta 0), reused 0 (delta 0)6 Receiving objects: 100% (6/6), done.7 Checking connectivity... done.8 [shihuc@CloudGame Music]$ ll9 total 4

10 drwxrwxr-x 3 shihuc shihuc 4096 Jan 20 10:59mueas11 [shihuc@CloudGame Music]$ cd mueas/

12 [shihuc@CloudGame mueas]$ ll13 total 8

14 -rw-rw-r-- 1 shihuc shihuc 14 Jan 20 10:59 file.java15 -rw-rw-r-- 1 shihuc shihuc 6 Jan 20 10:59test.txt16 [shihuc@CloudGame mueas]$ vim file.java17 [shihuc@CloudGame mueas]$18 [shihuc@CloudGame mueas]$19 [shihuc@CloudGame mueas]$20 [shihuc@CloudGame mueas]$ git status21 On branch master22 Your branch is up-to-date with 'origin/master'.23 Changes not staged forcommit:24 (use "git add ..."to update what will be committed)25 (use "git checkout -- ..." to discard changes inworking directory)26

27 modified: file.java28

29 no changes added to commit (use "git add" and/or "git commit -a")30 [shihuc@CloudGame mueas]$ git add file.java31 [shihuc@CloudGame mueas]$ git commit -m "modify with another user"

32

33 *** Please tell me whoyou are.34

35 Run36

37 git config --global user.email "you@example.com"

38 git config --global user.name "Your Name"

39

40 to set your account's default identity.

41 Omit --global to set the identity only inthis repository.42

43 fatal: unable to auto-detect email address (got 'shihuc@CloudGame.(none)')44 [shihuc@CloudGame mueas]$ git config --global user.email "shihucx@126.com"

45 [shihuc@CloudGame mueas]$ git config --global user.name "shihuc"

46 [shihuc@CloudGame mueas]$47 [shihuc@CloudGame mueas]$ git commit -m "modify with another user"

48 [master 41d96e1] modify with another user49 1 file changed, 8 insertions(+)50 [shihuc@CloudGame mueas]$51 [shihuc@CloudGame mueas]$ git push52 warning: push.default is unset; its implicit value has changed in

53 Git 2.0 from 'matching' to 'simple'. To squelch this message54 and maintain the traditional behavior, use:55

56 git config --global push.default matching57

58 To squelch this message and adopt the new behavior now, use:59

60 git config --global push.default simple61

62 When push.default is set to 'matching', git will push local branches63 to the remote branches that already exist with the same name.64

65 Since Git 2.0, Git defaults to the more conservative 'simple'

66 behavior, whichonly pushes the current branch to the corresponding67 remote branch that 'git pull'uses to update the current branch.68

69 See 'git help config' and search for 'push.default' forfurther information.70 (the 'simple' mode was introduced in Git 1.7.11. Use the similar mode71 'current' instead of 'simple' ifyou sometimes use older versions of Git)72

73 Counting objects: 3, done.74 Delta compression using up to 4threads.75 Compressing objects: 100% (3/3), done.76 Writing objects: 100% (3/3), 363 bytes | 0 bytes/s, done.77 Total 3 (delta 0), reused 0 (delta 0)78 To git@109.105.5.108:/data/git/mueas.git79 7f3f3b0..41d96e1 master -> master

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值