环境:CenoOS7
深刻理解git ,使用命令行操做html
$ sudo yum install git -y $ git --version
####深刻解析Git版本控制python
在进行开发以前咱们须要对git进行简单配置,这样便于管理代码,知道是那我的提交的。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修改和直接使用命令修改是同样的效果github
$ cat ~/.gitconfig [user] name = wutengbiao email = wutengbiao@sina.com [color] ui = true
一.建立本地库repository
1.进入你须要进行本地化仓库的目录ide
$ cd /home/wtb/github/project $ git init Initialized empty Git repository in /home/wtb/github/project/.git/
以上这样咱们就建立了一个空的Repository测试
二.克隆远程库ui
$ git clone https://github.com/kennethreitz/requests.git
####Day02idea
三.添加文件 和提交文件 添加文件命令行
$ 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 $ $
==================================================================
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 $ $
分支的合并 描述如图:
如今如何把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: 查看远程库的名称