远程服务器git仓库的搭建以及本地git库的首次推送首次克隆远程仓库的文件

来源:互联网 发布:pythonxy linux 编辑:程序博客网 时间:2024/05/18 03:04

参考官方文档:

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$ lsauthorized_keys2  id_dsa       known_hostsconfig            id_dsa.pub
                   我们需要寻找一对以 id_dsa 或 id_rsa 命名的文件,其中一个带有 .pub 扩展名。 .pub 文件是你的公钥,另一个则是私钥。

            2、生成公私钥:

                  如果 1 中没有相应的 id_dsa 或 id_rsa 文件,则说明名优公私钥,此时应该先生成:
                  首先 ssh-keygen 会确认密钥的存储位置(默认是 .ssh/id_rsa),然后它会要求你输入两次密钥口令。如果你不想在使用密钥时输入口令,将其留空即可。
                $ ssh-keygenGenerating 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-NDHR80KThe 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.pubssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDptxs2NHpTlalnaFN733cSOwIjdjVGBw0Bqk6raPJLNuhK+yVDcLP4sI26LPd7o9Pa209ZD0BulQh81DJTadikdwQ6TFAac8Selu6Q5cgFU1SS0XFMzTLp2WikGmQvUnkYu23iQEl+fMT2MWGcuAtpuH9mzs+WOxCWX5AUH9dcKe4NP0zR+jVj+lQU83tEjYUtGYOgAHbIX/kxjan94LFmi3KZlwxTCc4QEdZwMud1rJvtj8n05ZuvUcn9AypbD7FVkZdFpzJa8N4Dbhc6TbBKZ3QgpxPXKrvgx0bpQ3SOhMO/9qsTTvFrUdjjM6MS2is9M9YqnwNKcaTgwtoIz0jL 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:~# gitusage: 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 gitAdding 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 successfullyChanging the user information for gitEnter the new value, or press ENTER for the defaultFull Name []: gitRoom Number []: roomgitWork Phone []: 13265146272Home Phone []: 13265146272Other []:Is the information correct? [Y/n] yroot@jdu4e00u53f7:~#

第四步:创建远程git仓库

              我将仓库创建到目录 /opt/git/ 下:             
git@jdu4e00u53f7:~$ cd /optgit@jdu4e00u53f7:/opt$ su rootPassword:root@jdu4e00u53f7:/opt# mkdir gitroot@jdu4e00u53f7:/opt# cd gitroot@jdu4e00u53f7:/opt/git# mkdir myplan.gitroot@jdu4e00u53f7:/opt/git# cd myplan.git/root@jdu4e00u53f7:/opt/git/myplan.git# git init --bareInitialized empty Git repository in /opt/git/myplan.git/
               这样就创建好了。

第五步:本机git和远程git服务器建立链接

              1、先创建远程git用户中的.ssh路径以及文件:                         

root@jdu4e00u53f7:~# su gitgit@jdu4e00u53f7:/root$ cd ~git@jdu4e00u53f7:~$ mkdir .ssh && chmod 700 .sshgit@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 initInitialized empty Git repository in E:/IdeaProject/workspace-1/.git/

            2、对本地仓库中的代码整理:

git add src/git add pom.xmlgit commit -m "initial commit"[master (root-commit) 31e3fa6] initial commit 17 files changed, 1282 insertions(+)。。。
                    此时已经将代码提交到本地仓库中的本地分支master里面了。

             3、开启公匙认证:

                   在远程服务器上,下面这一步最好配置,不配置可能会导致服务器上公钥没作用!记得切换root用户,git没权限!(我自己没有试,因为我的远程服务器已经配置好了):                 
打开文件/etc/ssh/sshd_configRSAAuthentication 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 mastergit@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 ./objectsremote: fatal: failed to write objecterror: remote unpack failed: unpack-objects abnormal exitTo 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 origingit remote add origin git@117.48.200.4:/home/git/project.git/$ git push origin mastergit@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 -latotal 12drwxr-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 gitgit@jdu4e00u53f7:/opt$ sudo chown -R git:git gitsudo: 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 rootPassword:root@jdu4e00u53f7:/opt# chmod u+w /etc/sudoersroot@jdu4e00u53f7:/opt# vi /etc/sudoers此时找到这行 root ALL=(ALL) ALL,在他下面添加xxx ALL=(ALL) ALL (这里的xxx是你的用户名)添加一行:git ALL=(ALL) ALLroot@jdu4e00u53f7:/opt# chmod u-w /etc/sudoers
                然后修改git仓库的所属用户:
root@jdu4e00u53f7:/opt# su gitgit@jdu4e00u53f7:/opt$ sudo chown -R git:git gitsudo: unable to resolve host jdu4e00u53f7[sudo] password for git:git@jdu4e00u53f7:/opt$ ls -altotal 12drwxr-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 origingit remote add origin git@117.48.200.4:/opt/git/myplan.git/git push origin mastergit@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 initInitialized 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.pubssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCB007n/ww+ouN4gSLKssMxXnBOvf9LGt4LojG6rs6hPB09j9R/T17/x4lhJA0F3FR1rP6kYBRsWj2aThGw6HXLm9/5zytK6Ztg3RPKK+4kYjh6541NYsnEAZuXz0jTTyAUfrtU3Z5E003C4oxOj6H0rfIF1kKI9MAQLMdpGW1GYEIgS9EzSdfd8AcCIicTDWbqLAcU4UpkaX8KyGlLwsNuuGztobF8m72ALC/nLF6JLtPofwFBlgc+myivO7TCUSBdLQlgMVOFq1I2uPWQOkOWQAHukEOmfjy2jctxSDBQ220ymjaNsHT4kgtZg2AYYgPqdAv8JggJICUvax2T9va5 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












阅读全文
0 0
原创粉丝点击