码迷,mamicode.com
首页 > 其他好文 > 详细

worktools-git 工具的使用总结(1)

时间:2015-02-13 14:45:51      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

1.仓库创建

zhangshuli@zhangshuli-MS-7817:~$ mkdir myGit
zhangshuli@zhangshuli-MS-7817:~$ cd myGit/
zhangshuli@zhangshuli-MS-7817:~/myGit$ git init
Initialized empty Git repository in /home/zhangshuli/myGit/.git/

2.更改添加

//创建一个新文件
zhangshuli@zhangshuli
-MS-7817:~/myGit$ touch bbb.txt //查看当前目录
zhangshuli@zhangshuli
-MS-7817:~/myGit$ ls aaa.txt bbb.txt //查看仓库状态
zhangshuli@zhangshuli
-MS-7817:~/myGit$ git st
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#      bbb.txt //更改,还没有跟仓库扯上关系
nothing added to commit but untracked files present (use "git add" to track)
//提交到缓存去(工作目录树)
zhangshuli@zhangshuli-MS-7817:~/myGit$ git add .
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#    new file:   aaa.txt
#
 zhangshuli@zhangshuli-MS-7817:~/myGit$

3.仓库修改还原

zhangshuli@zhangshuli-MS-7817:~/myGit$ ls
aaa.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git br -av
* master 9a1e055 aaa.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch master
nothing to commit (working directory clean)
zhangshuli@zhangshuli-MS-7817:~/myGit$ touch bbb.txt ccc.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ ls
aaa.txt  bbb.txt  ccc.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    bbb.txt
#    ccc.txt
nothing added to commit but untracked files present (use "git add" to track)
zhangshuli@zhangshuli-MS-7817:~/myGit$ git add bbb.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    new file:   bbb.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    ccc.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git co ccc.txt
error: pathspec ccc.txt did not match any file(s) known to git.
zhangshuli@zhangshuli-MS-7817:~/myGit$ git checkout ccc.txt
error: pathspec ccc.txt did not match any file(s) known to git.
zhangshuli@zhangshuli-MS-7817:~/myGit$ git clear -df .
git: clear is not a git command. See git --help.

Did you mean this?
    clean
zhangshuli@zhangshuli-MS-7817:~/myGit$ git clean -df
Removing ccc.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    new file:   bbb.txt
#
zhangshuli@zhangshuli-MS-7817:~/myGit$

zhangshuli@zhangshuli-MS-7817:~/myGit$ git clean -df aaa.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    new file:   bbb.txt
#
# 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:   aaa.txt
#
zhangshuli@zhangshuli-MS-7817:~/myGit$ vim aaa.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git co aaa.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ ls
aaa.txt  bbb.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ touch ccc.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ ls
aaa.txt  bbb.txt  ccc.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    new file:   bbb.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    ccc.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git checkout ccc.txt
error: pathspec ‘ccc.txt‘ did not match any file(s) known to git.
zhangshuli@zhangshuli-MS-7817:~/myGit$ git co ccc.txt
error: pathspec ‘ccc.txt‘ did not match any file(s) known to git.
zhangshuli@zhangshuli-MS-7817:~/myGit$ git clean diff .
fatal: clean.requireForce defaults to true and neither -n nor -f given; refusing to clean
zhangshuli@zhangshuli-MS-7817:~/myGit$ git clean -f .
Removing ccc.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$


从上面我们可以得到如下结论

1)git co = git checkout

2)   git co 跟git clean 都是针对目录树的操作,对缓存区没有影响

3)   git co 是针对版本库或者缓存区已经存在的项目进行的操作,它是去除两者的差异,相当于还原

4)    git clean 针对的是目录树存在而版本库或者缓存区不存在的项目

5)两者的区别,其实就是,一个git co分析同一个文件内容差异;git clean分析文件目录差异

另外,cc = git co . | git clean -f .

4.版本库的撤消跟修改

 

zhangshuli@zhangshuli-MS-7817:~/myGit$ ls
aaa.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git br -av
* master 9a1e055 aaa.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch master
nothing to commit (working directory clean)
zhangshuli@zhangshuli-MS-7817:~/myGit$ ls
aaa.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ touch bbb.txt

zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    bbb.txt
nothing added to commit but untracked files present (use "git add" to track)
zhangshuli@zhangshuli-MS-7817:~/myGit$ git add .
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    new file:   bbb.txt
#
zhangshuli@zhangshuli-MS-7817:~/myGit$ git reset .
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    bbb.txt
nothing added to commit but untracked files present (use "git add" to track)
zhangshuli@zhangshuli-MS-7817:~/myGit$ git add .
zhangshuli@zhangshuli-MS-7817:~/myGit$ git commit -m "bbb.txt" -m "add"
[master 2f6533f] bbb.txt
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 bbb.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch master
nothing to commit (working directory clean)
zhangshuli@zhangshuli-MS-7817:~/myGit$ git br -av
* master 2f6533f bbb.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git log
commit 2f6533fe371f8b5a9dfa9c502bc20e22aaaa6177
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date:   Fri Feb 13 11:41:01 2015 +0000

    bbb.txt
    
    add

commit 9a1e05516a0436f46b73c9553795ae22acfb2eee
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date:   Fri Feb 13 10:54:58 2015 +0000

    aaa.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$

对于本地目录树跟缓冲区之间的操作,主要就是有两种

1)目录树->缓冲区:git add

2)缓冲区->目录树:git reset

3)看如下的例子

zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    bbb.txt
nothing added to commit but untracked files present (use "git add" to track)
zhangshuli@zhangshuli-MS-7817:~/myGit$ vim bbb.txt 
zhangshuli@zhangshuli-MS-7817:~/myGit$ git add .
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    new file:   bbb.txt
#
zhangshuli@zhangshuli-MS-7817:~/myGit$ vim bbb.txt 
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    new file:   bbb.txt
#
# 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:   bbb.txt
#
zhangshuli@zhangshuli-MS-7817:~/myGit$ git reset bbb.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    bbb.txt
nothing added to commit but untracked files present (use "git add" to track)
zhangshuli@zhangshuli-MS-7817:~/myGit$ git clean -f .
Removing bbb.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch master
nothing to commit (working directory clean)
zhangshuli@zhangshuli-MS-7817:~/myGit$ 

通过上面的操作,我们还可以再次加深下理解

1)git add :本质上是把一次修改先放到缓存区,如果你目录树中再次对它进行修改并提交,这个会覆盖之前的状态

2)git reset:是把缓存区的内容放回到目录树,但它不是覆盖,而是保持目录树中的当前状态

3)如果你删除了一个缓存区存在的文件,那么你要么可以使用git add . -A把缓存区的状态覆盖掉,要么可以git reset .这时候git reset 文件名 报错,因为找不到这个文件了

4)git commit -m "one" -m "two" ...其实就是注释的换行且中间间隔一行

5)git commit --amend 是在当前的节点上追加内容(当然也可以通过它来给注释重命名).理解错误,看如下实例

zhangshuli@zhangshuli-MS-7817:~/myGit$ git log
commit aec98e99d63338313b35f6f62a44de1e9aff7095
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date:   Fri Feb 13 13:03:09 2015 +0000

    my second commit

commit e63204faffe5e482c18f188ca1c690d961924846
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date:   Fri Feb 13 10:54:58 2015 +0000

    aaa.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git commit --amend
[master ef2db10] my second commit
 1 files changed, 1 insertions(+), 1 deletions(-)
zhangshuli@zhangshuli-MS-7817:~/myGit$ git log
commit ef2db102fc369c5a20e4b9521f0acb6532a75255
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date:   Fri Feb 13 13:03:09 2015 +0000

    my second commit

commit e63204faffe5e482c18f188ca1c690d961924846
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date:   Fri Feb 13 10:54:58 2015 +0000

    aaa.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$

你会发现git commit 之后,节点序号改变了,所以,它不是在原来节点上的增加,而是把他们两个合在一块形成了一次新的节点

6)git commit "file" 默认的是全部提交

6)git log -2 表示的是显示log的最近两次提交

5.仓库状态跳转修改

1)get reset

zhangshuli@zhangshuli-MS-7817:~/myGit$ git log
commit f73968df521e48d23289c5563f7ac7fbc5937b57
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date:   Fri Feb 13 13:24:15 2015 +0000

    my second commit

commit 4c7cede87be37783ca4528fbdcd79bc08e3870fe
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date:   Fri Feb 13 10:54:58 2015 +0000

    my frist commit
zhangshuli@zhangshuli-MS-7817:~/myGit$ git reset 4c7cede87be37783ca4528fbdcd79bc08e3870fe
Unstaged changes after reset:
M    aaa.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# 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:   aaa.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
zhangshuli@zhangshuli-MS-7817:~/myGit$ vim aaa.txt 
zhangshuli@zhangshuli-MS-7817:~/myGit$

它的意思是,从当前节点到指定节点,比较两者的差异,差异存在目录树

它类似于你在指定节点的基础上做了某些修改,现在是修改之后的样子

2)git reset --soft

zhangshuli@zhangshuli-MS-7817:~/myGit$ git log
commit f73968df521e48d23289c5563f7ac7fbc5937b57
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date:   Fri Feb 13 13:24:15 2015 +0000

    my second commit

commit 4c7cede87be37783ca4528fbdcd79bc08e3870fe
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date:   Fri Feb 13 10:54:58 2015 +0000

    my frist commit
zhangshuli@zhangshuli-MS-7817:~/myGit$ git reset --soft 4c7cede87be37783ca4528fbdcd79bc08e3870fe
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    modified:   aaa.txt
#
zhangshuli@zhangshuli-MS-7817:~/myGit$ git diff --cahced .
error: invalid option: --cahced
zhangshuli@zhangshuli-MS-7817:~/myGit$ git diff --cached .
diff --git a/aaa.txt b/aaa.txt
index 3e66ffe..b973e7f 100644
--- a/aaa.txt
+++ b/aaa.txt
@@ -1 +1,2 @@
-my frist commit
+my second commit
+
zhangshuli@zhangshuli-MS-7817:~/myGit$ vim aaa.txt 
zhangshuli@zhangshuli-MS-7817:~/myGit$ 

它跟不加参数的区别就是:它把差异放在了缓存区,相当于进行了一次commit

3)git reset --hard

zhangshuli@zhangshuli-MS-7817:~/myGit$ git log
commit f73968df521e48d23289c5563f7ac7fbc5937b57
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date:   Fri Feb 13 13:24:15 2015 +0000

    my second commit

commit 4c7cede87be37783ca4528fbdcd79bc08e3870fe
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date:   Fri Feb 13 10:54:58 2015 +0000

    my frist commit
zhangshuli@zhangshuli-MS-7817:~/myGit$ git reset --hard 4c7cede87be37783ca4528fbdcd79bc08e3870fe
HEAD is now at 4c7cede my frist commit
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch master
nothing to commit (working directory clean)
zhangshuli@zhangshuli-MS-7817:~/myGit$ vim aaa.txt 
zhangshuli@zhangshuli-MS-7817:~/myGit$

它比较暴力,是直接将当前状态,强制转化为了节点状态,不可恢复,除非你记住了转换之前的节点

上面说的是往前节点的跳转,如果往后其实也是一样的,如

git reset

zhangshuli@zhangshuli-MS-7817:~/myGit$ git reset f73968df521e48d23289c5563f7ac7fbc5937b57
Unstaged changes after reset:
M    aaa.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# 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:   aaa.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
zhangshuli@zhangshuli-MS-7817:~/myGit$ vim aaa.txt 
zhangshuli@zhangshuli-MS-7817:~/myGit$

其实,它相当于,你在跳转节点的基础上(当时aaa.txt里面的值为second commit)当前aaa.txt的值是first commit。它相当于你把aaa.txt的值有原来的second commit ->first commit

从而我们可以知道,节点严格来说不分所谓的前后,因为它代表的仅仅是一个提交点,它是一种状态的id,通过它我们可以找到那个状态。当然,这样说是不严谨的,因为节点都是有父子节点关系的

 

worktools-git 工具的使用总结(1)

标签:

原文地址:http://www.cnblogs.com/zhangshuli-1989/p/zhangshuli_zj_150213135.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!