远程服务器git仓库的搭建以及本地git库的首次推送首次克隆远程仓库的文件
2017-08-28 01:04
811 查看
参考官方文档:
我们需要寻找一对以 id_dsa 或 id_rsa 命名的文件,其中一个带有 .pub 扩展名。 .pub 文件是你的公钥,另一个则是私钥。
首先 ssh-keygen 会确认密钥的存储位置(默认是 .ssh/id_rsa),然后它会要求你输入两次密钥口令。如果你不想在使用密钥时输入口令,将其留空即可。
他们所要做的就是复制各自的 .pub 文件内容,并将其通过邮件发送。 公钥看起来是这样的:
我们需要将这段密钥复制,保存好,一会需要用到。
模板:
实例:
这个就说明安装好了,如果没有安装,就需要安装,下面一步。
我的服务器是ubuntu,就这样就可以了,不同的服务器不一样。
这样就创建好了。
然后将公钥保存到 authorized_keys 。
2)一种直接在代码工程的目录,将工程目录变成本地仓库
我现在是直接在代码工程目录,直接执行:
此时已经将代码提交到本地仓库中的本地分支master里面了。
操作:
此时不能修改的原因是因为新建的git用户没有加入到root组,不能使用sudo:
解决方法如下:
然后修改git仓库的所属用户:
然后在新机器的一个即将创建的仓库中执行:
https://git-scm.com/book/zh/v2/服务器上的-Git-生成-SSH-公钥
https://git-scm.com/book/zh/v2/%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E7%9A%84-Git-%E9%85%8D%E7%BD%AE%E6%9C%8D%E5%8A%A1%E5%99%A8
第一步:在本机上创建公私钥
本机上安装git就不说了,很简单的。1、首先查看是否已经有了公私钥:
$ cd ~/.ssh $ ls authorized_keys2 id_dsa known_hosts config id_dsa.pub
我们需要寻找一对以 id_dsa 或 id_rsa 命名的文件,其中一个带有 .pub 扩展名。 .pub 文件是你的公钥,另一个则是私钥。
2、生成公私钥:
如果 1 中没有相应的 id_dsa 或 id_rsa 文件,则说明名优公私钥,此时应该先生成:首先 ssh-keygen 会确认密钥的存储位置(默认是 .ssh/id_rsa),然后它会要求你输入两次密钥口令。如果你不想在使用密钥时输入口令,将其留空即可。
$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/Coral/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /c/Users/Coral/.ssh/id_rsa. Your public key has been saved in /c/Users/Coral/.ssh/id_rsa.pub. The key fingerprint is: SHA256:qcyHhGozWoDSZJf8VOMPyUF1FDiDhzNN6dfu463nsuE Coral@DESKTOP-NDHR80K The key's randomart image is: +---[RSA 2048]----+ | .=*o++. | | . . +=+B. | | o + . == o . | |.+ . + +. . . | |+ . . o S .. . | |.. . + o . | | * = . .. | | + o . .+o.| |. .EBo| +----[SHA256]-----+
3、查看公钥并保存:
现在,进行了上述操作的用户需要将各自的公钥发送给任意一个 Git 服务器管理员(假设服务器正在使用基于公钥的 SSH 验证设置)。他们所要做的就是复制各自的 .pub 文件内容,并将其通过邮件发送。 公钥看起来是这样的:
$ cat ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDptxs2NHpTlaln aFN733cSOwIjdjVGBw0Bqk6raPJLNuhK+yVDcLP4sI26LPd7o9Pa 209ZD0BulQh81DJTadikdwQ6TFAac8Selu6Q5cgFU1SS0XFMzTLp 2WikGmQvUnkYu23iQEl+fMT2MWGcuAtpuH9mzs+WOxCWX5AUH9dc Ke4NP0zR+jVj+lQU83tEjYUtGYOgAHbIX/kxjan94LFmi3KZlwxTC c4QEdZwMud1rJvtj8n05ZuvUcn9AypbD7FVkZdFpzJa8N4Dbhc6TbBK Z3QgpxPXKrvgx0bpQ3SOhMO/9qsTTvFrUdjjM6MS2is9M9YqnwNKcaT gwtoIz0jL Coral@DESKTOP-NDHR80K
我们需要将这段密钥复制,保存好,一会需要用到。
4、设置邮箱和用户名:
这个不设置的话,后面本地仓库中使用命令 git commit -m "" 时,会报错,内容是让你创建邮箱和用户名。模板:
git config --global user.email "you@example.com" git config --global user.name "Your Name"
实例:
$ git config --global user.email "1129988388@qq.com" $ git config --global user.name "wangtianyuan"
第二步:远程git的安装
1、首先测试远程服务是否已经安装了git:
root@jdu4e00u53f7:~# git usage: git [--version] [--help] [-C <path>] [-c name=value] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path] [-p | --paginate | --no-pager] [--no-replace-objects] [--bare] [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>] <command> [<args>] These are common Git commands used in various situations:
这个就说明安装好了,如果没有安装,就需要安装,下面一步。
2、安装git:
apt-get install git
我的服务器是ubuntu,就这样就可以了,不同的服务器不一样。
第三步:远程服务器创建git用户
首先创建git用户:root@jdu4e00u53f7:~# adduser git Adding user `git' ... Adding new group `git' (1000) ... Adding new user `git' (1000) with group `git' ... Creating home directory `/home/git' ... Copying files from `/etc/skel' ... Enter new UNIX password: //这里需要输入你创建git的用户密码 Retype new UNIX password: //重复输入密码 passwd: password updated successfully Changing the user information for git Enter the new value, or press ENTER for the default Full Name []: git Room Number []: roomgit Work Phone []: 13265146272 Home Phone []: 13265146272 Other []: Is the information correct? [Y/n] y root@jdu4e00u53f7:~#
第四步:创建远程git仓库
我将仓库创建到目录 /opt/git/ 下:git@jdu4e00u53f7:~$ cd /opt git@jdu4e00u53f7:/opt$ su root Password: root@jdu4e00u53f7:/opt# mkdir git root@jdu4e00u53f7:/opt# cd git root@jdu4e00u53f7:/opt/git# mkdir myplan.git root@jdu4e00u53f7:/opt/git# cd myplan.git/ root@jdu4e00u53f7:/opt/git/myplan.git# git init --bare Initialized empty Git repository in /opt/git/myplan.git/
这样就创建好了。
第五步:本机git和远程git服务器建立链接
1、先创建远程git用户中的.ssh路径以及文件:
root@jdu4e00u53f7:~# su git git@jdu4e00u53f7:/root$ cd ~ git@jdu4e00u53f7:~$ mkdir .ssh && chmod 700 .ssh git@jdu4e00u53f7:~$ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys
2、将本机git中那个公钥(第一步中3的公钥),复制到authorized_keys文件中:
git@jdu4e00u53f7:~$ vim .ssh/authorized_keys
然后将公钥保存到 authorized_keys 。
第六步:将本地代码推送到远程仓库
1、建立本地仓库:
1)一种是你直接建立本地仓库,然后将代码放入仓库中2)一种直接在代码工程的目录,将工程目录变成本地仓库
我现在是直接在代码工程目录,直接执行:
$ git init Initialized empty Git repository in E:/IdeaProject/workspace-1/.git/
2、对本地仓库中的代码整理:
git add src/ git add pom.xml git commit -m "ini 4000 tial commit" [master (root-commit) 31e3fa6] initial commit 17 files changed, 1282 insertions(+) 。。。
此时已经将代码提交到本地仓库中的本地分支master里面了。
3、开启公匙认证:
在远程服务器上,下面这一步最好配置,不配置可能会导致服务器上公钥没作用!记得切换root用户,git没权限!(我自己没有试,因为我的远程服务器已经配置好了):打开文件/etc/ssh/sshd_config RSAAuthentication yes #开启RSA认证功能 PubkeyAuthentication yes #开启公匙认证 StricModes no #据说不改会强制要求登录用户和文件拥有者用户相同
4、将本地仓库的代码推送到远程仓库:
使用命令:git remote add origin git@ip:远程仓库目录操作:
$ git remote add origin git@117.48.200.4:/opt/git/myplan.git/ $ git push origin master git@117.48.200.4's password: //此时需要输入远程中git用户的密码,最开始设置的 Counting objects: 38, done. Delta compression using up to 8 threads. Compressing objects: 100% (27/27), done. Writing objects: 100% (38/38), 10.94 KiB | 1.37 MiB/s, done. Total 38 (delta 1), reused 0 (delta 0) remote: error: insufficient permission for adding an object to repository database ./objects remote: fatal: failed to write object error: remote unpack failed: unpack-objects abnormal exit To 117.48.200.4:/opt/git/myplan.git/ ! [remote rejected] master -> master (unpacker error) error: failed to push some refs to 'git@117.48.200.4:/opt/git/myplan.git/' 此时:无法推送到服务器上,我猜是服务器上/opt/git目录的权限问题, 我现在修改一下远程仓库的位置,我在git用户下新建一个仓库然后修改下remote: git remote rm origin git remote add origin git@117.48.200.4:/home/git/project.git/ $ git push origin master git@117.48.200.4's password: Counting objects: 38, done. Delta compression using up to 8 threads. Compressing objects: 100% (27/27), done. Writing objects: 100% (38/38), 10.94 KiB | 1.37 MiB/s, done. Total 38 (delta 1), reused 0 (delta 0) To 117.48.200.4:/home/git/project.git/ * [new branch] master -> master 然后推送就成功了,远程仓库
5、解决 4 中的问题:原因是/opt/git的路径的所属用户问题,修改下文件所属用户:
首先cd 进入仓库目录: git@jdu4e00u53f7:/$ cd opt/ git@jdu4e00u53f7:/opt$ ls -la total 12 drwxr-xr-x 3 root root 4096 Aug 27 21:07 . drwxr-xr-x 23 root root 4096 Aug 23 23:33 .. drwxr-xr-x 3 root root 4096 Aug 27 21:08 git git@jdu4e00u53f7:/opt$ sudo chown -R git:git git sudo: unable to resolve host jdu4e00u53f7 [sudo] password for git: Sorry, try again. [sudo] password for git: git is not in the sudoers file. This incident will be reported.
此时不能修改的原因是因为新建的git用户没有加入到root组,不能使用sudo:
解决方法如下:
git@jdu4e00u53f7:/opt$ su root Password: root@jdu4e00u53f7:/opt# chmod u+w /etc/sudoers root@jdu4e00u53f7:/opt# vi /etc/sudoers 此时找到这行 root ALL=(ALL) ALL,在他下面添加xxx ALL=(ALL) ALL (这里的xxx是你的用户名) 添加一行:git ALL=(ALL) ALL root@jdu4e00u53f7:/opt# chmod u-w /etc/sudoers
然后修改git仓库的所属用户:
root@jdu4e00u53f7:/opt# su git git@jdu4e00u53f7:/opt$ sudo chown -R git:git git sudo: unable to resolve host jdu4e00u53f7 [sudo] password for git: git@jdu4e00u53f7:/opt$ ls -al total 12 drwxr-xr-x 3 root root 4096 Aug 27 21:07 . drwxr-xr-x 23 root root 4096 Aug 23 23:33 .. drwxr-xr-x 3 git git 4096 Aug 27 21:08 git
6、重新推送本地仓库文件到远程/opt/git:
git remote rm origin git remote add origin git@117.48.200.4:/opt/git/myplan.git/ git push origin master git@117.48.200.4's password: //输入远程git用户的密码 Counting objects: 38, done. Delta compression using up to 8 threads. Compressing objects: 100% (27/27), done. Writing objects: 100% (38/38), 10.94 KiB | 1.82 MiB/s, done. Total 38 (delta 1), reused 0 (delta 0) To 117.48.200.4:/opt/git/myplan.git/ * [new branch] master -> master
第七步:新机器获取远程仓库中的代码:
如果一个新机器需要获取远程仓库中的代码,需要先和远程仓库链接,首先需要将新机器中生成的公钥,放入远程用户git中的.ssh文件中,/home/git/.ssh然后在新机器的一个即将创建的仓库中执行:
$ git init Initialized empty Git repository in E:/ideaworkspace-myplan/.git/ $ git clone git@117.48.200.4:/opt/git/myplan.git/ Cloning into 'myplan'... git@117.48.200.4's password: //远程用户git的密码 remote: Counting objects: 38, done. remote: Compressing objects: 100% (27/27), done. remote: Total 38 (delta 1), reused 0 (delta 0) Receiving objects: 100% (38/38), 10.94 KiB | 279.00 KiB/s, done. Resolving deltas: 100% (1/1), done.
额外问题:
常用公钥保存: 我们需要为系统用户 git 的 authorized_keys 文件添加一些开发者 SSH 公钥。 假设我们已经获得了若干受信任的公钥,并将它们保存在临时文件中。 与前文类似,这些公钥看起来是这样的: $ cat /tmp/id_rsa.john.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCB007n/ww+ouN4gSLKssMxXnBOvf9LGt4L ojG6rs6hPB09j9R/T17/x4lhJA0F3FR1rP6kYBRsWj2aThGw6HXLm9/5zytK6Ztg3RPKK+4k Yjh6541NYsnEAZuXz0jTTyAUfrtU3Z5E003C4oxOj6H0rfIF1kKI9MAQLMdpGW1GYEIgS9Ez Sdfd8AcCIicTDWbqLAcU4UpkaX8KyGlLwsNuuGztobF8m72ALC/nLF6JLtPofwFBlgc+myiv O7TCUSBdLQlgMVOFq1I2uPWQOkOWQAHukEOmfjy2jctxSDBQ220ymjaNsHT4kgtZg2AYYgPq dAv8JggJICUvax2T9va5 gsg-keypair 将这些公钥加入系统用户 git 的 .ssh 目录下 authorized_keys 文件的末尾: $ cat /tmp/id_rsa.john.pub >> ~/.ssh/authorized_keys $ cat /tmp/id_rsa.josie.pub >> ~/.ssh/authorized_keys $ cat /tmp/id_rsa.jessica.pub >> ~/.ssh/authorized_keys
相关文章推荐
- 自己搭建Git服务器实现SSH以及远程仓库迁移
- [置顶] 本地git仓库推送到服务器自建的git仓库实现目录文件同步教程[自整理]
- git的常用操作(克隆到本地和推送到远程仓库)
- git 将本地文件推送到Github远程仓库
- Git学习笔记(4)——添加远程仓库,克隆远程库,以及库的推送
- 删除本地文件后 Git从远程仓库重新获取
- windows下多个git账号配置并且将本地仓库项目推送到远程仓库
- windows下多个git账号配置并且将本地仓库项目推送到远程仓库
- GIT 如何合并另一个远程Git仓库的文件到本地仓库里某个指定子文件夹并不丢失远程提交记录?
- Git - 如何修改开源项目并推送给作者 / 克隆远程仓库到本地
- mac本机pySpark配置并且能在本地远程调用服务器Spark以及文件
- 手把手git教程(05)--远程仓库推送和克隆
- windows下多个git账号配置并且将本地仓库项目推送到远程仓库
- windows下多个git账号配置并且将本地仓库项目推送到远程仓库
- Git :本地仓库提交到远程服务器
- Git的使用(本地仓库的设置,文件添加,关联github远程仓库)
- 在本地用命令行创建一个git仓库,并推送到远程
- windows下多个git账号配置并且将本地仓库项目推送到远程仓库
- git关联本地文件到远程仓库
- GIT 如何合并另一个远程Git仓库的文件到本地仓库里某个指定子文件夹并不丢失远程提交记录?