Linux学习之路嵌入式 Linux C ARM 我用 Linux

和Leon一起从头学Git(三)

2019-01-08  本文已影响0人  Leon_Geo

一、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 README hello.php 

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

$ git status -s
A  README
A  hello.php

3、git commit

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

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

$ 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
# 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

<?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 命令用于将文件从暂存区撤回。

# 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 文件的修改并未提交。

$ 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 rm hello.php
rm'hello.php'
$ git commit
$ ls
README
$ git rm -f README
$ls
$ 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有两个主要的应用场景:

$ 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 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!';
+?>
上一篇 下一篇

猜你喜欢

热点阅读