http://kasicass.blog.163.com/blog/static/39561920133294219374/
创建测试仓库
$ git init
$ echo "line one" >> foo.txt
$ git add foo.txt
$ git commit -m "first commit"
 
说说 add/reset/diff
我们修改一下 foo.txt,看看效果。
$ echo "line two" >> foo.txt
$ git diff
diff --git a/foo.txt b/foo.txt
index 2d00bd5..e5c5c55 100644
--- a/foo.txt
+++ b/foo.txt
@@ -1 +1,2 @@
 line one
+line two
此时,git diff 可以看到修改内容,但不能 git commit 提交之。
修改的内容,默认会处于 unstage 状态(修改了,但commit不提交),git status 可以看到状态。
$ 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:   foo.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
如果想提交,需要显式 git add 一下。修改的文件状态会变为 staged。
然后 git diff 就看不到修改内容了,而 git diff --cached 才能看到。
当然,也可以用 git commit -a foo.txt 直接提交,而不需要显式 git add foo.txt。
$ git add foo.txt
$ git diff
$ git diff --cached
diff --git a/foo.txt b/foo.txt
index 2d00bd5..e5c5c55 100644
--- a/foo.txt
+++ b/foo.txt
@@ -1 +1,2 @@
 line one
+line two
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# modified:   foo.txt
#
这个优点,同时修改了两处代码,只想提交某一处修改,则显式 git add 想提交的文件即可。
已经 git add 的文件,可以通过  git reset <file> 来 un-add。
$ git reset foo.txt
Unstaged changes after reset:
M foo.txt
$ git diff --cached
$ git diff
diff --git a/foo.txt b/foo.txt
index 2d00bd5..e5c5c55 100644
--- a/foo.txt
+++ b/foo.txt
@@ -1 +1,2 @@
 line one
+line two
如果要将 foo.txt 恢复到修改前的状态,可以 git checkout。
$ git checkout foo.txt
$ git diff
$ git status
# On branch master
nothing to commit (working directory clean)
 
rm、mv 和 log --follow
先给仓库再加入个文件 bar.txt
$ echo "barbar" >> bar.txt
$ git add bar.txt
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: bar.txt
#
此时,bar.txt 并没有通过 git commit 提交到仓库,如果想 unadd foo.txt,需要:
$ git rm --cached bar.txt
rm 'bar.txt'
$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
# bar.txt
nothing added to commit but untracked files present (use "git add" to track)
顺便说一下,对于未 git add 的文件,处于 untracked 状态。git commit 不会提交 untracked 的文件。
对于已经 git add && commit 的文件,直接通过 git rm && commit 将起删除即可。
$ git add bar.txt
$ git commit -m "add bar.txt"
[master 0fa635d] add bar.txt
 1 file changed, 1 insertion(+)
 create mode 100644 bar.txt
$ git rm bar.txt
rm 'bar.txt'
$ git commit -m "remove bar.txt"
[master fcdfd15] remove bar.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 bar.txt
$ ls
foo.txt
我们还可以通过 git mv 来重命名文件。而 git log 的 --follow 参数,可以查看到文件 mv 之前的修改记录。
$ git mv foo.txt rename.txt
$ git commit -m "rename it"
[master 788b36d] rename it
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename foo.txt => rename.txt (100%)
$ git log rename.txt
commit 788b36d799e4f8cea226b29efe13e62ecadea7b8
Author: kasicass <kasicass@gmail.com>
Date:   Thu Apr 4 10:29:47 2013 +0800
 
    rename it
$ git log --follow rename.txt 
commit 788b36d799e4f8cea226b29efe13e62ecadea7b8
Author: kasicass <kasicass@gmail.com>
Date:   Thu Apr 4 10:29:47 2013 +0800
 
    rename it
 
commit 3cc14639388770d01eb8db5fbe57f917584508bf
Author: kasicass <kasicass@gmail.com>
Date:   Thu Apr 4 10:10:42 2013 +0800
 
    first commit
 
深入理解 index
git 中 index 的概念,就是“当前要提交的内容(tracks what you want to commit)”。
我们先来修改一下 rename.txt 并 git add 之。
$ echo "line two" >> rename.txt 
$ git add rename.txt 
$ git diff --cached
diff --git a/rename.txt b/rename.txt
index 2d00bd5..e5c5c55 100644
--- a/rename.txt
+++ b/rename.txt
@@ -1 +1,2 @@
 line one
+line two
我们再修改一下 rename.txt 的内容。
你会发现 git diff 和 git diff --cached 的结果是不一样的。
$ echo "line three" >> rename.txt 
$ git diff
diff --git a/rename.txt b/rename.txt
index e5c5c55..0c2aa38 100644
--- a/rename.txt
+++ b/rename.txt
@@ -1,2 +1,3 @@
 line one
 line two
+line three
$ git diff --cached
diff --git a/rename.txt b/rename.txt
index 2d00bd5..e5c5c55 100644
--- a/rename.txt
+++ b/rename.txt
@@ -1 +1,2 @@
 line one
+line two
对的,git diff --cached 是查看即将提交到仓库的变化,而 git diff 是查看当前与 --cached 的变化。
要想提交最新修改,还得再 git add rename.txt 一次。
$ git add rename.txt 
$ git diff
$ git diff --cached
diff --git a/rename.txt b/rename.txt
index 2d00bd5..0c2aa38 100644
--- a/rename.txt
+++ b/rename.txt
@@ -1 +1,3 @@
 line one
+line two
+line three
所以,可以理解为 current diff ==> index cache ==> repo。
当前的修改,通过 git add 提交到 index cache,再通过 git commit 正式提交到 repo。
 
ps.
如果 git commit 没有 -m 参数,git 会启用 GIT_EDITOR 来编辑 message。
$ export GIT_EDITOR=vim
 
 
 
 
 

[git] 细说commit (git add/commit/diff/rm/reset 以及 index 的概念)的更多相关文章

  1. git分支,git commit,git流程

    1. git分支命令规范 1. Master 主分支 2. Dev 开发分支 3. Feature 功能分支(例如:feature-x) 4. Release 预发布分支(例如:release-1.2 ...

  2. Git(6)-- 记录每次更新到仓库(git clone、status、add、diff、commit、rm、mv命令详解)

    @ 目录 1.克隆现有仓库:git clone 2.检查当前文件状态 :git status 3.跟踪新文件:git add 4.暂存已修改的文件:git add 5.状态简览: git status ...

  3. Git 学习(三)本地仓库操作——git add & commit

    Git 学习(三)本地仓库操作——git add & commit Git 和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念.这在上文已有提及,本文具体说明什么是工作区及暂存区,以及 ...

  4. 第二章——建立一个HelloWorld项目,练习使用git的add/commit/push/pull/fetch/clone等基本命令。比较项目的新旧版本的差别-----答题者:徐潇瑞

    1.首先下载安装git,很简单所以就不详细说了,当弹出一个类似的命令窗口的东西,就说明Git安装成功 2.因为Git是分布式版本控制系统,所以需要填写用户名和邮箱作为一个标识 3.接着,注册githu ...

  5. Git学习01 --git add, git commit , git log ,git status, git reset --hard, head

    Git官方提供的快速入门教程:https://try.github.io/levels/1/challenges/1 特点:Git极其强大的分支管理:分布式版本 集中式版本控制系统,版本库是集中存放在 ...

  6. 删除GitHub或者GitLab 上的文件夹,git rm -r --ceched 文件夹名 ,提交commit,git push

    方法一 这里以删除 .setting 文件夹为案例 git rm -r --cached .setting #--cached不会把本地的.setting删除 git commit -m 'delet ...

  7. Git CMD连接,管理(remote,add,commit,push)github repository

    git initmd testcd testgit statusgit add test  //git add test/a.txtgit status git remote add origin g ...

  8. Git学习 --> 个人常用命令add,commit以及push

    Git命令行配置1 安装Github2 安装msysgit3 要配置用户名和油箱  git config --global user.name <用户名> 我的命令就是:git confi ...

  9. git 命令的使用(一) add commit push pull

    一. commit 和 push 的区别 git作为支持分布式版本管理的工具,它管理的库(repository)分为本地库.远程库.git commit操作的是本地库,git push操作的是远程库. ...

随机推荐

  1. Linux 查看 硬件配置

    一:查看cpu more /proc/cpuinfo | grep "model name" grep "model name" /proc/cpuinfo 如 ...

  2. Spring+Struts集成(方案一)

    SSH框架是现在非常流行的框架之一,本文接下来主要来对Spring和Struts的集成进行展示. 集成原理:在Action中取得BeanFactory,通过BeanFactory取得业务逻辑对象. 集 ...

  3. android4.0下如何判断手机是否有底部物理按键(menu物理按键)

    某些手机底部是在触摸屏内部有软按键,就是如(back,home,menu 等)而有的手机底部(非屏幕内部)有物理按键,就是生产厂商不愿意有google自带的虚拟按键,而做的电容式的物理按键,如(bac ...

  4. s15day14 ssh秘钥远程连接

    1 使用密钥登录    1.1 创建密钥对    1.2 上传公钥文件    1.3 导入公钥信息    1.4 使用密钥对登录2 远程执行命令    2.1 简单命令    2.2 使用脚本执行多命 ...

  5. JS和CSS加载(渲染)机制不同

    一.结论 CSS可以在页面加载完成后随时渲染.举个例子:通过js给某个元素加一个id或者css,只要这个id或者css有对应的样式,此元素的样式就会自动生效. JS不可以在页面加载完成后生效.最明显的 ...

  6. CSS盒模型和margin重叠

    在 CSS 中,width 和 height 指的是内容区域的宽度和高度.增加内边距.边框和外边距不会影响内容区域的尺寸,但是会增加元素框的总尺寸.(div的实际占用尺寸变打了) 但: 一旦为页面设置 ...

  7. Shortest Path

    Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  8. android4.0 的图库Gallery2代码分析(二)

    最近迫于生存压力,不得不给人兼职打工.故在博文中加了个求点击的链接.麻烦有时间的博友们帮我点击一下.没时间的不用勉强啊.不过请放心,我是做技术的,肯定链接没病毒,就是我打工的淘宝店铺.嘻嘻.http: ...

  9. JavaScript(三)---- 控制流程语句

    常用的控制流程语句有判断语句.分支语句.循环语句.基本用法都和java中的一致,switch有几点特殊. 1.判断语句 格式:        if(判断条件){            符合条件执行的代 ...

  10. iOS之Nib和Xib以及storyboard(故事版)

    本文转发至:http://blog.csdn.net/tonny_guan/article/details/8542789 nib.xib与故事板 如果大家使用过苹果的官方资料,一定会发现某些资料上会 ...