1、Git基础

1.1、版本管理的演变

  集中式VCS  -> 分布式VCS   (vcs:版本控制系统)

 

  集中式VCS:

  有集中的版本管理服务器

  具备文件版本管理和分支管理能力

  集成效率有明显的提高

  客户端必须时刻和服务器相连

 

  分布式VCS:

  服务端和客户端都有完整的版本库

  脱离服务端,客户端照样可以管理版本

  查看历史和版本比较等多数操作,都不需要访问服务器,比集中式VCS更能提高版本管理效率

 

  学习顺序:Git -> github -> gitlab

1.2、安装Git

   https://git-scm.com/downloads

1.3、使用Git之前需要做的最小配置

 

## 设置name 和 email 地址,出现问题发邮件给你
$ git config --global user.name 'dy201'
$ git config --global user.email 'dy201@163.com'

##local 、global system (缺省 = local)
$ git config --local        对某特定仓库起作用
$ git config --global      对所有仓库起作用
$ git config --system      对系统所有登录的用户起作用 

## 显示 config 的配置,加 --list 
$ git config --list --local    
$ git config --list --global      
$ git config --list --system      

 

1.4、创建第一个仓库并配置local用户信息

 

 建Git仓库

  两种场景:

  1、  把已有的项目代码纳入Git管理

  $ cd 项目代码所在的文件夹

  $ git init

  2、  新建的项目直接用Git管理(下面举例说明)

  $ cd  某个文件夹

  $ git init you_project #会在当前路径下创建和项目名称同名的文件(隐藏文件夹.git)

  $ cd your_project

  

   文件准备: 

$ cd /data/
k@k-PC MINGW64 /data
$ ll
total 1
-rw-r--r-- 1 k 197121 13 三月   22 23:14 readme

  操作:

##代码存放目录(自己创建)
$ cd /user/dy201/101-GitRunner/

##设置为项目目录
$ git init git_learning
Initialized empty Git repository in D:/Git/user/dy201/101-GitRunner/git_learning/.git/

##进入项目目录,查看结构
k@k-PC MINGW64 /user/dy201/101-GitRunner/git_learning (master)
$ ls -al
total 4
drwxr-xr-x 1 k 197121 0 三月   22 23:09 ./
drwxr-xr-x 1 k 197121 0 三月   22 23:09 ../
drwxr-xr-x 1 k 197121 0 三月   22 23:09 .git/

##在项目目录中放入数据
$ cp /data/readme  .

##将数据加入git
$ git add readme
warning: LF will be replaced by CRLF in readme.
The file will have its original line endings in your working directory

##查看数据状态
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   readme

##提交数据
$ git commit -m "add readme"
[master (root-commit) 226b8bf] add readme
 1 file changed, 2 insertions(+)
 create mode 100644 readme

##查看git日志
$ git log
commit 226b8bf13841275f3404945a3853f1b4d1adc28c (HEAD -> master)
Author: k <15757388@163.com>
Date:   Fri Mar 22 23:21:29 2019 +0800

    add readme

 

  使用时,local 优先级比 global 高  

 

1.5、通过几次commit来认识工作区和暂存区

步骤(案例在下面):

  - 1、将项目中的文件放入暂存区

    git add "文件名"

       - 2、查看文件的添加状态

    git status

##代码没有add时的状态

$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        index.html

nothing added to commit but untracked files present (use "git add" to track)

##代码成功add后的状态

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   index.html

       - 3、将add成功的代码进行提交

    git -commit m "添加的理由"

  - 4、修改已经提交的代码

    ## 修改操作

  - 5、查看修改后的状态

    ## 修改后的状态的 modify

  - 6、修改的代码全部加入缓存区

    git add -u   // -u表示 update

  - 7、全部提交

       git commit -m "迭代理由"

  - 8、查看提交日志信息

     git log

 

案例:

##回到我们的项目路径
/user/dy201/101-GitRunner/git_learning (master)

##在路径中放入 index.html
k@k-PC MINGW64 /user/dy201/101-GitRunner/git_learning (master)
$ cp /data/index.html  .

##查看文件状态
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        index.html

nothing added to commit but untracked files present (use "git add" to track)

##加入到缓存区
$ git add index.html images/
warning: LF will be replaced by CRLF in index.html.
The file will have its original line endings in your working directory

##再次查看index.html状态
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   index.html

## 提交文件(我这里多 add了一个文件)
$ git commit -m "Add index + logo"
[master f7c30fa] Add index + logo
 2 files changed, 2 insertions(+)
 create mode 100644 images/git-logo.png
 create mode 100644 index.html

## 同理提交了多了文件后,查看日志
$ git log
commit 8b16af839848fa063920a9d9d4e9550a57f19c25 (HEAD -> master)
Author: = <dy201@163.com>
Date:   Sat Mar 23 11:26:40 2019 +0800

    add style.css

commit f7c30fa886d41c6c13010b45a3544e15b5fc930b
Author: dy201 <dy201@163.com>
Date:   Sat Mar 23 11:21:01 2019 +0800

    Add index + logo

commit 226b8bf13841275f3404945a3853f1b4d1adc28c
Author: k <dy201@163.com>
Date:   Fri Mar 22 23:21:29 2019 +0800

    add readme

##对提交的代码进行修改
$ vi index.html

##查看被修改文件的状态(修改后为modify)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")

##快捷命令add所有修改代码(  -u 表示 update)
$ git add -u
warning: LF will be replaced by CRLF in index.html.
The file will have its original line endings in your working directory

##查看add修改代码的状态
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   index.html

##提交修改代码
$ git commit -m 'second time vim'
[master e722d48] second time vim
 1 file changed, 2 insertions(+)

##查看git提交日志
$ git log
commit e722d48b53663e4a915306ba6cc4eee6b99a168c (HEAD -> master)
Author: = <dy201@163.com>
Date:   Sat Mar 23 11:33:31 2019 +0800

    second time vim

commit 6cb9d1bc3df731bc491040f90fbbd9a0524ed60d
Author: = <dy201@163.com>
Date:   Sat Mar 23 11:29:47 2019 +0800

    add js

commit 8b16af839848fa063920a9d9d4e9550a57f19c25
Author: = <dy201@163.com>
Date:   Sat Mar 23 11:26:40 2019 +0800

    add style.css

commit f7c30fa886d41c6c13010b45a3544e15b5fc930b
Author: dy201 <dy201@163.com>
Date:   Sat Mar 23 11:21:01 2019 +0800

    Add index + logo

 

1.6、给文件重命名的简便方法

  原始方法

##将 readme 文件重命名为 readme.md
$ mv readme readme.md

##查看git 状态
$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    readme

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        readme.md

no changes added to commit (use "git add" and/or "git commit -a")

##git add ream.md文件
$ git add readme.md

warning: LF will be replaced by CRLF in readme.md.
The file will have its original line endings in your working directory

##再次查看git状态
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   readme.md

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    readme

## 按照提示删除readme
$ git rm readme
rm 'readme'

##查看git状态
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        renamed:    readme -> readme.md

 

  快速方法

## 重置上面的操作,将readme.md 恢复到 readme
$ git reset --hard
HEAD is now at e722d48 second time vim

##查看git状态
$ git status
On branch master
nothing to commit, working tree clean

##执行git重命名方法
$ git mv readme readme.md

##查看git状态
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        renamed:    readme -> readme.md

##提交代码
$ git commit -m " let readme to readme.md"
[master 3966a67]  let readme to readme.md
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename readme => readme.md (100%)

##最后查看提交历史
$ git log
  // 太长了,就不展示了

 

1.7、通过git log 查看版本演变历史

##之前的git log命令显示太具体了,现在用一行简要来显示
$ git log --oneline
3966a67 (HEAD -> master)  readme -> readme.md
e722d48 second time vim
6cb9d1b add js
8b16af8 add style.css
f7c30fa Add index + logo
226b8bf add readme

## 可以加上参数,只显示具体的条数(最近更改的信息)
$ git log -n2 --oneline
3966a67 (HEAD -> master)  readme -> readme.md
e722d48 second time vim

##查看分支
$ git branch -v
* master 3966a67  readme -> readme.md

##添加分支
$ git checkout -b temp 3966a6773a812ecdd
Switched to a new branch 'temp'

##再次查看分支
$ git branch -av
  master 3966a67  readme -> readme.md
* temp   ff49d0f add test

##图形界面展示git历史
$ git log --all --graph

##查看所有更改简要
$ git log --oneline --all  (-n+数字,表示显示多少条)

##查看所有更改简要(图像化展示)
$ git log --oneline --all  --graph

##查看master分支历史信息
$ git log --oneline master
3966a67 (master)  readme -> readme.md
e722d48 second time vim
6cb9d1b add js
8b16af8 add style.css
f7c30fa Add index + logo
226b8bf add readme

 

1.8、Gitk:通过图形界面工具来查看版本历史

##输入 gitk
$ gitk

跳出弹窗

 

1.9、探秘git目录

 

##HEAD是放分支branch的
$ cat HEAD     // 显示当前分支
ref: refs/heads/temp

##查看项目分支(*表示目前所在分支)
$ git branch -av
  master 3966a67  readme -> readme.md
* temp   ff49d0f add test

## 进入master分支
$ git checkout master
Switched to branch 'master'

##config是显示配置的,可以使用vi手动编辑
$ cat config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[user]
        name = dy201
        email = dy201@163.com

## refs文件里面放的是 heads分支和tag标签
$ cd refs/
$ ll
drwxr-xr-x 1 k 197121 0 3月  24 10:49 heads/
drwxr-xr-x 1 k 197121 0 3月  22 23:09 tags/

##可以分别进入 refs 的 heads、tags目录查看分支和标签情况
$ cat heads/master
3966a6773a812ecdd8ce93b26b68340341731538

##  cat-file  -t表示类型   -p 表示内容
$ git cat-file -t 3966a6773a81
commit

##查看.git/object文件夹,里面存放了blob tree commit
drwxr-xr-x 1 k 197121 0 3月  24 10:49 ./
drwxr-xr-x 1 k 197121 0 3月  24 15:08 ../
drwxr-xr-x 1 k 197121 0 3月  22 23:21 22/
drwxr-xr-x 1 k 197121 0 3月  24 10:49 30/
drwxr-xr-x 1 k 197121 0 3月  24 10:16 39/
drwxr-xr-x 1 k 197121 0 3月  22 23:16 4b/
drwxr-xr-x 1 k 197121 0 3月  22 23:16 4f/
drwxr-xr-x 1 k 197121 0 3月  23 11:26 56/
drwxr-xr-x 1 k 197121 0 3月  23 11:13 58/
drwxr-xr-x 1 k 197121 0 3月  23 11:21 65/
drwxr-xr-x 1 k 197121 0 3月  23 11:29 6c/
drwxr-xr-x 1 k 197121 0 3月  24 10:16 75/
drwxr-xr-x 1 k 197121 0 3月  22 23:21 77/
drwxr-xr-x 1 k 197121 0 3月  23 11:26 8a/
drwxr-xr-x 1 k 197121 0 3月  23 11:29 8b/
drwxr-xr-x 1 k 197121 0 3月  23 11:29 96/
drwxr-xr-x 1 k 197121 0 3月  23 11:29 9b/
drwxr-xr-x 1 k 197121 0 3月  24 10:49 9d/
drwxr-xr-x 1 k 197121 0 3月  23 11:26 c3/
drwxr-xr-x 1 k 197121 0 3月  23 11:33 cf/
drwxr-xr-x 1 k 197121 0 3月  23 11:33 e7/
drwxr-xr-x 1 k 197121 0 3月  23 11:21 f7/
drwxr-xr-x 1 k 197121 0 3月  23 11:32 f8/
drwxr-xr-x 1 k 197121 0 3月  24 10:49 ff/
drwxr-xr-x 1 k 197121 0 3月  22 23:09 info/
drwxr-xr-x 1 k 197121 0 3月  22 23:09 pack/

 

1.10、commit、tree和blob三个对象之间的关系

## 总结
##所有的代码都在一个总的tree下面 ##tree 下面的文件称为blob、文件夹称为tree ## 一个文件称为blob ##
1、使用git log 查出所有的提交历史 $ git log commit 3966a6773a812ecdd8ce93b26b68340341731538 (HEAD -> master) (查出commit的值) Author: = <dy201@163.com> Date: Sun Mar 24 10:16:20 2019 +0800 readme -> readme.md ##2、查出了总的tree $ git cat-file -p 3966a6773a812ecd根据 查出commit的值,找出总的tree,前缀匹配即可) tree 75494b8335b6f4f90d0722db4844189da813fae9 // 总的tree的值 parent e722d48b53663e4a915306ba6cc4eee6b99a168c author = <dy201@163.com> 1553393780 +0800 committer = <dy201@163.com> 1553393780 +0800 readme -> readme.md ##3、查看总的tree的目录结构 $ git cat-file -p 75494b8335b6f4f90d0722db // 根据总的tree的值查看目录结构(前缀匹配即可) 040000 tree 65db96442b5767e40db2bf894134cbae00fa4050 images 100644 blob f8b0da4d1d7478b81fe326b17bd7e406bcd5320e index.html 040000 tree 96d78fd2ba470a48267915fc82e03f5879812630 js 100644 blob 4fa0f399aedbcbbe66250214c559da669dbf8f01 readme.md 040000 tree c3e52b87f853824625a94cd5b7e9d666792d2d10 styles

 

1.11、小练习:数一数tree的个数

##新建一个仓库
$ git init guessHowMuch
Initialized empty Git repository in D:/Git/user/dy201/101-GitRunner/guessHowMuch/.git/

##objects为空
$ find .git/objects -type f

##在仓库里add一个文件夹,文件夹下放了一个文件
$ git add test
warning: LF will be replaced by CRLF in test/hello.txt.
The file will have its original line endings in your working directory

##再次查看objects状态
$ find .git/objects -type f
.git/objects/d9/158aee2d315593aa118ad47d04f0fd47fc9c61

##提交后查看objects状态$ find .git/objects -type f
.git/objects/4d/ead74182122a955876eca726c0ff9f81409440
.git/objects/56/41d97e632a0b4904ebd937984854ddeff441c0
.git/objects/d9/158aee2d315593aa118ad47d04f0fd47fc9c61
.git/objects/fb/a317667c85dcbd4bc546c2e67ac5f7fb155b71

## 分别查看对应的 内容和属性
git cat-file -t
git cat-file -p

 

1.12、分离头指针情况下的注意事项

## git指向某一个commit的时候,处于头指针分离状态
$ git checkout 8b16af839848fa
Note: checking out '8b16af839848fa'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 8b16af8 add style.css

##提示头指针分离
$ git status
HEAD detached at 8b16af8
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   styles/style.css

no changes added to commit (use "git add" and/or "git commit -a")

##使用gitk发现没有这个分支,一段时间会被系统清理
$ gitk

##切换到其它分支时,会收到提醒
$ git checkout master
Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  c412e25 Backgroud: orange -> green

If you want to keep it by creating a new branch, this may be a good time
to do so with:

 git branch <new-branch-name> c412e25

Switched to branch 'master'

##新建分支css-fix,放入其中
$ git branch css-fix c412e25

 

1.13、进一步理解HEAD和branch

##创建并进入分支
$ git checkout -b fix_readme css-fix
Switched to a new branch 'fix_readme'

##HEAD不仅仅指新分支的最后一次提交
##同时还可以不跟分支挂钩处于分离头状态,不跟任何分支挂钩
$ cat .git/HEAD
ref: refs/heads/fix_readme

##分支最后也是落脚到commit的
$ cat .git/refs/heads/fix_readme
c412e25118f7164f082717824f5d790d6b3bd8c2

##比较两个commit的差异
$ git diff HEAD HEAD^1
diff --git a/styles/style.css b/styles/style.css
index 2127c2d..561df99 100644
--- a/styles/style.css
+++ b/styles/style.css
@@ -1,2 +1 @@
 this is style.css
-backgroud: oranger -> green

 

posted @ 2019-03-23 00:07  老L头  阅读(229)  评论(0编辑  收藏  举报