# yum -y install git
# git --version
git version 1.7.1 #注意服务器版本和git版本,不同版本的系统的git配置可能不同
# vim /etc/ssh/sshd_config #修改配置文件允许通过密钥认证
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_key
# useradd git # 添加用户,注意这里要和你未来web服务器的名字要相同,如果不是做web服务器用的可以随便取名字
# passwd git# 添加密码
# chown -R git:git /path/to/projectDir/ # 修改仓库的属组和属主
/srv/sample.git
,在/srv
目录下输入命令: git init --bare sample.git
$ download https://git-scm.com/download/win #在windows下下载git客户端
$ 安装、右键打开Git Bash
$ ssh git@gitserverIp #ssh连接你的web服务器,如ssh [email protected], 这里是git是你刚刚添加的用户。
输入yes
这里需要输入yes,是ssh的安全机制,用于首次通信,第二次连接就不会有了,此时C:\Users\用户\.ssh 多出一个known_hosts文件
$ ssh-keygen -t rsa #生成公钥和密钥,此时 C:\Users\用户\.ssh\会多出两个文件id_rsa.pub和id_rsa.pub,公钥和私钥,复制id_rsa.pub内容
# mkdir -p /home/git/.ssh #由配置文件我们把认证信息放到了用户家目录下的.ssh文件夹中,www为刚刚添加的用户
# vim /home/git/.ssh/authorized_keys #粘贴你刚刚复制的id_rsa.pub内容
# chmod 700 /home/git/.ssh/ #为了保证安全性,需要修改权限
# chmod 600 /home/git/.ssh/authorized_keys #修改文件权限
# usermod -s /usr/bin/git-shell git#不允许该用户登录,只能做git操作
如果想要简洁操作可以在Windows git bash上使用这个命令:
ssh-copy-id -i /c/Users/用户名/.ssh/id_rsa.pub [email protected]
linux也可以使用上面这条命令或者执行
# ssh-keygen //生成linux的公钥和私钥 /root/.ssh目录下面
# ls
authorized_keys(公钥) id_dsa known_hosts 将公钥拷贝到到服务器的/home/git/.ssh/authorized_keys
# git clone [email protected]:/disk/git/test.git 从服务器克隆
Initialized empty Git repository in /root/.ssh/test/.git/
The authenticity of host '192.168.0.246 (192.168.0.246)' can't be established.
RSA key fingerprint is 8f:17:75:e3:1b:8b:91:0b:91:62:e1:a3:02:8d:83:ff.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.246' (RSA) to the list of known hosts.
warning: You appear to have cloned an empty repository.(第一次是空的还没放文件呢)
更新版本
# git add .
# git commit -m "first commit" //注释
[master (root-commit) 69e2d7a] first commit
Committer: root
Your name and email address were configured automatically based //linuc用户会自动注册email和用户
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name" 或使用这2条命令 在windows下需要执行 不然会报错
git config --global user.email [email protected]
If the identity used for this commit is wrong, you can fix it with:
git commit --amend --author='Your Name
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 1.test
# git remote add gitserv [email protected]:/disk/git/test.git 给远程服务器起个别名
# git push -u gitserv master 上传
注意:如果有多个人员参与,每个人的公钥都要追加到authorized_keys里面,ssh-copy-id命令会自动创建.ssh文件夹和追加公钥到authorized_keys文件里
$ download
https://download.tortoisegit.org/tgit/2.4.0.0/TortoiseGit-2.4.0.2-64bit.msi #
https://download.tortoisegit.org/tgit/2.4.0.0/TortoiseGit-LanguagePack-2.4.0.0-64bit-zh_CN.msi #官方汉化工具
$ 在项目文件上,右键
URL:[email protected]:/path/to/projectDir/
更多命令见https://www.liaoxuefeng.com/wiki/896043488029600