照着 pro git book来架了个私有git 远程库,做个记录。
验证选用ssh,RSA加密方式。
首先
在远程服务器架设库:
yum install git-core
建好后,就是建一个远程裸库了:
cd /opt
mkdir git
cd git
mkdir yukonProject.git
cd yukonProject.git
git --bare init
就已经建好裸库了
新建用户
sudo adduser -m yukon12345
passwd yukon12345
su yukon12345
cd ~
mkdir .ssh
设置ssh的公钥
以下2步可以使本地计算机操作时免输入密码
生成RSA 公钥和私钥
首先看看本地电脑有没有现成的秘钥
ls -al ~/.ssh
如果有内容,就不用继续操作了
没有的话
ssh-keygen -t rsa -C "电子邮件地址"
之后一路回车就行。
在 C:/用户/.ssh文件夹里,
会生成id_rsa.pub 和id_rsa等。找到id_rsa.pub,这是公钥
把生成的公钥上传到 user的home目录,
使用 cat id_rsa.pub >> ~/.ssh/authorized_keys
或者复制id_rsa.pub 的内容到.ssh/authorized_keys
这一步为添加信任公钥。
本地的git操作练习
从远程库获取本地库
本地端:
cd d:\gittest
git clone yukon12345@yukon12345.com:/opt/git/yukonProject.git
收到反馈:
Warning: Permanently added 'yukon12345.com,114.215.191.81' (RSA) to the list of known hosts.
yukon12345@yukon12345.com's password:
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
仍然要输入密码?
这里已经把公钥提交到了authorized_keys仍然要输入密码,查了下是由于这个文件不能有所有者之外的写权限。以及.ssh也不能有之外的写权限而默认的authorized_keys 还有个用户组的写权限。
这里去除group的写权限:
su yukon12345
cd ~
chmod 700 .ssh
cd .ssh
chmod 644 authorized_keys
使得文件权限为如下:
-rw-r--r-- 1 yukon12345 yukon12345 400 Mar 3 23:33 authorized_keys
drwxr----- 2 yukon12345 yukon12345 4096 Mar 3 23:33 .ssh
再次使用clone或者pull等时无需密码。
那么就设置成功了。以后就无需输入可以直接操作远程git库。
查看状态
在gittest里会生成已个yukonProject的文件夹。里面有.git的空目录。往目录里加一个hello.txt后:
git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.txt
nothing added to commit but untracked files present (use "git add" to track)
本地库的更新提交
git add .
git commit
windows下会聪明的弹出记事本来叫你输入。执行
git log
commit 025369a380e9d1409fbacadb82d2ad56ea245fe6
Author: yukon12345 <yukon12345@163.com>
Date: Mon Mar 2 22:40:48 2015 +0800
git for new user
执行 git push
来推向远程服务器
Warning: Permanently added 'yukon12345.com,114.215.191.81' (RSA) to the list of known hosts.
yukon12345@yukon12345.com's password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 214 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
error: unpack failed: unpack-objects abnormal exit
error: insufficient permission for adding an object to repository database ./objects
fatal: failed to write object
To yukon12345@yukon12345.com:/opt/git/yukonProject.git
! [remote rejected] master -> master (n/a (unpacker error))
error: failed to push some refs to 'yukon12345@yukon12345.com:/opt/git/yukonProject.git'
无法更新库?
这里出现了报错,看了下是由于权限问题导致的。
ls -al
total 16
drwxr-xr-x. 3 root root 4096 Mar 2 22:28 .
dr-xr-xr-x. 22 root root 4096 Feb 17 17:27 ..
drwxr-xr-x 3 root root 4096 Mar 2 22:29 git
将服务器库的所有者或者所有者组权限修改:
chown -R yukon12345 /opt/git
chgrp yukon12345 /opt/git
再次push后就没有报错了
使用帮助:
如下格式:
git reset --help
这句话在windows下直接弹出帮助的网页。