CentOS7平台上Git的安装使用
环境:CenoOS7
深入理解git ,使用命令行操作
Git的安装
$ sudo yum install git -y
$ git --version
####深入解析Git版本控制
Day01
在进行开发之前我们需要对git进行简单配置,这样便于管理代码,知道是那个人提交的。
$ git config --global user.name wutengbiao
$ git config --global user.email wutengbiao@sina.com
$ git config --global color.ui true #设置GIT的打印颜色
实际上我们 上面的操作,使用git config --global命令 就是对.gitconfig 进行的操作,所以可以直接打开.gitconfig修改和直接使用命令修改是一样的效果
$ cat ~/.gitconfig
[user]
name = wutengbiao
email = wutengbiao@sina.com
[color]
ui = true
一.创建本地库repository
1.进入你需要进行本地化仓库的目录
$ cd /home/wtb/github/project
$ git init
Initialized empty Git repository in /home/wtb/github/project/.git/
以上这样我们就创建了一个空的Repository
二.克隆远程库
$ git clone https://github.com/kennethreitz/requests.git
####Day02
三.添加文件 和提交文件 添加文件
$ git status #查看状态
[root@bogon porject]# git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
$
$
$ vi code.py
# -*- coding:UTF-8 -*-
print "hello world "
print "hello world "
print "hi git "
$
$ git add code.py #添加文件
$ git status
$
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: code.py
#
[root@bogon porject]#
四.提交文件
$ git commit -m 'init commit' 不加-m 会打开vi 信息
[root@bogon porject]# git commit -m 'init commit'
[master (root-commit) 5f2078a] init commit
1 file changed, 6 insertions(+)
create mode 100644 code.py
[root@bogon porject]#
$ git status #查看状态
$ git status
# On branch master
nothing to commit, working directory clean
[root@bogon porject]#
[root@bogon porject]# git status -s
[root@bogon porject]# 没有任何东西了
五.Git 工作流程图

1.我先对code.py文件进行了修改,添加了一行信息,进行查看git status -s ;后面出现一个M. 2.现在把code.py git add 到了staging erea区域,进行查看git status -s ;最前面出现一个M出现. 3.现在我再次对code.py进行了修改保存,然后执行git status -s 就可以看到两个MM
vi code.py
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
print "hello world "
print "hello world "
print "hi git "
print "hi git " //添加一行
$
$ git status -s //查看
M code.py
$
$ git add code.py //add到 staging erea区域
$
现在再次修改code.py文件,然后保存 //在staging erea区域进行了修改
$
$ git status -s
MM code.py
$
$
注意:
第一个M 表示在staging area区域做了变化
第二个M表示working Directory区域做了变化
现在我执行 git add code.py .从working directory ->添加到 staging area区域. 这个时候 在执行git status -s 命令
$ git status -s
M code.py
$
现在只剩下前面的一个M了
$ git commit -m 'updata'
[master 4df82c2] updata
1 file changed, 2 insertions(+)
$ git status -s
$
到这来就表示三个文件多完全相同了.
####Day03
1.查看working directory区域与 staging area区域 发生什么变化 我再次修改了code.py文件,并保存之后查看
$ git status -s #查看
M list.py
$
上面看到的就是后面有一个M了,我们working directory发生了变化,可以使用git diff查看文件发生了什么变化
$ git diff #查看究竟发生了什么变化
$ git add list.py #add文件到staging area区域
$ git status -s #查看
M list.py
$
上面看到的就是排在第一的M了,可以使用git diff查看文件发生了什么变化
$ git diff
$
这时看不到任何变化了,表示 staging area区域 working Directory区域 完全相同
2,查看staging area区域与history区域 发生什么变化
$ git diff --staged
$
3.查看 workingDirectory 区域与 history区域 发生什么变化
$ git diff HEAD
$
$ git diff --stat HEAD #查看
list.py | 2 +- #修改了2个地方,
1 file changed, 1 insertion(+), 1 deletion(-) 删除一行,增加一行
$
我现在对code.py进行了修改,然后查看
$ git diff --stat #比较
$ git status -s
MM list.py
$
$如果我现在进行 git commit -m 'new update'
$ git commit -m 'new update'
[master 08356f2] new update
1 file changed, 1 insertion(+), 1 deletion(-)
$
$ git status -s
M list.py 第一个M清空了,第二个M还在
$
$ git diff HEAD
$ git diff --staged
$
描述:
因为我在code.py添加了1行,执行:git status -s 就会看到两个MM, 但是我没有把他add 到staging area区域
,而直接执行了git commit -m 'new upate' 提交到了History区域, 这个时候working directory新添加的
1行并不会直接把他放到history之前的code.py 里面去,而是在history里面产生新的code.py,
图形分析:

####Day04
如何撤销一个文件
如果一个文件git add 到了stoging area区域,如何撤销文件.
$ git status -s
M list.py
$
$ git diff
$ git add code.py #把code.py放到了staging erea里面
$
$ git status -s
M code.py
$
====现在我想撤销这个操作使用git reset命令======
$ git reset code.py
Unstaged changes after reset:
M code.py #意思是说从history区域取出code.py 覆盖到,stoging area区域里面的code.py
$
$ git status -s #查看
M list.py #现在又回到了原来的状态
$
如果我想把stoging area区域里面的code.py取出来覆盖到working directory区域的code.py 如何操作:
$ git status
# On branch master
nothing to commit, working directory clean
$ cat list.py
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
print 'hi git '
print 'hi hub'
$
$
现在我又修改了code.py这个文件
$ vi list.py
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
print 'hi git '
print 'hi hub'
print 'one'
print 'two'
print 'three'
$
$ git checkout code.py
$ cat code.py
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
print 'hi git '
print 'hi hub'
$
现在又回到了 原来状态.
如果我想把history区域里面的code.py取出来覆盖到working directory区域的code.py 如何操作:
比方说我现在又修改了一下文件
$ vi code.py
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
print 'hello world'
print 'hello world'
print 'hi git'
print 'hi git' //添加四行
print 'hi git'
print 'hi git'
print 'hi git'
现在我们来操作 ,history区域里面的code.py取出来覆盖到working directory区域的code.py
$ git checkout HEAD code.py
$ cat code.py
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
print 'hello world'
print 'hello world'
print 'hi git'
现在又回到了 原来状态.
==============================================================
如何从working directory区域 直接提交到history区域:
比如我现在对代码最了一点修改
$ vi list.py
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
print 'hello world'
print 'hello world'
print 'hi git'
print 'welcome to git ' //添加一行
$
$ git status -s
M list.py //表示working directory 区域做了修改
$
$ git commit -am 'add new code' //
[master beedb07] add new code
1 file changed, 3 insertions(+)
$ git status -s
$
表示我们已经提交成功
####Day05
如何在git 中删除文件
$ ls
code.py old.py README.txt // 比如我在history区域添加了old.py
$
$ git rm old.py // 现在删除old.py
rm 'old.py'
$
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: old.py
#
$ git status -s
D old.py
$
$ git commit -m 'delete old.py'
[master 5642c5d] delete old.py
1 file changed, 17 deletions(-)
delete mode 100644 old.py
$
$ ls
code.py README.txt
$
比如我想删除 stoging area区域 里面的code.py,想保留working directory区域的code.py如何操作:
$
$ git rm --cached code.py
rm 'code.py'
$
$ ls
code.py README.txt
$
$ git status -s
D code.py
?? code.py
如果我们现在想撤销上面这个操作,如何操作呢:
$ git reset code.py //从history里面吧code.py 覆盖到staging erea里面
$ git status -s
$
git 里面如何对对文件重命名,如何操作:
$
$ git mv README.txt README.md
$
$ ls
code.py README.md
$ git status -s
R README.txt -> README.md
$
$ git commit -m 'rename README' //提交
$
$
####day06
在开发的过程中可能遇到很多的突发事件,比如我现在修改了code.py文件
$ vi list.py
$ vi list.py
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
print 'hello world'
print 'hello world'
print 'hi git'
print 'git study' //添加了两行
print 'git version'
$
$ git status -s
M list.py //working directory 有修改
$
现在我又修改了 README.txt 文件
$ vi README.txt
abcdefghijkmn
$
$ git status -s
M code.py
M README.txt
$
我现在对文件进行code.py add 到staging erea区域
$ git add code.py
$ git status -s
MM code.py
M README.txt
$
$
如果在开发过程中出现 做了很多复杂的修改,现在代码改到一半,没法提交,没有测试,但是之前代码又出现了一个问题,需要紧急修改,如上问题处理
$ vi code.py
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
print 'hello world'
print 'hello world'
print 'hi git'
print 'git study' //添加了两行
print 'git version'
$
$
我们在这样的情况下就可以使用git stash 来操作
$ git stash
Saved working directory and index state WIP on master: 21b9a6c rename code.py
HEAD is now at 21b9a6c rename code.py
$
$ git status
# On branch master
nothing to commit, working directory clean
$
刚才修改了很多东西,现在又恢复到了原始状态
$ vi code.py
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
print 'hello world'
print 'hello world'
print 'hi git'
$
这里我们就可以吧需要紧急处理的代码加进去,比如我这里 少了两个感叹号
$ vi code.py
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
print 'hello world !' //加上!号
print 'hello world !'
print 'hi git'
$
$
$ git commit -am 'quick fix'
[master 7bfbb97] quick fix
1 file changed, 3 insertions(+), 1 deletion(-)
$
$
现在 修改完了之后,我需要把之前的代码都拿处理,相当于把之前收拾代码的抽屉打开,
$ git stash list //先查看
$
$ git stash pop
$
$ git status -s //查看
$
$vi code.py
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
print 'hello world !'
print 'hello world !'
print 'hi git'
print 'great ,git '
print 'git is fun'
$
$
$git commit -am 'update 2 files' //提交
================================================================
####day07
history区分析
$ ls
code.py REDAME.txt docs //增加了docs
$
$ tree.
├── code.py
├── docs
│ ├── code.html
│ └── index.html
└── README.txt
1 directory, 4 files
$
$ git log 查看日志,可以看到commit的编号
$

$
$ git cat-file -t HEAD //打印HEAD的指向
commit //表示指向了commit
$
$ git cat-file -p HEAD //打印HEAD的内容
tree 34ae6325e61788f961bc966d769d898a194a9bab
parent 7bfbb97b720d85043c8475a3cdd559f46ee01e03
author wutengbiao <wutengbiao@sina.com> 1476977118 -0700
committer wutengbiao <wutengbiao@sina.com> 1476977118 -0700
add docs
$ git cat-file -t 34ae632
tree //指向tree
$
$
$ git cat-file -p 34ae63
100644 blob d5e1e2a31e03b64702fc7017bad1495c370c73ab code.py
100644 blob 23543ec40e22d3b460ce3133f91340b0d585322a README.txt
100644 blob dfsdfdfec40e22d3b460ce3133f91340b0d5853gfd docs
$
$ git cat-file -p dfsdfdf
100644 blob12we34ed31e03b64702fc7017bad1495c370c73ab code.html
100644 blob 4r56r22d3b460ce3133f91340b0d585322a index.html
$
$
==================================================================
day08
tree-ish 定位对象
$ ls -A //查看隐藏文件
$ cd .git/
$
$ ll
total 32
drwxr-xr-x. 2 root root 6 Oct 18 07:01 branches
-rw-r--r--. 1 root root 3 Oct 21 08:45 COMMIT_EDITMSG
-rw-r--r--. 1 root root 92 Oct 18 07:01 config
-rw-r--r--. 1 root root 73 Oct 18 07:01 description
-rw-r--r--. 1 root root 23 Oct 18 07:01 HEAD
drwxr-xr-x. 2 root root 4096 Oct 18 07:01 hooks
-rw-r--r--. 1 root root 367 Oct 21 08:45 index
drwxr-xr-x. 2 root root 20 Oct 18 07:01 info
drwxr-xr-x. 3 root root 28 Oct 18 07:43 logs
drwxr-xr-x. 40 root root 4096 Oct 21 07:51 objects
-rw-r--r--. 1 root root 41 Oct 20 08:14 ORIG_HEAD
drwxr-xr-x. 4 root root 41 Oct 20 08:14 refs
$
查看 HEAD文件
$ cat HEAD
ref: refs/heads/master
$
$ tree refs/
refs/
├── heads
│ └── master
├── stash
└── tags
2 directories, 2 files
$
$ cat refs/heads/master
b9ea1014735aae7cd0821df77e65627b7fb23af9 //哈希码
$
$
$ git cat-file -t b9ea1014735aae7cd0821df77e65627b7fb23af9
commit
$
$ git log --oneline //打印git commit 的 日志信息
beedb07 add docs
7bfbb97 quick fix
5642c5d delete old.py
11660c4 new upate
所以我们这个master的哈希码就是指向beedb07 这个哈希值
$
$
$ git rev-parse HEAD //表示指向HEAD的哈希码
b9ea1014735aae7cd0821df77e65627b7fb23af9
$
$ git rev-parse HEAD~ //表示指向第一个HEAD
$
$ git rev-parse HEAD~4 //表示指向第四个HEAD
$
$
$git rev-parse HEAD~4^{tree} //打印tree的哈希码
dfdfwe43735aae7cd0821df77e65627bdfdfs
$
$ git cat-file -p dfdfwe43735aae7cd0821df77e65627bdfdfs //打印到内容
$
$
$ git cat-file -p HEAD~4:code.py
或者
$ git show HEAD~4:code.py
####Day09 branch 分支
$ git branch //列出所以的brach
$ * master
$git branch tryideal //创建一个名为tryideal的brach
$
$git branch
* master
tryideal
表示当前我们在 master branch 上.
//切换branch分支
$ git checkout tryideal
Switched to branch tryideal
$
$ git branch
master
* tryideal //切换到了tryideal branch
//我们来验证 一下是否切换到了分支tryideal分支上
$ cd .git/
$cd refs/heads
$ ls
master tryideal
$
$cat *
b9ea1014735aae7cd0821df77e65627b7fb23af9
b9ea1014735aae7cd0821df77e65627b7fb23af9
打印出来的信息完全相同
$ cd ../
$ cat HEAD
ref: refs/heads/tryideal
现在已经切换到了tryideal分支上
如图:

如果现在我们又想切回到master分支上
$ git checkout master //现在又切换到了master分支上
$
$cat .git/HEAD //查看
ref: refs/heads/master
我们如何删除分支:
$ git branch -d tryideal //删除分支
Delete branch tryideal
$
$ git branch
* master
$
$ ls
如何使用更简便的方法进行创建分支和切换分支:
$ git branch
* master
$ git checkout -b tryideal //快速创建分支切换分支
Switched to a new branch 'tryideal'
$
$ git branch
master
* tryideal
现在我修改一下code.py代码
$ vi code.py
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
print 'hello world !'
print 'hello world !'
print 'hi git'
print " new ideal"
$
$ git commit -am 'new idea'
$
现在我想切换回到mastrer分支
$ git checkout master //切到master分支
Switched to branch 'master'
$
$git branch
* master
tryideal
$
$
$ git checkout -b tryideal //这样删除 会报错 ,不允许删除
$
$ git merge tryideal //把tryidel新功能合并到master上面
$
现在就可以直接删除 tryideal 分支
$ git branch -d tryidea
delete branch tryideal
$
$ git branch
* master
$
$
Day09
分支的合并 描述如图:

现在如何把bugfix分支合并到master里面去
$git branch
* master
$
$ git chechout -b bugfix //创建bugfix
$ vi code.py //第一次修改
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
print 'hello world ' //修改!号
print 'hello world ' //修改!号
print 'hi git'
print 'great git '
print 'git is fun'
print " new ideal"
$ git commit -am 'fix 2 bugs'
$
$ vi code.py //第二次修改
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
print 'hello world '
print 'hello world '
print 'hi git ! ! !' //修改
print 'great git '
print 'git is fun'
print " new ideal"
$
$ git commit -am 'fix 1 bugs' //提交
$
// 切换到master分支上
$
$
$ git chechout master
$
$vi code.py
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
print 'hello world ! '
print 'hello world ! '
print 'hi git'
print 'great git '
print 'git is fun'
print " new ideal"
print 'yet another new feature' // master分支上新加的内容
$
$ git commit -am " updata"
$
现在就需要把 bugfix 分支的代码合并到master分支里面
$
$ git merge bugfix //
$
$ git log //可以看到新创建的一个新的commit,我们的 bugfix修改的代码就合并到了里面
$ git branch
bugfix
* master
$
$
现在就可以把bugfix 这个分支删除.
$ git branch -d bugfix //删除
$
图形解析如下:

1.为了进行合并,先找到他们共同的部分c ,然后是e ,f
2.在创建g之前他先用c和f进行比较,比较完了之后创建一个pach
3.产生的pach 会应用到e上面,然后在产生g (three way merge)
常用的查看命令:
git branch: 查看分支信息。检查自己正工作在哪一个分支
git branch –a: 查看本地和远程的所有分支
git remote –v: 查看当前关联的远程库及对应的名称
git log: 查看提交的log信息
git status: 查看当前状态
git remote show: 查看远程库的名称
来源:oschina
链接:https://my.oschina.net/u/2491034/blog/761983