分布式版本控制系统-git

Git是目前世界上最早进的分布式版本控制系统git

SVN是集中式的版本控制系统,而Git是分布式版本控制系统,集中式和分布式版本控制系统有什么区别呢?这个能够找度娘......分布式

1.安装Gitide

yum install git

查看git版本ui

git --version

2.建立git本地用户名和邮箱.spa

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.建立版本库:code

版本库又名仓库,英文名repository,你能够简单理解成一个目录,这个目录里面的全部文件均可以被Git管理起来,每一个文件的修改、删除,Git都能跟踪,以便任什么时候刻均可以追踪历史,或者在未来某个时刻能够“还原”。
1.> 建立目录.blog

[root@localhost ~]# mkdir oldman
[root@localhost ~]# cd oldman/
[root@localhost oldman]# ll
total 0
[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 12
drwxr-xr-x   3 root root 4096 Jan 23 13:59 .
dr-xr-x---. 26 root root 4096 Jan 23 13:58 ..
drwxr-xr-x   7 root root 4096 Jan 23 13:59 .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, 1 insertions(+), 0 deletions(-)
 create mode 100644 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 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1,2 @@
 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
 1 files changed, 1 insertions(+), 0 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 23 15:08:41 2017 +0800

    the 3th commit

commit 6dd3e4d624d28ded804d95c151af96cca51a7184
Author: Sanerii <ylemail2002@sina.cn>
Date:   Mon Jan 23 15:07:54 2017 +0800

    the 2td commit

commit 9fd15c47471eb23732bcbd0ad5926dc53b2fce98
Author: Sanerii <ylemail2002@sina.cn>
Date:   Mon Jan 23 14:04:22 2017 +0800

    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~100。 [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@{
0}: HEAD^: updating HEAD da0125f HEAD@{1}: commit: the 3th commit 6dd3e4d HEAD@{2}: commit: the 2td commit 6dd3e4d HEAD@{0}: 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 commitgit 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 

相关文章
相关标签/搜索