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. springMVC如何访问静态文件

    在进行Spring MVC的配置时,通常我们会配置一个dispatcher servlet用于处理对应的URL.配置如下:<servlet><servlet-name>mvc- ...

  2. ThinkPHP3.2.3版本框架could not find driver错误

    ThinkPHP3.2.3版本框架could not find driver错误 在更新ThinkPHP框架 3.2.3 时出现错误 解决方法如下: 修改php.ini文件 extension=php ...

  3. MySQL 建表

    SET NAMES utf8; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for ` ...

  4. [转载] ASP.NET MVC4使用百度UEDITOR编辑器

    前言 配置.net mvc4项目使用ueditor编辑器,在配置过程中遇见了好几个问题,以此来记录解决办法.编辑器可以到http://ueditor.baidu.com/website/downloa ...

  5. CentOS 修改DNS,固定IP等操作(网络)

    1.修改DNS 修改对应网卡的DNS的配置文件 vi /etc/resolv.conf 内容格式(西工大) nameserver 114.114.114.114 nameserver 202.117. ...

  6. .net杂记

    1. .net 接口命名规范例如I***able; 2. 接口扩展功能,代表类Can do;抽象类代表族 Is a; 3. 不要在foreach中处理异常;

  7. 一个门外汉的理解 ~ Faster R-CNN

    首先放R-CNN的原理图 显然R-CNN的整过过程大致上划分为四步: 1.输入图片 2.生成候选窗口 3.对局部窗口进行特征提取(CNN) 4.分类(Classify regions) 而R-CNN的 ...

  8. Bootstrap登录样式

    样式1 <!--bs中所有的均应该包裹在其中.--> <div class="container"> <!-- row是珊栏系统的包裹容器--> ...

  9. 行内元素为何不能设置margin-top、margin-bottom;padding-top、padding-bottom值

    曾经学过的教程中写明:行内元素的特点有: 1.与其他元素在同一行 2.宽度(width).高度(height).内边距的top/bottom(padding-top/padding-bottom)和外 ...

  10. JavaFX 2.0+ WebView /WebEngine render web page to an image

    http://stackoverflow.com/questions/7796558/javafx-2-0-webview-webengine-render-web-page-to-an-image ...