Git(分布式版本控制系统)

Git 理论

版本控制是一种记录一个或若个文件内容变化,以便将来查阅特定版本修订情况的系统(记录代码文件的变化),采用版本控制系统(version control system—>VCS)你就可以将某个文件回溯到之前的状态,甚看,至将整个项目都回退到过去某个时间点的状态,你可以比较文件的变化细节,查出最后是谁修改了哪个地方,从而找出导致怪异问题出现的原因,又是谁在何时报告了某个功能缺陷等等,使用版本控制系统通常还意味着,就算你乱来一气把整个项目中的文件改的改删的删,你也照样可以轻松恢复到原先的样子,但额外增加的工作量却微乎其微

Git是一个分布式版本控制系统:记录项目代码

版本控制系统存在的方式(VCS version control system):
1.简单的VCS 单个数据库记录代码的内容变化
2.集中式的版本控制系统CVCS centralized version control system svn
3.分布式的版本控制系统DVCS distributed version control system
集中式的缺点是:单节点,会出现宕机的现象,不能及时处理
在这里插入图片描述

svn:

SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS、CVS,它采用了分支管理系统,它的设计目标就是取代CVS。互联网上很多版本控制服务已从CVS迁移到Subversion。说得简单一点SVN就是用于多个人共同开发同一个项目,共用资源的目的。

分布式VCS
在这里插入图片描述
git特点
1.直接记录快照,而非差异比较(不用比较差异)
2.Git一般只增加数据(拉取、上传代码)
3.Git保证代码完整性(git在每一次提交上传的时候,会比较远程仓库的信息校验和本地仓库的信息校验是否一样,一样的话继续上传,不一样停止上传,任何事情不可以绕开git操作)

git常规操作逻辑:
1.添加到暂存区add
2.添加到本地仓库commit(新写的代码)
3.上传到远程仓库push

SVN与Git的最主要的区别?

SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以首先要从中央服务器哪里得到最新的版本,然后干活,干完后,需要把自己做完的活推送到中央服务器。集中式版本控制系统是必须联网才能工作,如果在局域网还可以,带宽够大,速度够快,如果在互联网下,如果网速慢的话,就纳闷了。

Git是分布式版本控制系统,那么它就没有中央服务器的,每个人的电脑就是一个完整的版本库,这样,工作的时候就不需要联网了,因为版本都是在自己的电脑上。既然每个人的电脑都有一个完整的版本库,那多个人如何协作呢?比如说自己在电脑上改了文件A,其他人也在电脑上改了文件A,这时,你们两之间只需把各自的修改推送给对方,就可以互相看到对方的修改了。

安装git

[root@localhost ~]# yum -y install git

一、搭建本地仓库

[root@localhost ~]# mkdir /test
[root@localhost ~]# cd /test/
[root@localhost test]# git init  #将当前的目录作为本地仓库
初始化空的 Git 版本库于 /test/.git/
[root@localhost test]# ls -a
.  ..  .git

把代码上传到本地仓库

[root@localhost test]# vim test.py
#!/usr/bin/python
print("蚊子 小奥奥");
[root@localhost test]# git add test.py  #添加到暂存区
[root@localhost test]# git status #查看代码当前的状态
# 位于分支 master
#
# 初始提交
#
# 要提交的变更:
#   (使用 "git rm --cached <file>..." 撤出暂存区)
#
#	新文件:    test.py
[root@localhost test]# git commit -m "first" #提交到本地仓库
*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@localhost.(none)')


#注:再提交到本地之前就需要写上用户名和邮箱 如果不切换目录,用户名和邮箱只需要写一次,之后就默认使用这个邮箱和用户名

[root@localhost test]# vim test.py
#!/usr/bin/python
print("蚊子 小奥奥");
print("蚊子 果果牛");

[root@localhost test]# git add test.py 
[root@localhost test]# git config --global user.name "haha"
[root@localhost test]# git config --global user.email haha@161.com
[root@localhost test]# git commit -m "first"
[master(根提交) 028602e] first
 1 file changed, 3 insertions(+)
 create mode 100644 test.py

代码的回滚
1、在暂存区中回滚

#!/usr/bin/python
print("蚊子 小奥奥");
print("蚊子 果果牛");
#添加
print("1111");
[root@localhost test]# git add test.py 
[root@localhost test]# git status
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#	修改:      test.py
#
[root@localhost test]# git reset HEAD test.py #撤出暂存区
重置后撤出暂存区的变更:
M	test.py
[root@localhost test]# git checkout -- test.py  #回滚
[root@localhost test]# cat test.py  #可以看到刚刚添加的1111没有了
#!/usr/bin/python
print("蚊子 小奥奥");
print("蚊子 果果牛");

2、已经添加到本地仓库当中,进行回滚

[root@localhost test]# vim test.py  #添加代码用于验证
print("蚊子");
[root@localhost test]# git add test.py 
[root@localhost test]# git commit -m "second"
[master 4ff75f0] second
 1 file changed, 1 insertion(+)
 [root@localhost test]# git log #查看git日志
 
 [root@localhost test]# git reset --hard  028602e28f8c78b1d4bf822e8db7bf8c1c95d7d5
HEAD 现在位于 028602e first
[root@localhost test]# cat test.py 
#!/usr/bin/python
print("蚊子 小奥奥");
print("蚊子 果果牛");

3、删除本地仓库的文件

[root@localhost test]# git rm test.py 
rm 'test.py'
[root@localhost test]# git commit -m "delete"
[master 71affa7] delete
 1 file changed, 3 deletions(-)
 delete mode 100644 test.py
[root@localhost test]# ls

二,远程仓库

https://github.com 注册账号

创建存储库:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1、本地仓库和远程仓库进行相连

[root@localhost test]# ssh-keygen #免密登录,直接四次回车即可
[root@localhost test]# cat /root/.ssh/id_rsa.pub  #查看生成的密钥
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyBxdelPAGJVA+y9F/lGyT8p7L2r2mR1h9V4eZGsEXICxgkz1ImJYmDDu52kgtPkxEFYtf2kG6gjCVF8Llt7J7sHsEz6/GUkATqb3VIZ1bMGgJdn8jcqckJRkdGSH/b3Lm88GQ84BgOdIUBdYqdAi7plbAOonGeFeRN87fqmUg0+NOBTXRGUwZfykU6YhyGNkd1KnR6rMZP04rW3zawYQmPXdDVHN/ppPY8pOzmzX1lxCjH/IwHf0FladpgidNvA5gIFJMrCv6nrZPzF+nwGrSpqfKbSUHUIEAJaexW3z1CubaqeODe/vG2p5UXNIWgnelVOmXFaRvKtuNu7O6UbVT root@localhost

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2、上传代码到github

[root@localhost test]# systemctl stop firewalld
[root@localhost test]# setenforce 0
[root@localhost test]# ssh -T git@github.com  #联网登录
输入yes
[root@localhost test]# vim test.py
#!/usr/bin/python 
print("小奥奥")
[root@localhost test]# git add test.py 
[root@localhost test]# git commit -m "third"
[master ae14c9a] third
 1 file changed, 2 insertions(+)
 create mode 100644 test.py

在这里插入图片描述

[root@localhost test]# git remote add origin git@github.com:xiaoaoao123/renzihua.git
[root@localhost test]# git push -u origin master 

查看github上面是否存在代码
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
3、在github上面下载代码

[root@localhost test]# mkdir /test1
[root@localhost test]# cd /test1/
[root@localhost test1]# ls

复制克隆代码
在这里插入图片描述

[root@localhost test1]# git clone git@github.com:xiaoaoao123/renzihua.git
正克隆到 'renzihua'...
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 8 (delta 0), reused 8 (delta 0), pack-reused 0
接收对象中: 100% (8/8), done.
[root@localhost test1]# ls
renzihua
[root@localhost test1]# cd renzihua/
[root@localhost renzihua]# ls
test.py

进行修改,然后上传

[root@localhost renzihua]# vim test.py
#!/usr/bin/python
print("小奥奥")
print("果果牛")
[root@localhost renzihua]# git add test.py 
[root@localhost renzihua]# git config --global user.name "xixi"  #这个名字可以自定义
[root@localhost renzihua]# git config --global user.email xixi@123.com  #邮箱也是自定义即可
[root@localhost renzihua]# git commit -m "xixi"
[master a0ff5a7] xixi
 1 file changed, 1 insertion(+)
[root@localhost renzihua]# git push origin master #上传
………………
To git@github.com:xiaoaoao123/renzihua.git
   ae14c9a..a0ff5a7  master -> master

4、添加版本标签

[root@localhost renzihua]# pwd
/test1/renzihua
[root@localhost renzihua]# git tag v1.0.1  #这个版本号随意
[root@localhost renzihua]# git push origin v1.0.1
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:xiaoaoao123/renzihua.git
 * [new tag]         v1.0.1 -> v1.0.1
[root@localhost renzihua]# vim test.py 
#!/usr/bin/python
print("小奥奥")
print("果果牛")
print("haha")
[root@localhost renzihua]# git add test.py 
[root@localhost renzihua]# git commit -m "haha"
[master 9851f96] haha
 1 file changed, 1 insertion(+)
[root@localhost renzihua]# git push origin master
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 277 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:xiaoaoao123/renzihua.git
   a0ff5a7..9851f96  master -> master
[root@localhost renzihua]# git tag v1.0.3
[root@localhost renzihua]# git push origin v1.0.3
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:xiaoaoao123/renzihua.git
 * [new tag]         v1.0.3 -> v1.0.3
[root@localhost renzihua]# git tag
v1.0.1
v1.0.3

在这里插入图片描述
在这里插入图片描述
5、分支

[root@localhost renzihua]# git branch
* master
[root@localhost renzihua]# git branch haha  #创建分支的名字,自定义
[root@localhost renzihua]# git branch
  haha
* master

[root@localhost renzihua]# git checkout haha  #切换分支
切换到分支 'haha'
[root@localhost renzihua]# git branch
* haha
  master

[root@localhost renzihua]# vim test.py 
#添加新的内容
#!/usr/bin/python
print("小奥奥")
print("果果牛")
print("haha")
print("sdbsddsf")
[root@localhost renzihua]# git add test.py 
[root@localhost renzihua]# git commit -m "yy"  #在haha的分支中上传
[haha a76c80a] yy
 1 file changed, 1 insertion(+)
[root@localhost renzihua]# git checkout master  #切换到主干,发现没有修改
切换到分支 'master'
[root@localhost renzihua]# cat test.py 
#!/usr/bin/python
print("小奥奥")
print("果果牛")
print("haha")

[root@localhost renzihua]# git merge haha   #合并haha分支
更新 9851f96..a76c80a
Fast-forward
 test.py | 1 +
 1 file changed, 1 insertion(+)
[root@localhost renzihua]# cat test.py #查看代码发生修改
#!/usr/bin/python
print("小奥奥")
print("果果牛")
print("haha")
print("sdbsddsf")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值