和Leon一起从头学Git(三)

作者: Leon_Geo | 来源:发表于2019-01-08 06:26 被阅读0次

一、Git 基本操作

上一节,我们讲解了git init 和git clone命令,他们分别用来在本地初始化一个Git目录资源及从远端库直接复制过来一个Git资源。那么Git到底是做什么的呢?
Git的工作就是创建和保存你项目的快照及与之后的快照进行对比。今天我们就来讲解创建,提交,修改,撤回快照的命令。

1、git status

git status 用来查看自你上次提交之后,工作目录是否有修改。默认会详细输出比较内容,但如果添加了-s参数,可以获得简短的结果输出。

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   README
    new file:   hello.php
$ git status -s
     ??  README
     ??  hello.php

??:代表文件为新创建的
M:代表文件有改动
A:代表文件已添加到暂存区
AM:代表已添加到暂存区的文件又有了修改

2、git add

git add 命令作用为将该文件添加到缓存。

  • 首先我们创建以下两个文件:
$ touch README
$ touch hello.php

$ ls
README      hello.php

$ git status -s
?? README
?? hello.php
$ 
  • 接下来我们执行 git add 命令来添加文件:
$ git add README hello.php 

$ git status -s
A  README
A  hello.php
$ 
  • 我们修改 README 文件:
    $ vim README
    在 README 添加以下内容:# Leon Git 测试,然后保存退出。再执行一下 git status:
$ git status -s
AM README
A  hello.php
  • 再执行git add命令将其添加到缓存中。新项目中,添加所有文件很普遍,我们可以使用git add.命令来添加当前项目的所有文件。
$ git add .

$ git status -s
A  README
A  hello.php

3、git commit

git add 命令将想要快照的内容写入缓存区, 而git commit 将缓存区内容添加到本地仓库中。

Git 为你的每一个提交都记录你的名字与电子邮箱地址,所以如果还没有配置你的用户名和邮箱地址,还需要首先配置一下,方法在系列一中已有讲述。

  • 在将 README和hello.php 这两个文件加入了缓存区之后,接下来就对他们进行提交。在首个例子中,我们使用 -m 选项以在命令行中提供提交注释。
$ git add README hello.php 
$ git status -s
A  README
A  hello.php

$ git commit -m '第一次版本提交'
[master (root-commit) d32cf1f] 第一次版本提交
 2 files changed, 4 insertions(+)
 create mode 100644 README
 create mode 100644 hello.php
  • 现在我们已经记录了快照。如果我们再执行 git status:
$ git status
# On branch master
nothing to commit (working directory clean)

以上输出说明我们在最近一次提交之后,没有做任何改动,是一个"working directory clean:干净的工作目录"。

如果你没有设置 -m 选项,Git 会尝试为你打开一个编辑器以填写提交信息。 如果 Git 在你对它的配置中找不到相关信息,默认会打开 vim。屏幕会像这样:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# modified:   hello.php
#
~
~
".git/COMMIT_EDITMSG" 9L, 257C

如果你觉得 git add 提交缓存的流程太过繁琐,Git 也允许你用 -a 选项跳过这一步。命令格式为:git commit -a

  • 我们先修改 hello.php 文件为以下内容:
<?php
echo 'hello world!';
echo 'hello world!';
?>

再执行以下命令:

git commit -am '修改 hello.php 文件'
[master 71ee2cb] 修改 hello.php 文件
 1 file changed, 1 insertion(+)

4、git reset

git reset HEAD 命令用于将文件从暂存区撤回。

  • 我们先改动文件 README 文件,内容如下:
# Leon Git 测试
# hello world!

hello.php 文件修改为:

<?php
echo 'hello world!';
echo 'helloworld!';
echo 'helloworld!';
?>
  • 现在两个文件修改后,都提交到了缓存区,我们现在要取消其中一个的缓存,操作如下:
$ git status -s
 M README
 M hello.php

$ git add .
$ git status -s
A  README
A  hello.php

$ git reset hello.php 
Unstaged changes after reset:
??  hello.php

$ git status -s
A  README
?? hello.php

现在你执行 git commit,只会将 README 文件的改动提交,而 hello.php 是没有的。

$ git commit -m '修改'
[master f50cfda] 修改
 1 file changed, 1 insertion(+)

$ git status -s
 M hello.php

可以看到 hello.php 文件的修改并未提交。

  • 这时我们可以使用以下命令将 hello.php 的修改提交:
$ git commit -am '修改 hello.php 文件'
[master 760f74d] 修改 hello.php 文件
 1 file changed, 1 insertion(+)

$ git status
On branch master
nothing to commit, working directory clean

简而言之,执行 git reset 以取消之前 git add 添加,但不希望包含在下一提交快照中的缓存。

5、git rm

  • 如果只是简单地从工作目录中手工删除文件,运行 git status 时就会出现 Changes not staged for commit 的提示。
  • 要从 Git 中移除某个文件,就必须要从已跟踪文件清单中移除,然后提交。可以用git rm <file>命令完成此项工作。
    如我们删除当前工作目录中且还没暂存的hello.php文件:
$ git rm hello.php
rm'hello.php'
$ git commit
$ ls
README
  • 如果删除之前修改过并且已经放到暂存区域的话,即把工作目录和暂存区的文件都删除,则必须要用git rm -f <file>
$ git rm -f README
$ls
  • 如果把文件从暂存区域移除,但仍然希望保留在当前工作目录中,换句话说,仅是从跟踪清单中删除,使用 --cached 选项即可
$ git rm --cached README
rm 'README'
$ls
README
  • 递归删除,即如果后面跟的是一个目录做为参数,则会递归删除整个目录中的所有子目录和文件:
git rm –r *

进入某个目录中,执行此语句,会删除该目录下的所有文件和子目录。

6、git mv

git mv 命令用于移动或重命名一个文件、目录、软连接。

我们先把刚移除的 README 添加回来:

$ git add README 

然后对其重名:

$ git mv README  README.md
$ ls
README.md

7、git diff

git status显示你上次提交更新后的更改或者写入缓存的改动,而gitdiff一行一行地显示这些改动具体是啥。
gitdiff有两个主要的应用场景:

  • git diff 命令显示工作区与暂存区的改动。但若显示摘要而非整个diff,可以添加--stat选项。
    修改hello.php文件,添加以下内容:
$ cat <EOF
   <?php
   echo 'hello world!';
   ?>
EOF >>hello.php

$git status -s
A     README
AM  hello.php

$git diff
diff --git a /hello.php b /hello.php
indexe 69de29..69b5711100644
---    a/hello.php
+++ b/hello.php
@@-0,0+1,3@@
+<?php
+echo 'hello world!';
+?>
  • git diff --cached命令显示当前暂存区中文件最近一次的改动.
    接下来我们来查看下 git diff --cached 的执行效果:
$ git add hello.php 
$ git status -s
A  README
A  hello.php

$ git diff --cached
diff --git a/README b/README
new file mode 100644
index 0000000..8f87495
--- /dev/null
+++ b/README
@@ -0,0 +1 @@
+# Leon Git 测试
diff --git a/hello.php b/hello.php
new file mode 100644
index 0000000..69b5711
---       /dev/null
+++ b/hello.php
@@ -0,0 +1,3 @@
+<?php
+ echo 'hello world!';
+?>

相关文章

  • 和Leon一起从头学Git(三)

    一、Git 基本操作 上一节,我们讲解了git init 和git clone命令,他们分别用来在本地初始化一个G...

  • 和Leon一起从头学Git(四)

    Git 分支管理 几乎每一种版本控制系统都以某种形式支持分支。使用分支意味着你可以从开发主线上分离开来,然后在不影...

  • 和Leon一起从头学Git(二)

    一、使用Git的一般情景 从远程库repo克隆Git项目资源作为工作目录。 在克隆的资源上添加或修改文件。 如果其...

  • 和Leon一起从头学Git(一)

    一、Git 的安装与配置 Git 目前支持 Linux/Unix、Solaris、Mac和 Windows 平台上...

  • 和Leon一起从头学Git(五)

    一、提交历史 在使用Git提交了若干更新之后,又或者克隆了某个项目,想回顾下提交历史,我们可以使用gitlog命令...

  • 和leon一起从头学Git(五)

    一、提交历史 在使用Git提交了若干更新之后,又或者克隆了某个项目,想回顾下提交历史,我们可以使用gitlog命令...

  • 和leon一起学Vim

    为何学vim? 所有的UnixLike系统都会内建vi文书编辑器,其他的文书编辑器则不一定会存在。 但是目前我们使...

  • Git 终端命令从头学

    1.cd到目标文件夹,执行 git init 创建新仓库 2.使用git add把文件添加到仓库,可以添加单个文件...

  • 从头开始学Git

    常用命令 git init 初始化git仓库git add 添加文件git status 查看...

  • 前端书籍

    Nodejs应用 《《一起学Koa》》 Git和Github 《《Git教程》》 ES6 《《ES6教程》》

网友评论

    本文标题:和Leon一起从头学Git(三)

    本文链接:https://www.haomeiwen.com/subject/yxxerqtx.html