7.Git工作区和暂存区

Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念。
先来看名词解释。

1.工作区(Working Directory)

就是你在电脑里能看到的目录,比如我的test文件夹就是一个工作区:
$ ls test/
helloWorld.txt

2.版本库(Repository)

工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。
Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。

7.Git工作区和暂存区
前面讲了我们把文件往Git版本库里添加的时候,是分两步执行的:
第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区:
第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支.

因为我们创建Git版本库时,Git自动为我们创建了唯一一个master分支.所以,现在,git commit就是往master分支上提交更改.

你可以简单理解为,需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改.

俗话说,实践出真知.现在,我们再练习一遍,先对helloWorld.txt做个修改,比如,加上一行内容:
    $ vi helloWorld.txt
    $ cat helloWorld.txt
    hello world !
    first:di yi ci xiugai!

    second:di er ci xiugai!

    third:di san ci xiugai!

然后,在工作区新增一个readme.md文本文件(内容随便写),
    $ echo 'readme' >> readme.md
    $ cat readme.md
    readme
先用git status查看一下状态:
    $ git status
    On branch dev
    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:   helloWorld.txt
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            readme.md
    no changes added to commit (use "git add" and/or "git commit -a")
</file></file></file>
Git&#x975E;&#x5E38;&#x6E05;&#x695A;&#x5730;&#x544A;&#x8BC9;&#x6211;&#x4EEC;,helloWorld.txt&#x88AB;&#x4FEE;&#x6539;&#x4E86;,&#x800C;readme.md&#x8FD8;&#x4ECE;&#x6765;&#x6CA1;&#x6709;&#x88AB;&#x6DFB;&#x52A0;&#x8FC7;,&#x6240;&#x4EE5;&#x5B83;&#x7684;&#x72B6;&#x6001;&#x662F;Untracked.

&#x73B0;&#x5728;,&#x4F7F;&#x7528;&#x547D;&#x4EE4;git add .,&#x518D;&#x7528;git status&#x518D;&#x67E5;&#x770B;&#x4E00;&#x4E0B;&#xFF1A;
    $ git add .
    warning: LF will be replaced by CRLF in readme.md.

    The file will have its original line endings in your working directory

    $ git status
    On branch master
    Your branch is ahead of 'origin/master' by 1 commit.

      (use "git push" to publish your local commits)
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
            new file:   README.md
            modified:   helloWorld.txt
&#x73B0;&#x5728;&#xFF0C;&#x6682;&#x5B58;&#x533A;&#x7684;&#x72B6;&#x6001;&#x5C31;&#x53D8;&#x6210;&#x8FD9;&#x6837;&#x4E86;&#xFF1A;
</file>

7.Git工作区和暂存区
&#x6240;&#x4EE5;&#xFF0C;git add&#x547D;&#x4EE4;&#x5B9E;&#x9645;&#x4E0A;&#x5C31;&#x662F;&#x628A;&#x8981;&#x63D0;&#x4EA4;&#x7684;&#x6240;&#x6709;&#x4FEE;&#x6539;&#x653E;&#x5230;&#x6682;&#x5B58;&#x533A;&#xFF08;Stage&#xFF09;&#xFF0C;&#x7136;&#x540E;&#xFF0C;&#x6267;&#x884C;git commit&#x5C31;&#x53EF;&#x4EE5;&#x4E00;&#x6B21;&#x6027;&#x628A;&#x6682;&#x5B58;&#x533A;&#x7684;&#x6240;&#x6709;&#x4FEE;&#x6539;&#x63D0;&#x4EA4;&#x5230;&#x5206;&#x652F;&#x3002;
    $ git commit -m "&#x77E5;&#x9053;&#x6682;&#x5B58;&#x533A;stage&#x7684;&#x610F;&#x601D;&#x4E86;"
    [master 9d65cb2] &#x77E5;&#x9053;&#x6682;&#x5B58;&#x533A;stage&#x7684;&#x610F;&#x601D;&#x4E86;
     2 files changed, 2 insertions(+)
     create mode 100644 readme.md
 &#x4E00;&#x65E6;&#x63D0;&#x4EA4;&#x540E;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x53C8;&#x6CA1;&#x6709;&#x5BF9;&#x5DE5;&#x4F5C;&#x533A;&#x505A;&#x4EFB;&#x4F55;&#x4FEE;&#x6539;&#xFF0C;&#x90A3;&#x4E48;&#x5DE5;&#x4F5C;&#x533A;&#x5C31;&#x662F;&#x201C;&#x5E72;&#x51C0;&#x201D;&#x7684;&#xFF1A;
    $ git status
    On branch master
    nothing to commit, working directory clean
&#x73B0;&#x5728;&#x7248;&#x672C;&#x5E93;&#x53D8;&#x6210;&#x4E86;&#x8FD9;&#x6837;&#xFF0C;&#x6682;&#x5B58;&#x533A;&#x5C31;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x5185;&#x5BB9;&#x4E86;&#xFF1A;

7.Git工作区和暂存区
&#x6682;&#x5B58;&#x533A;&#x662F;Git&#x975E;&#x5E38;&#x91CD;&#x8981;&#x7684;&#x6982;&#x5FF5;,&#x5F04;&#x660E;&#x767D;&#x4E86;&#x6682;&#x5B58;&#x533A;,&#x5C31;&#x5F04;&#x660E;&#x767D;&#x4E86;Git&#x7684;&#x5F88;&#x591A;&#x64CD;&#x4F5C;&#x5230;&#x5E95;&#x5E72;&#x4E86;&#x4EC0;&#x4E48;.

Original: https://www.cnblogs.com/apollo1616/p/10440109.html
Author: 阿波罗Apollo
Title: 7.Git工作区和暂存区

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/37629/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

最近整理资源【免费获取】:   👉 程序员最新必读书单  | 👏 互联网各方向面试题下载 | ✌️计算机核心资源汇总