分布式版本控制系统-git
Git是目前世界上最先进的分布式版本控制系统
SVN是集中式的版本控制系统,而Git是分布式版本控制系统,集中式和分布式版本控制系统有什么区别呢?这个可以找度娘......
1.安装Git
yum install git
查看git版本
git --version
2.创建git本地用户名和邮箱.
git config --global user.name "Sanerii"
git config --global user.email ylemail2002@sina.cn
查看git配置.
[root@localhost ~]# git config --list
user.name=Sanerii
user.email=ylemail2002@sina.cn
给git配置颜色.
git config --global color.ui true
3.创建版本库:
版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改、删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻可以“还原”。
1.> 创建目录.
[root@localhost ~]# mkdir oldman
[root@localhost ~]# cd oldman/
[root@localhost oldman]# ll
total
[root@localhost oldman]#
2.>通过git init命令把这个目录变成Git可以管理的仓库:
[root@localhost oldman]# git init
Initialized empty Git repository in /root/oldman/.git/
[root@localhost oldman]# ls -la
total
drwxr-xr-x root root Jan : .
dr-xr-x---. root root Jan : ..
drwxr-xr-x root root Jan : .git
#瞬间Git就把仓库建好了,而且告诉你是一个空的仓库(empty Git repository)
3.>编写文件提交到git,并运行git status命令看看结果:
添加文件到Git仓库,分两步:
第一步,使用命令git add <file>,将文件添加到暂存区,注意,可反复多次使用,添加多个文件;<文件需要存在或提前创建好.>
第二步,使用命令git commit,完成提交。
git add命令实际上就是把要提交的所有修改放到暂存区(Stage),然后,执行git commit就可以一次性把暂存区的所有修改提交到分支。
[root@localhost oldman]# echo "hello world" >> readme.txt #在git目录写一个文件
[root@localhost oldman]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# readme.txt
nothing added to commit but untracked files present (use "git add" to track)
[root@localhost oldman]#
[root@localhost oldman]# git add readme.txt #添加一个文件到git
[root@localhost oldman]# git status #查看git状态
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: readme.txt
#
[root@localhost oldman]# git commit -m "the first commit" #提交文件
[master (root-commit) 9fd15c4] the first commit
files changed, insertions(+), deletions(-)
create mode readme.txt
[root@localhost oldman]# git status
# On branch master
nothing to commit (working directory clean) [root@localhost oldman]# echo "working..." >> readme.txt #向文件中追加内容
[root@localhost oldman]# git diff readme.txt #使用diff命令查看文件改变内容
diff --git a/readme.txt b/readme.txt
index 3b18e51..5c1acd5
--- a/readme.txt
+++ b/readme.txt
@@ - +, @@
hello world
+working... //为追加的内容
[root@localhost oldman]# git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: readme.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@localhost oldman]# git add readme.txt #添加文件到git
[root@localhost oldman]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: readme.txt
#
[root@localhost oldman]# git commit -m "t2 commit" #提交文件
[master 4f34513] t2 commit
files changed, insertions(+), deletions(-)
[root@localhost oldman]# git status
# On branch master
nothing to commit (working directory clean)
[root@localhost oldman]#
提交文件及查看状态
小结
要随时掌握工作区的状态,使用git status命令。
如果git status告诉你有文件被修改过,用git diff可以查看修改内容。
4.版本回退
命令:git reset --hard commit_id
回退到上一个版本:git reset --hard HEAD^
git log 命令显示从最近到最远的提交日志,如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline参数:
Git提供了一个命令git reflog用来记录你的每一次命令:
[root@localhost oldman]# cat readme.txt #我的文件每一行代表提交了一次
hello world
hello world too
hello world 3too
[root@localhost oldman]# git log #显示从最近到最远的提交日志,我们可以看到3次提交.
commit da0125f6d4f02a96a423e4d19290db9db6fc70b0
Author: Sanerii <ylemail2002@sina.cn>
Date: Mon Jan :: + the 3th commit commit 6dd3e4d624d28ded804d95c151af96cca51a7184
Author: Sanerii <ylemail2002@sina.cn>
Date: Mon Jan :: + the 2td commit commit 9fd15c47471eb23732bcbd0ad5926dc53b2fce98
Author: Sanerii <ylemail2002@sina.cn>
Date: Mon Jan :: + the first commit
[root@localhost oldman]#
[root@localhost oldman]# git log --pretty=oneline #如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline参数:
da0125f6d4f02a96a423e4d19290db9db6fc70b0 the 3th commit
6dd3e4d624d28ded804d95c151af96cca51a7184 the 2td commit
9fd15c47471eb23732bcbd0ad5926dc53b2fce98 the first commit
#在Git中,用HEAD表示当前版本,也就是最新的提交da0125...c70b0(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^,上上一个版本就是HEAD^^,
#当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~。
[root@localhost oldman]# git reset --hard HEAD^
HEAD is now at 6dd3e4d the 2td commit #回退到上一个版本.
[root@localhost oldman]# cat readme.txt
hello world
hello world too
#我们要把当前版本回退到上一个版本,就可以使用git reset --hard HEAD^ 命令: 在Git中,总是有后悔药可以吃的。当你用$ git reset --hard HEAD^回退到the 2td commit版本时,再想恢复到the 3th commit,就必须找到the 3th commit的commit id.
Git提供了一个命令git reflog用来记录你的每一次命令:
[root@localhost oldman]# git reflog
6dd3e4d HEAD@{}: HEAD^: updating HEAD
da0125f HEAD@{}: commit: the 3th commit
6dd3e4d HEAD@{}: commit: the 2td commit
6dd3e4d HEAD@{}: HEAD^: updating HEAD
第二行显示the 3th commit的commit id是da0125f,现在,你又可以回到提交前的状态了。
[root@localhost oldman]# git reset --hard da0125f #使用id恢复到第三次提交的版本.
HEAD is now at da0125f the 3th commit
[root@localhost oldman]# cat readme.txt
hello world
hello world too
hello world 3too
[root@localhost oldman]#
现在总结一下:
HEAD指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使用命令git reset --hard commit_id。
穿梭前,用git log可以查看提交历史,以便确定要回退到哪个版本。
要重返未来,用git reflog查看命令历史,以便确定要回到未来的哪个版本。
5.撤销修改(两种情况)
1.>git checkout -- file可以丢弃工作区的修改:
git checkout -- file
命令git checkout -- readme.txt意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况:
一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
总之,就是让这个文件回到最近一次git commit或git add时的状态。
2.>修改只是添加到了暂存区,还没有提交的撤销.
git reset HEAD file可以把暂存区的修改撤销掉(unstage),重新放回工作区:
git reset HEAD file
git reset命令既可以回退版本,也可以把暂存区的修改回退到工作区。当我们用HEAD时,表示最新的版本。
小结
又到了小结时间。
场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file。
场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD file,就回到了场景1,第二步按场景1操作。
场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退一节,不过前提是没有推送到远程库。
参考文档:
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
分布式版本控制系统-git的更多相关文章
- [.net 面向对象程序设计进阶] (27) 团队开发利器(六)分布式版本控制系统Git——在Visual Studio 2015中使用Git
[.net 面向对象程序设计进阶] (26) 团队开发利器(六)分布式版本控制系统Git——在Visual Studio 2015中使用Git 本篇导读: 接上两篇,继续Git之旅 分布式版本控制系统 ...
- [.net 面向对象程序设计进阶] (26) 团队开发利器(五)分布式版本控制系统Git——图形化Git客户端工具TortoiseGit
[.net 面向对象程序设计进阶] (26) 团队开发利器(五)分布式版本控制系统Git——图形化Git客户端工具TortoiseGit 读前必备: 接上篇: 分布式版本控制系统Git——使用GitS ...
- [.net 面向对象程序设计进阶] (25) 团队开发利器(四)分布式版本控制系统Git——使用GitStack+TortoiseGit 图形界面搭建Git环境
[.net 面向对象程序设计进阶] (25) 团队开发利器(四)分布式版本控制系统Git——使用GitStack+TortoiseGit 图形界面搭建Git环境 本篇导读: 前面介绍了两款代码管理工具 ...
- 分布式版本控制系统 Git 的安装与使用
作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 远端库地址:https://github.com/CJL29 ...
- 分布式版本控制系统Git的安装与使用
分布式版本控制系统Git的安装与使用 作业要求来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2103 我的远端仓库地址是:htt ...
- 【软件工程】分布式版本控制系统Git的安装与使用
作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 远端库地址:https://github.com/Richa ...
- 分布式版本控制系统Git的安装与使用(作业2)
(本次作业要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2103) 分布式版本控制系统Git的安装与使用 一.安装Git b ...
- 分布式版本控制系统Git——使用GitStack+TortoiseGit 图形界面搭建Git环境(服务器端及客户端)(转)
近期想改公司内部的源码管控从TFS为git,发现yubinfeng大侠有关git的超详细大作,现将其转载并记录下,以防忘记,其原博客中有更加详细的git及.net开发相关内容.原文地址:http:// ...
- 分布式版本控制系统 Git 教程
简介 Git 是什么? Git 是一个开源的分布式版本控制系统. 什么是版本控制? 版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统. 什么是分布式版本控制系统? 介绍分布 ...
随机推荐
- JAVA String类型和原型模式
如上例所述,变量a,b和它们的值10,20都是存在栈里面,声明的所以String类型的引用也都是存在栈里.而字符串abc是存在字符串常量池中,new出来的String对象则是存在堆里. String ...
- MySQL——修改数据库远程权限
语句 赋予权限 ON *.*前一个*代表库后一个代表表 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'xxxxx' WITH GRA ...
- xadmin后台 导入 excel 功能拓展
新建 excel 文件 在 xadmin 的 plugins 下添加一个 excel.py # _*_ coding:utf-8 _*_ __author__ = "yangtuo" ...
- kafka 发送确认参数acks的几种模式
1. acks=0 意味着生产者能够通过网络吧消息发送出去,那么就认为消息已成功写入Kafka 一定会丢失一些数据 2. acks=1 意味着首领在疏导消息并把它写到分区数据问津是会返回确认或者错误响 ...
- Linux端口被占用的解决(附Python专版)
先说一般情况的解决: lsof -i:8000 查出PID,然后 kill掉程序,接着就可以了 软件重启之后绑定没有释放,lsof -i:8080也查不出来占用的情况 再来个长连接版Python解决法 ...
- 洛谷P1233 木棍加工题解 LIS
突然发现自己把原来学的LIS都忘完了,正好碰见这一道题.|-_-| \(LIS\),也就是最长上升子序列,也就是序列中元素严格单调递增,这个东西有\(n^{2}\)和\(nlog_{2}n\)两种算法 ...
- openssl命令行将pfx格式转.key和.crt文件,Apache适用
以前使用的windows平台作为php运行的平台,感觉整体速度太慢,于是现在改为centos系统的linux平台.需要把windows中用pfx证书转为appache用到key和crt组成的证书 将* ...
- redis源码解析(1):redisObject对象说明
Redis在实现键值对数据库时,并没有直接使用数据结构,而是基于已有的数据结构创建了一个对象系统,每种对象至少包含一种数据结构. redis3.0 中对象结构: typedef struct redi ...
- python14 1.带参装饰器 | wrapper 了了解 # 2.迭代器 ***** # 可迭代对象 # 迭代器对象 # for迭代器 # 枚举对象
## 复习 '''函数的嵌套定义:在函数内部定义另一个函数 闭包:被嵌套的函数 -- 1.外层通过形参给内层函数传参 -- 2.验证执行 开放封闭原则: 功能可以拓展,但源代码与调用方式都不可以改变 ...
- saltstack主机管理项目:计主机管理项目命令分发器(三)
一.开发目标命令格式如下: 二.目录结构 三.代码注解 01.salt.py 只是一个入口,没干什么事情 #!/usr/bin/env python # -*- coding:utf-8 -*- # ...