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

1.仓库创建

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

2.更改添加

//创建一个新文件[email protected]-MS-7817:~/myGit$ touch bbb.txt
//查看当前目录[email protected]-MS-7817:~/myGit$ ls
aaa.txt  bbb.txt
//查看仓库状态[email protected]-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)//提交到缓存去(工作目录树)
[email protected]7817:~/myGit$ git add .
[email protected]7817:~/myGit$ git st
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#    new file:   aaa.txt
#
 [email protected]-MS-7817:~/myGit$

3.仓库修改还原

[email protected]7817:~/myGit$ ls
aaa.txt
[email protected]-MS-7817:~/myGit$ git br -av
* master 9a1e055 aaa.txt
[email protected]-MS-7817:~/myGit$ git st
# On branch master
nothing to commit (working directory clean)
[email protected]-MS-7817:~/myGit$ touch bbb.txt ccc.txt
[email protected]-MS-7817:~/myGit$ ls
aaa.txt  bbb.txt  ccc.txt
[email protected]-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)
[email protected]-MS-7817:~/myGit$ git add bbb.txt
[email protected]-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
[email protected]-MS-7817:~/myGit$ git co ccc.txt
error: pathspec ‘ccc.txt‘ did not match any file(s) known to git.
[email protected]-MS-7817:~/myGit$ git checkout ccc.txt
error: pathspec ‘ccc.txt‘ did not match any file(s) known to git.
[email protected]-MS-7817:~/myGit$ git clear -df .
git: ‘clear‘ is not a git command. See ‘git --help‘.

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

[email protected]:~/myGit$ git clean -df aaa.txt[email protected]:~/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#[email protected]:~/myGit$ vim aaa.txt [email protected]:~/myGit$ git co aaa.txt[email protected]:~/myGit$ lsaaa.txt  bbb.txt[email protected]:~/myGit$ touch ccc.txt[email protected]:~/myGit$ lsaaa.txt  bbb.txt  ccc.txt[email protected]:~/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[email protected]:~/myGit$ git checkout ccc.txterror: pathspec ‘ccc.txt‘ did not match any file(s) known to git.[email protected]:~/myGit$ git co ccc.txterror: pathspec ‘ccc.txt‘ did not match any file(s) known to git.[email protected]:~/myGit$ git clean diff .fatal: clean.requireForce defaults to true and neither -n nor -f given; refusing to clean[email protected]:~/myGit$ git clean -f .Removing ccc.txt[email protected]:~/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.版本库的撤消跟修改

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

[email protected]-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)
[email protected]-MS-7817:~/myGit$ git add .
[email protected]-MS-7817:~/myGit$ git st
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    new file:   bbb.txt
#
[email protected]-MS-7817:~/myGit$ git reset .
[email protected]-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)
[email protected]-MS-7817:~/myGit$ git add .
[email protected]-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
[email protected]-MS-7817:~/myGit$ git st
# On branch master
nothing to commit (working directory clean)
[email protected]-MS-7817:~/myGit$ git br -av
* master 2f6533f bbb.txt
[email protected]-MS-7817:~/myGit$ git log
commit 2f6533fe371f8b5a9dfa9c502bc20e22aaaa6177
Author: zhangshuli <[email protected]>
Date:   Fri Feb 13 11:41:01 2015 +0000

    bbb.txt

    add

commit 9a1e05516a0436f46b73c9553795ae22acfb2eee
Author: zhangshuli <[email protected]>
Date:   Fri Feb 13 10:54:58 2015 +0000

    aaa.txt
[email protected]-MS-7817:~/myGit$

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

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

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

3)看如下的例子

[email protected]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)
[email protected]-MS-7817:~/myGit$ vim bbb.txt
[email protected]-MS-7817:~/myGit$ git add .
[email protected]-MS-7817:~/myGit$ git st
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    new file:   bbb.txt
#
[email protected]-MS-7817:~/myGit$ vim bbb.txt
[email protected]-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
#
[email protected]-MS-7817:~/myGit$ git reset bbb.txt
[email protected]-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)
[email protected]-MS-7817:~/myGit$ git clean -f .
Removing bbb.txt
[email protected]-MS-7817:~/myGit$ git st
# On branch master
nothing to commit (working directory clean)
[email protected]-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 是在当前的节点上追加内容(当然也可以通过它来给注释重命名).理解错误,看如下实例

[email protected]7817:~/myGit$ git log
commit aec98e99d63338313b35f6f62a44de1e9aff7095
Author: zhangshuli <[email protected]>
Date:   Fri Feb 13 13:03:09 2015 +0000

    my second commit

commit e63204faffe5e482c18f188ca1c690d961924846
Author: zhangshuli <[email protected]>
Date:   Fri Feb 13 10:54:58 2015 +0000

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

    my second commit

commit e63204faffe5e482c18f188ca1c690d961924846
Author: zhangshuli <[email protected]>
Date:   Fri Feb 13 10:54:58 2015 +0000

    aaa.txt
[email protected]-MS-7817:~/myGit$

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

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

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

5.仓库状态跳转修改

1)get reset

[email protected]7817:~/myGit$ git log
commit f73968df521e48d23289c5563f7ac7fbc5937b57
Author: zhangshuli <[email protected]>
Date:   Fri Feb 13 13:24:15 2015 +0000

    my second commit

commit 4c7cede87be37783ca4528fbdcd79bc08e3870fe
Author: zhangshuli <[email protected]>
Date:   Fri Feb 13 10:54:58 2015 +0000

    my frist commit
[email protected]-MS-7817:~/myGit$ git reset 4c7cede87be37783ca4528fbdcd79bc08e3870fe
Unstaged changes after reset:
M    aaa.txt
[email protected]-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")
[email protected]-MS-7817:~/myGit$ vim aaa.txt
[email protected]-MS-7817:~/myGit$

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

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

2)git reset --soft

[email protected]7817:~/myGit$ git log
commit f73968df521e48d23289c5563f7ac7fbc5937b57
Author: zhangshuli <[email protected]>
Date:   Fri Feb 13 13:24:15 2015 +0000

    my second commit

commit 4c7cede87be37783ca4528fbdcd79bc08e3870fe
Author: zhangshuli <[email protected]>
Date:   Fri Feb 13 10:54:58 2015 +0000

    my frist commit
[email protected]-MS-7817:~/myGit$ git reset --soft 4c7cede87be37783ca4528fbdcd79bc08e3870fe
[email protected]-MS-7817:~/myGit$ git st
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    modified:   aaa.txt
#
[email protected]-MS-7817:~/myGit$ git diff --cahced .
error: invalid option: --cahced
[email protected]-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
+
[email protected]-MS-7817:~/myGit$ vim aaa.txt
[email protected]-MS-7817:~/myGit$ 

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

3)git reset --hard

[email protected]7817:~/myGit$ git log
commit f73968df521e48d23289c5563f7ac7fbc5937b57
Author: zhangshuli <[email protected]>
Date:   Fri Feb 13 13:24:15 2015 +0000

    my second commit

commit 4c7cede87be37783ca4528fbdcd79bc08e3870fe
Author: zhangshuli <[email protected]>
Date:   Fri Feb 13 10:54:58 2015 +0000

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

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

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

git reset

[email protected]7817:~/myGit$ git reset f73968df521e48d23289c5563f7ac7fbc5937b57
Unstaged changes after reset:
M    aaa.txt
[email protected]-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")
[email protected]-MS-7817:~/myGit$ vim aaa.txt
[email protected]-MS-7817:~/myGit$

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

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

时间: 2024-11-09 02:45:05

worktools-git 工具的使用总结(1)的相关文章

git工具的使用

Git工具的出现降低了软件版本维护的成本,极大的提高了工作效率,在这里列出了一些使用方法,方便以后查看. 1.Git的初始化->创建一个Git仓库:git init 2.创建信息:git config --global user.name-->创建用户名 git config --global user.email-->创建用户邮箱 git config --list-->查看配置信息 3.提交创建的文件:git add 文件的路径(这是具体的一个文件路径)-->添加到缓存区

Myeclipse如何使用自带git工具向远程仓库提交代码

先看一下Myeclipse自带的git工具  本人是在码云上面注册的账号,上面有项目的仓库,将仓库的项目克隆到本地之后,在myeclipse中导入该项目. 那么如何将修改后的代码再提交到码云上面? 第一步:将改动的代码标记 项目右键:team->synchronize workspace  点击确定  项目右键>add to git index  第二步:将工作空间的代码提交到本地仓库 commit  需要写出注释  第三步:将远程仓库的代码合并到本地仓库 pull 这一步可能需要解决代码冲突

日积月累Learning Linux(一):在Linux下安装Git工具

背景: 由于项目需要,经常在github上下载开源库项目源代码.因此最近决定花时间系统学习一下Linux下的相关软件的安装方法,这里就以Git工具的安装为例进行讲解. Linux(fedora18)安装Git工具: 1)Git与Github的区别: Github是一个网站,给用户提供git服务.相当于web版的Git工具,在Github上注册个账号,就可以享受网站提供的Git服务.而Git是一个版本控制系统,与SVN.CVS是类似的概念,简单点说就是对你的文件的一种版本管理(此处所谓的版本其实就

Git详解之六 Git工具(转)

Git 工具 现在,你已经学习了管理或者维护 Git 仓库,实现代码控制所需的大多数日常命令和工作流程.你已经完成了跟踪和提交文件的基本任务,并且发挥了暂存区和轻量级的特性分支及合并的威力. 接下来你将领略到一些 Git 可以实现的非常强大的功能,这些功能你可能并不会在日常操作中使用,但在某些时候你也许会需要. 6.1  修订版本(Revision)选择 Git 允许你通过几种方法来指明特定的或者一定范围内的提交.了解它们并不是必需的,但是了解一下总没坏处. 单个修订版本 显然你可以使用给出的

windows中使用Git工具连接GitHub(配置篇)

Git在源码管理领域目前占很大的比重了,而且开源的项目很多都转到GitHub上面了.例如:jQuery, reddit, Sparkle, curl, Ruby on Rails, node.js, ClickToFlash, Erlang/OTP,CakePHP, Redis. 本文详细的说明了如何在Windows下安装配置Git工具连接GitHub.并可以在Visual Studio中使用Git. 一.准备工具 1.下载Git Extensions.地址 http://code.google

【转】第 02 天:在 Windows 平台必裝的三套 Git 工具

原文网址:https://github.com/doggy8088/Learn-Git-in-30-days/blob/master/docs/02%20%E5%9C%A8%20Windows%20%E5%B9%B3%E5%8F%B0%E5%BF%85%E8%A3%9D%E7%9A%84%E4%B8%89%E5%A5%97%20Git%20%E5%B7%A5%E5%85%B7.markdown 要開始使用 Git 版本控管,首先要安裝適當的 Git 工具,這個系列的文章主要還是以 Windows

git工具的使用教程

Git 是一种版本控制工具,也叫作版本管理软件(分布式管理软件).这里介绍Git的基本使用步骤,关于 Git 更详细的介绍,读者可以参考其官方网站提供的文档. 1  安装Git 在Ubuntu系统中安装Git工具: # sudo apt-get install git 如果是第一次使用 Git,需要进行如下设置: # git config --global user.name "laoyao" # git config --global user.email "[email 

代码管理工具:使用github和git工具管理自己的代码

一.git工具和账户创建 1.1 安装 Git 是 Linus Torvalds 最近实现的源代码管理软件."Git 是一个快速.可扩展的分布式版本控制系统,它具有极为丰富的命令集,对内部系统提供了高级操作和完全访问." Git 目前主要由寻找 CVS 或专有代码管理解决方案替代物的软件开发人员所使用.Git 与 CVS 有很多区别: 分支更快.更容易. 支持离线工作:本地提交可以稍后提交到服务器上. Git 提交都是原子的,且是整个项目范围的,而不像 CVS 中一样是对每个文件的.

git工具学习

最近实习的时候,遇到git工具,发现好强大之前没用过,特来学习下,然后自己注册了一个github账号,结合git命令练习一下,git的安装就不说了. git简介: git是分布式版本控制系统,相对于集中式版本控制系统有很大的优势,集中式版本控制系统最大的毛病就是必须联网才能工作,并且,分布式版本控制系统的安全性要高很多,因为每个人电脑里都有完整的版本库,某一个人的电脑坏掉了不要紧,随便从其他人那里复制一个就可以了.而集中式版本控制系统的中央服务器要是出了问题,所有人都没法干活了. git常用命令

如何禁用/关闭vs2017自带的Git工具的?

对于用习惯了独立Git工具和命令行的人来说,看到Visual Studio自带的Git工具后,很是别扭,到处充满了不习惯,而且是不是还会出现电脑卡顿的现象(可能是我自身电脑配置一般的问题). 如何关闭vs2017自带的Git工具? 解决方案:Vs顶部菜单 - 工具 - 选项 - 源代码管理 - 插件选择 - 当前源代码插件:设置为“无”. 如何禁用Vs2017自带的Git ? 解决方案: ctrl + r 输入 regedit 打开注册表后找到:HKEY_CURRENT_USER\Softwar