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分支,git commit,git流程
1. git分支命令规范 1. Master 主分支 2. Dev 开发分支 3. Feature 功能分支(例如:feature-x) 4. Release 预发布分支(例如:release-1.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 ...
- Git 学习(三)本地仓库操作——git add & commit
Git 学习(三)本地仓库操作——git add & commit Git 和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念.这在上文已有提及,本文具体说明什么是工作区及暂存区,以及 ...
- 第二章——建立一个HelloWorld项目,练习使用git的add/commit/push/pull/fetch/clone等基本命令。比较项目的新旧版本的差别-----答题者:徐潇瑞
1.首先下载安装git,很简单所以就不详细说了,当弹出一个类似的命令窗口的东西,就说明Git安装成功 2.因为Git是分布式版本控制系统,所以需要填写用户名和邮箱作为一个标识 3.接着,注册githu ...
- Git学习01 --git add, git commit , git log ,git status, git reset --hard, head
Git官方提供的快速入门教程:https://try.github.io/levels/1/challenges/1 特点:Git极其强大的分支管理:分布式版本 集中式版本控制系统,版本库是集中存放在 ...
- 删除GitHub或者GitLab 上的文件夹,git rm -r --ceched 文件夹名 ,提交commit,git push
方法一 这里以删除 .setting 文件夹为案例 git rm -r --cached .setting #--cached不会把本地的.setting删除 git commit -m 'delet ...
- 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 ...
- Git学习 --> 个人常用命令add,commit以及push
Git命令行配置1 安装Github2 安装msysgit3 要配置用户名和油箱 git config --global user.name <用户名> 我的命令就是:git confi ...
- git 命令的使用(一) add commit push pull
一. commit 和 push 的区别 git作为支持分布式版本管理的工具,它管理的库(repository)分为本地库.远程库.git commit操作的是本地库,git push操作的是远程库. ...
随机推荐
- ZEN_CART_如何添加自定义页面
按照下面的路径,添加自己的文件,就OK了 以about us页面为例, 默认模板 \includes\templates\template_default\templates\tpl_about_us ...
- HDU 2475 BOX 动态树 Link-Cut Tree
Box Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) [Problem De ...
- MapReduce常见算法
1.单词计数 2.数据去重 3.排序 4.Top K(求数据中的最大值) 5.选择 6.投影 7.分组 8.多表连接 9.单表关联
- 链表基础 HDU1267
基础的链表,模拟一下就好了...就签个到
- android脚步---将layout和其输出一起保存成图片
public void convertViewToBitmap(View view) { //View view = getLayoutInflater().inflate(R.layout.test ...
- FZU Problem 2029 买票问题(树状数组)
当我看到题目是5秒的时候,压根没有想树状数组,一直奔着模拟队列去想了,最后也没搞定,赛后看到大神的题解才恍然大悟,原来如此,题目中有明显的暗示,求前n项和,骤然感叹,自己太low... 果然还是要多做 ...
- webapp之路--apple私有属性apple-touch-icon
以前我们用过favicon在浏览器给网站进行身份标识,用法如下: <link href="http://image.feeliu.com/web/favicon.ico" r ...
- 【BZOJ 1572】 1572: [Usaco2009 Open]工作安排Job(贪心+优先队列)
1572: [Usaco2009 Open]工作安排Job Description Farmer John 有太多的工作要做啊!!!!!!!!为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单 ...
- PHP 对MySQLI预处理的包装
mysql 类 <?php class Mysql { private static $instance; private $link; private $query; private $stm ...
- SSH使用TCP Wrappers实现访问控制
SSH使用TCP Wrappers实现访问控制主要配置文件/etc/hosts.allow/etc/hosts.deny===TCP Wrappers的访问控制原则首先检查 hosts.allow 文 ...