二、GIT基础-记录文件更新

2、记录文件更新

1)检查当前文件状态

要查看哪些文件处于什么状态,可以用 git status 命令。

[root@node1 git-test]# git status
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to track)

我们可以看到这是个刚初始化的目录,没有未提交的文件。后面还提示我们,可以创建或者复制文件,然后使用git add命令对文件进行跟踪。

2)跟踪新文件

我们首先在当前目录下创建一个新文件README,然后再查看当前文件的状态

[root@node1 git-test]# ll
total 4
-rw-r--r-- 1 root root 26 Nov 28 08:13 README

[root@node1 git-test]# git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    README

nothing added to commit but untracked files present (use "git add" to track)
[root@node1 git-test]#

在状态报告中可以看到新建的 README 文件出现在 Untracked files 下面。 未跟踪的文件意味着 Git 在之前的快照(提交)中没有这些文件;Git 不会自动将之纳入跟踪范围,而且后面的提示我们可以使用git add命令去跟踪新文件
使用命令 git add 开始跟踪一个文件。
[root@node1 git-test]# git add README
再运行 git status 命令,会看到 README 文件已被跟踪,并处于暂存状态:

On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)   git add 的逆操作,具体后面讲解

    new file:   README

只要在 Changes to be committed 这行下面的,就说明是已暂存状态。 如果此时commit,那么该文件此时此刻的版本将被留存在历史记录中。 git add 命令使用文件或目录的路径作为参数;如果参数是目录的路径,该命令将递归地跟踪该目录下的所有文件。我们也可以使用git add *提交当前目录下的所有文件

3)提交更新

现在我们已经把创建的文件提交到了暂存区,接下来我们就需要把暂存区的文件提交到本地版本库进行管理。注意:在每交提交前最好先使用git status查看一下,是不是所有更改的文件都已经暂存,然后再使用git commit命令提交。

[master (root-commit) 5e874cc] first submit
 1 file changed, 1 insertion(+)
 create mode 100644 README
[root@node1 git-test]# git status
On branch master
nothing to commit, working tree clean

请记住,提交时记录的是放在暂存区域的快照。 Git的每一次提交操作,都是对你项目作一次快照,以后可以回到这个状态,或者进行比较。

4)暂存己修改的文件

我们首先再新建一个Hello.txt的文件并提交:

[root@node1 git-test]# echo "Hello world">Hello.txt
[root@node1 git-test]# git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    Hello.txt

nothing added to commit but untracked files present (use "git add" to track)
[root@node1 git-test]# git add Hello.txt 
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   Hello.txt

接着我们修改刚才已经提交过的README文件,然后查看状态:

[root@node1 git-test]# echo "first modify" >>README 
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   Hello.txt

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:   README

文件 README 出现在 Changes not staged for commit 这行下面,说明已跟踪文件的内容发生了变化,但还没有放到暂存区。 要暂存这次更新,需要运行 git add 命令。 这是个多功能命令:可以用它开始跟踪新文件,或者把已跟踪的文件放到暂存区,还能用于合并时把有冲突的文件标记为已解决状态等。 将这个命令理解为“添加内容到下一次提交中”更加合适。 现在让我们运行 git add 将"README"放到暂存区,然后再看看 git status 的输出:

You have new mail in /var/spool/mail/root
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   Hello.txt
    modified:   README

现在两个文件都已暂存,下次提交时就会一并记录到仓库。 假设此时,你想要在 README 里再加一条内容, 重新编辑存盘后,再运行 git status 看看:

[root@node1 git-test]# echo "second modify" >>README 
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   Hello.txt
    modified:   README

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:   README

你看到了什么? 现在 README 文件同时出现在暂存区和非暂存区。 这是为什么? 好吧,实际上 Git 只不过暂存了你运行 git add 命令时的版本, 如果你现在提交,README的版本是你最后一次运行 git add 命令时的那个版本,而不是你运行 git commit 时,在工作目录中的当前版本。 所以,运行了 git add 之后又作了修订的文件,需要重新运行 git add 把最新版本重新暂存起来:

[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   Hello.txt
    modified:   README

[root@node1 git-test]# git commit -m "second submit"
[master a306d94] second submit
 2 files changed, 3 insertions(+)
 create mode 100644 Hello.txt
[root@node1 git-test]# git status
On branch master
nothing to commit, working tree clean

5)四个区域和文件的四种状态
四个区域:


image.png

文件的四种状态:


image.png

6)查看已暂存和未暂存的修改

我们再次修改 README 文件后暂存,然后编辑 Hello.txt文件后先不暂存, 运行 status 命令将会看到:

[root@node1 git-test]# echo "first modify" >>Hello.txt 
[root@node1 git-test]# git add README 
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   README

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:   Hello.txt

要查看尚未暂存的文件更新了哪些部分,不加参数直接输入 git diff:

[root@node1 git-test]# git diff
diff --git a/Hello.txt b/Hello.txt
index 802992c..6b599f0 100644
--- a/Hello.txt
+++ b/Hello.txt
@@ -1 +1,2 @@
 Hello world
+first modify

此命令比较的是工作目录中当前文件和暂存区域快照之间的差异, 也就是修改之后还没有暂存起来的变化内容。
要查看已暂存的将要添加到下次提交里的内容,可以用 git diff --cached 命令或git diff --staged,效果是相同的。

[root@node1 git-test]# git diff --cached
diff --git a/README b/README
index 771c673..5f9ac91 100644
--- a/README
+++ b/README
@@ -1,3 +1,4 @@
 My first git test project
 first modify
 second modify
+three modify
[root@node1 git-test]# git diff --staged
diff --git a/README b/README
index 771c673..5f9ac91 100644
--- a/README
+++ b/README
@@ -1,3 +1,4 @@
 My first git test project
 first modify
 second modify
+three modify

现在我们将Hello.txt文件暂存,然后再进行编辑后,查看状态:

[root@node1 git-test]# git add Hello.txt 
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   Hello.txt
    modified:   README

[root@node1 git-test]# git diff
[root@node1 git-test]# echo "second modify" >>Hello.txt 
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   Hello.txt
    modified:   README

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:   Hello.txt

[root@node1 git-test]# git diff
diff --git a/Hello.txt b/Hello.txt
index 6b599f0..bfd0f21 100644
--- a/Hello.txt
+++ b/Hello.txt
@@ -1,2 +1,3 @@
 Hello world
 first modify
+second modify
[root@node1 git-test]# git diff --staged
diff --git a/Hello.txt b/Hello.txt
index 802992c..6b599f0 100644
--- a/Hello.txt
+++ b/Hello.txt
@@ -1 +1,2 @@
 Hello world
+first modify
diff --git a/README b/README
index 771c673..5f9ac91 100644
--- a/README
+++ b/README
@@ -1,3 +1,4 @@
 My first git test project
 first modify
 second modify
+three modify

请注意,git diff 本身只显示尚未暂存的改动,而不是自上次提交以来所做的所有改动。 所以有时候你一下子暂存了所有更新过的文件后,运行 git diff 后却什么也没有,就是这个原因

7)跳过使用暂存区

git commit 加上 -a 选项,Git 就会自动把所有已经跟踪过的文件暂存起来一并提交,从而跳过 git add 步骤:

[root@node1 git-test]# 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:   Hello.txt

no changes added to commit (use "git add" and/or "git commit -a")
[root@node1 git-test]# git commit -a -m "jump git add submit"
[master 4794019] jump git add submit
 1 file changed, 2 insertions(+)
[root@node1 git-test]# git status
On branch master
nothing to commit, working tree clean

8、移除文件

我们先创建一个用于删除测试的文件a.txt,并提交

[root@node1 git-test]# git add a.txt 
[root@node1 git-test]# git commit -m "rm test"
[master a80c6a9] rm test
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt

现在我们在工作目录删除a.txt文件,然后查看状态,在 “Changes not staged for commit” 部分(也就是 未暂存清单)看到

[root@node1 git-test]# git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    deleted:    a.txt

no changes added to commit (use "git add" and/or "git commit -a")

然后再运行 git rm 从暂存区删除文件:

rm 'a.txt'
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    a.txt

[root@node1 git-test]# git commit -m "rm test finish"
[master 3a2db18] rm test finish
 1 file changed, 1 deletion(-)
 delete mode 100644 a.txt
[root@node1 git-test]# git status
On branch master
nothing to commit, working tree clean

另外一种情况是,我们想把文件从 Git 仓库中删除(亦即从暂存区域移除),但仍然希望保留在当前工作目录中。 换句话说,你想让文件保留在磁盘,但是并不想让 Git 继续跟踪。使用 --cached 选项

[root@node1 git-test]# git commit -m "delete a.txt from response"

9、移动文件

使用git mv命令对文件进行改名操作:
使用mv命令改名:

[root@node1 git-test]# git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    deleted:    Hello.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    Helloword.txt

no changes added to commit (use "git add" and/or "git commit -a")
[root@node1 git-test]# git add Helloword.txt 
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   Helloword.txt

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    deleted:    Hello.txt

[root@node1 git-test]# git commit -m "mv test"
[master 9ccca93] mv test
 1 file changed, 4 insertions(+)
 create mode 100644 Helloword.txt
[root@node1 git-test]# git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    deleted:    Hello.txt

no changes added to commit (use "git add" and/or "git commit -a")
[root@node1 git-test]# git rm Hello.txt
rm 'Hello.txt'
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    Hello.txt

[root@node1 git-test]# git commit -m "delete Hello.txt"
[master 2b1583e] delete Hello.txt
 1 file changed, 4 deletions(-)
 delete mode 100644 Hello.txt
You have new mail in /var/spool/mail/root
[root@node1 git-test]# git status
On branch master
nothing to commit, working tree clean
[root@node1 git-test]# ll
total 8
-rw-r--r-- 1 root root 52 Nov 28 11:17 Helloword.txt
-rw-r--r-- 1 root root 66 Nov 28 10:42 README

使用git mv 命令改名

[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    renamed:    Helloword.txt -> Hello.txt

[root@node1 git-test]# git commit -m "git mv test"
[master 6597abf] git mv test
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename Helloword.txt => Hello.txt (100%)
[root@node1 git-test]# git status
On branch master
nothing to commit, working tree clean

不管何种方式结果都一样。 两者唯一的区别是,mv 是一条命令而另一种方式需要三条命令,直接用 git mv 轻便得多

10、忽略文件

一般我们总会有些文件无需纳入 Git 的管理,也不希望它们总出现在未跟踪文件列表。 通常都是些自动生成的文件,比如日志文件,或者编译过程中创建的临时文件等。 在这种情况下,我们可以创建一个名为 .gitignore 的文件,列出要忽略的文件模式。
我们再看一个 .gitignore 文件的例子:

*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in the build/ directory
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory
doc/**/*.pdf
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容

  • 1.git的安装 1.1 在Windows上安装Git msysgit是Windows版的Git,从https:/...
    落魂灬阅读 12,581评论 4 54
  • 天气 阵雨 每天老师讲的都是不一样的加人方法,让朋友分享到朋友圈的之前试过,可能是方法不对,所以也没人加。今天上班...
    欢Y阅读 107评论 0 0
  • 很感谢生命中出现的每一个人,他们教会你成长。 人生的路很遥远,这么长的路途中你会遇到各色各样的人。比如,工作中,同...
    巫女颖惠阅读 357评论 0 0
  • 每天中午去外面吃午饭的时候,总要经过一家有名的整容医院,有时候会看到出入医院的妙龄女郎,面容姣好,身材健美,不禁会...
    Jane_gz阅读 136评论 0 0
  • 像往常一样,每天看完勃君的更新后开始思考今天该写点什么,勃君的文章正在一点点的发生着变化,思维逻辑更加严谨,文笔更...
    黑莓可爱多阅读 1,345评论 0 0