1、git分布式版本控制系统是什么?
现在本地电脑1有一套data_system的代码,如果想把这套代码放到云服务器上,应该怎么做呢?
最简单的做法是直接将本地电脑1上的代码拷贝到服务器上。
但是我们拷贝完成后发现了一个bug1,在本地电脑1的代码中修复了之后。需要与云服务器上的代码同步,还是要将本地电脑1的代码拷贝到服务器上吗?
团队的另一个人修改了bug2,没有修改bug1,他如果将自己本地电脑2的代码拷贝到服务器上,就会将你刚刚修改的bug1代码覆盖掉。
所以我们现在需要一个第三方先将代码处理整合一下,将我们自己的代码与团队其他人的代码整合起来,再通过git pull操作将整合后的代码下拉到云服务器上。
git就是这样一个第三方的工具。帮助我们更简单的实现多个平台代码同步,以及多个开发者的代码冲突等问题。我们画个图了解一下工作流程:
2、git基本操作
-
git add filename
将本地修改的文件,添加到缓存 -
git commit -m 'Cloudox commit'
git add 命令是将代码添加到缓存区中,git commit将缓存区中的内容添加到仓库中。-m后面跟注释信息,备注本次提交的修改,方便以后维护。 -
git push origin master
将本地仓库中的内容,提交到远程master分支上(即github库中的master分支) -
git pull origin master
将远程master分支上的代码下拉下来
这几个命令是我们下面课程中较常用到的,如果你是新手,不理解也很正常,多操作几次就熟练了。git其他具体的操作可以在网上找课程学习一下,这个不是我们课程的重点,就不展开讲了。
3、通过git部署项目
(1)先通过github创建一个用来保存代码的仓库
登录github网站https://github.com/,注册登录一下。
登录后,点击右上角的头像,点击下拉菜单中的Your repositories
会进入下面页面,我们再点击new按钮新建一个仓库(repositories)
填写Repository name为我们项目的名称data_system,选择Public。点击create repository按钮完成创建
4、github仓库认证本地SSH key
本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,所以必须要让github仓库认证本地电脑的SSH key。我们需要在本地生成SSH key
(1)登录vagrant,基于宠物论坛项目,如果你只是根据本节学习git项目部署,不用登录vagrant。
xuzhaoning@xuchaoningdeMacBook-Air:~$ vagrant up
xuzhaoning@xuchaoningdeMacBook-Air:~$ vagrant ssh
(2)打开项目目录,运行ssh-keygen -t rsa -C 注册github用的邮箱
来生成SSH key
//打开项目目录
vagrant@vagrant-ubuntu-trusty-64:~$ cd /vagrant/data_system
//生成SSH key
/vagrant/data_system$ ssh-keygen -t rsa -C 注册github用的邮箱
//系统返回信息
Enter file in which to save the key (/home/vagrant/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): //直接回车即可,无需设置密码
Enter same passphrase again: //同上
Your identification has been saved in /home/vagrant/.ssh/id_rsa.
//显示生成的SSH key保存到了/home/vagrant/.ssh/id_rsa.pub文件中
Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub.
The key fingerprint is:
************加密******************
The key's randomart image is:
+--[ RSA 2048]----+
| ooo |
| o o. |
| ..E. |
| .+..+ |
| =ooS |
| ..+ |
| o. . |
| ..+o. |
| oo oo. |
+-----------------+
(3)根据系统返回信息我们得知刚刚生成SSH key保存到了/home/vagrant/.ssh/id_rsa.pub文件中,我们执行cat /home/vagrant/.ssh/id_rsa.pub
查看文件,并将文件中保存的密码复制下来(文件中所有的内容都有复制,不要落了第一行以及后面的邮箱)
/vagrant/data_system$ cat /home/vagrant/.ssh/id_rsa.pub
//复制下面信息
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6Vh5Dm+gHhRK8MmZx5DeuJPCBNkylrEp/v2F0QZgpBl4I24DWHHXKS0duS5BV4VKdUiBH3O45rcwyP9GZCUgkvp87bCm7NOZ39q7NNMQXFTO+trFBc6uy8F79AUT6bM3H+vdq2W6lXX8eFN88RZfZMJbZ1mf3X9p8RkpDK2GgJ85h18aMIN58FT/fqpJ7j8oZ3tMdtt0WI3H7eEBZjYShSiwT7Bq4v9VoEe4Kg1UIH1Ow/1hiohnyTxPe+iuDxBQmgreLv0jJszRbVgmxNcNbnjVNjvFoTzMSfycqTwSZhGKL/f6fyw4q8loyFSvLBFGkmMg+Tm/DRUUCuGM+uRYD 1369822836@qq.com
(4)点击右上角头像—settings—SSH and GPG keys--new粘贴密码
或直接点击链接https://github.com/settings/ssh/new粘贴密码
这样github仓库就认证了本地的SSH key
5、将本地项目上传到github仓库中
(1)第一次上传代码需要设置自己的邮箱和名称
邮箱建议填写github的注册邮箱
名称建议填写自己名称的全拼或者首拼
/vagrant/data_system$ git config --global user.email 你的邮箱
/vagrant/data_system$ git config --global user.name 你的名称
(2)将代码提交到本地的仓库
vagrant@vagrant-ubuntu-trusty-64:/vagrant/data_system$ git add .
vagrant@vagrant-ubuntu-trusty-64:/vagrant/data_system$ git commit -m "first"
(3)运行git remote add origin git@github.com:********.git
将本地仓库与github仓库关联,只是第一次上传代码需要这样操作。git@github.com:********.git
链接需要替换成自己刚刚创建的github仓库的ssh链接
/vagrant/data_system$ git remote add origin git@github.com:********.git
下面我们来获取一下github仓库的ssh链接
点击进入我们的仓库页面,https://github.com/Sherozn?tab=repositories,进入本项目的github远程仓库中
复制红框里的链接,添加到
git remote add origin
后面(4)将本地仓库中的代码提交到github的远程仓库中
vagrant@vagrant-ubuntu-trusty-64:/vagrant/data_system$ git push -u origin master
#系统返回信息
Counting objects: 161, done.
Compressing objects: 100% (148/148), done.
Writing objects: 100% (159/159), 101.69 KiB | 0 bytes/s, done.
Total 159 (delta 16), reused 0 (delta 0)
remote: Resolving deltas: 100% (16/16), completed with 1 local object.
To [git@github.com](mailto:git@github.com):Sherozn/data_system.git
653badd..f98de7a master -> master
Branch master set up to track remote branch master from origin.
可能出现错误
错误出现原因是远程仓库有新的更新没有下拉,我们需要先进行git pull 操作
Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
To [git@github.com](mailto:git@github.com):Sherozn/data_system.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:Sherozn/data_system.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
解决方法
#先运行git pull origin master将远程库中更新的代码下拉到本地
vagrant@vagrant-ubuntu-trusty-64:/vagrant/data_system$ git pull origin master
#系统返回信息
warning: no common commits
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
Unpacking objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
From github.com:Sherozn/data_system
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
Auto-merging .gitignore
CONFLICT (add/add): Merge conflict in .gitignore
Automatic merge failed; fix conflicts and then commit the result.
#再重新进行git commit操作
vagrant@vagrant-ubuntu-trusty-64:/vagrant/data_system$ git commit -m "first"
#系统返回信息
[master f98de7a] first
#最后将代码提交到远程库
vagrant@vagrant-ubuntu-trusty-64:/vagrant/data_system$ git push -u origin master
6、将github远程库中的代码下拉到云服务器上。
(1)ssh登录云服务器,新建并且打开项目文件夹
ubuntu@VM-16-15-ubuntu:~$ mkdir data_system
ubuntu@VM-16-15-ubuntu:~$ cd data_system
(2)在刚刚创建的data_system目录下运行 ssh-keygen -t rsa -C 注册github用的邮箱
将生成SSH key,与本地生成SSH key相同的操作,我们再来示范一下
ubuntu@VM-16-15-ubuntu:~/data_symtem$ ssh-keygen -t rsa -C 注册github用的邮箱
//系统返回信息
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ubuntu/.ssh/id_rsa.
Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub.
The key fingerprint is:
***************加密*****************
1369822836@qq.com
The key's randomart image is:
+---[RSA 2048]----+
| . o |
| o * + . |
| o O + + |
| . . B o E . |
| . S. ...o |
| . . .o + =. |
| o ..o X ..|
| . .+ =+ =..|
| .oo*=+==o. |
+----[SHA256]—————+
打开保存SSH key的文件,复制SSH key,点击右上角头像—settings—SSH and GPG keys—new将云服务器的SSH key再添加到github中,注意Title最好不要跟刚刚添加本地电脑SSH key的Title重复
ubuntu@VM-16-15-ubuntu:~/data_symtem$ cat /home/ubuntu/.ssh/id_rsa.pub
#复制下面信息粘贴到github保存SSH key的页面
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDCKL0MGoJnVk5Ecc+GWe02GYkvXKRN9hnWcuxGjt1k4fd9sIuICaLQKf4QZubYfH/PyzKp2J+BOS7Gj7IYLz/++hpP0lgf43GOOblVaNLQKV8sbpvmne0XIBIi5SC+VzkIMmgf0PLpoL8/5Xx7L/fabHyOBse0iXgl7yY+Tj1nKEavN/E/RQVng7YU2HDQCvv/Q/ukWT+Xqit9QEb8DahAq6JEYw/JSWldv2YAyDNny/HvmgFzOo/hohXxrqwvlE9/Jy+EPF+xK2SVOwtSTNsgbx+iVrUG/XDYkpTWgbeO30W+cPfwcpCWVxni+kxYU+VOpyUYjJVafPg1uQ24tz [1369822836@qq.com](mailto:1369822836@qq.com)
(3)git pull下拉代码
#将云服务与github仓库关联
ubuntu@VM-16-15-ubuntu:~/data_symtem$ git remote add origin git@github.com:******************
#系统返回信息报错,缺少.git文件。那我们需要先执行一下git init
fatal: Not a git repository (or any of the parent directories): .git
#执行git init
ubuntu@VM-16-15-ubuntu:~/data_symtem$ git init
#系统返回信息
Initialized empty Git repository in /home/ubuntu/data_symtem/.git/
#再重新运行git remote
ubuntu@VM-16-15-ubuntu:~/data_symtem$ git remote add origin git@github.com:******************
#将代码下拉到云服务器上
ubuntu@VM-16-15-ubuntu:~/data_symtem$ git pull origin master
#系统返回信息
The authenticity of host 'github.com(52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.
remote: Enumerating objects: 162, done.
remote: Counting objects: 100% (162/162), done.
remote: Compressing objects: 100% (134/134), done.
Receiving objects: 100% (162/162), 102.81 KiB | 51.00 KiB/s, done.
Resolving deltas: 100% (16/16), done.
remote: Total 162 (delta 16), reused 160 (delta 16), pack-reused 0
From [github.com:Sherozn/data_system](http://github.com:Sherozn/data_system)
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
(4)测试,查看云服务上的代码
//执行ls,出现以下目录,说明项目已经部署完成
ubuntu@VM-16-15-ubuntu:~/data_symtem$ ls
#系统返回信息
app config data_system Gemfile lib package.json Rakefile test vendor
bin config.ru db Gemfile.lock log public README.md tmp
网友评论