Git的基本操作

知识来源GitHub入门与实践

git init ——初始化仓库

msi@DESKTOP-RNI6GS3 MINGW64 /g
$ mkdir git-tutorial

msi@DESKTOP-RNI6GS3 MINGW64 /g
$ cd git-tutorial/

msi@DESKTOP-RNI6GS3 MINGW64 /g/git-tutorial
$ git init
Initialized empty Git repository in G:/git-tutorial/.git/

如果初始化成功,执行了git init 命令会生成.git目录。在Git中,我们将这个目录的内容称为“附属于该仓库的工作树”。文件的编辑等操作在工作树中进行,然后记录到仓库中,以此来管理文件的历史快照。如果想将文件恢复到原先的状态,可以从仓库中调取之前的快照,在工作树种打开。

git status —— 查看仓库状态

msi@DESKTOP-RNI6GS3 MINGW64 /g/git-tutorial (master)
$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

结果显示了当前正处于master分支下,还显示了当前没有可以提交的内容。

msi@DESKTOP-RNI6GS3 MINGW64 /g/git-tutorial (master)
$ touch README.md

msi@DESKTOP-RNI6GS3 MINGW64 /g/git-tutorial (master)
$ git status
On branch master

No commits yet

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

nothing added to commit but untracked files present (use "git add" to track)

在Untrack files 中显示了README.md文件。只要对Git的工作树或者仓库进行操作,git status就会进行变化。

git add —— 向暂存区中添加文件

如果只是用Git仓库的工作树创建了文件,那么该文件不会被记入Git仓库的版本管理对象中。所以,git status时会看到README.md会显示在Untracked files里。

想要文件成为Git仓库的管理对象,就需要用git add命令将其加入暂存区(Stage或Index)。暂存区是提交之前的临时区域。

msi@DESKTOP-RNI6GS3 MINGW64 /g/GitDirectory/git-tutorial (master)
$ git add README.md

msi@DESKTOP-RNI6GS3 MINGW64 /g/GitDirectory/git-tutorial (master)
$ git status
On branch master

No commits yet

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

git commit——保存仓库的历史记录

git commit 命令可以将当前暂存区的文件实际保存到仓库的历史记录中。通过这些记录,我们可以在工作树中复原这些文件。

  • 记述一行提交信息
msi@DESKTOP-RNI6GS3 MINGW64 /g/GitDirectory/git-tutorial (master)
$ git commit -m "first commit"
[master (root-commit) a2695ea] first commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md

-m参数后的"First commit" 称为提交信息,是对这个提交的概述。

  • 记述详细的提交信息

直接使用git commit 不用加 -m

git log ——查看提交日志

git log命令可以查看以往提交的的日志。

$ git log
commit 1ba27d6c696f5c1fe66c1b40df7397db9e55f83d (HEAD -> master)
Author: JiangCheng <zhou19970407@126.com>
Date:   Wed Jul 1 22:53:10 2020 +0800

    first commit

    try git commit

commit a2695eaa5d8af91565ef675937b4b27d3acef53a
Author: JiangCheng <zhou19970407@126.com>
Date:   Wed Jul 1 22:41:32 2020 +0800

    first commit

commit 旁边显示了显示的“1ba27d6c69.....”是指向这个提交的哈希值。Git的其他命令中,在指向提交时会用到这个哈希值。

Author 显示我们给Git设置的用户名和邮箱地址。Date栏显示的是提交执行的日期和时间。

最后就是提交的信息

  • 只显示提交信息的第一行

    可以在git log命令加上 --pretty=short。

  • 只显示指定目录、文件的日志

    git log 后面加上目录名或文件名

  • 显示文件的改动

    如果想查看文件提交带来的改动,可以加上 -p参数。

git diff ——查看更改前后的差别

git diff命令可以查看工作树、暂存区、最新提交之间的差别。

在刚刚提交的md文件里写点东西

  • 查看工作树和暂存区的差别

    执行git diff命令,查看当前工作树和暂存区的差别

    $ git diff
    diff --git a/README.md b/README.md
    index e69de29..b52d3b6 100644
    --- a/README.md
    +++ b/README.md
    @@ -0,0 +1,2 @@
    +# Git教程
    +
    

    由于我们尚未使用 git add命令向暂存区添加任何东西,所以程序只会显示工作树和最新提交状态之间的差别。

    "+"标出的是新添加的行,"-"标出的是被删除的行。

    用git add 将README.md加入暂存区。

    $ git add README.md
    
  • 查看工作树和最新提交的差别

    如果现在执行git diff命令,由于工作树和暂存区没有差别,结果什么都没有显示,要查看最新提交的差别,请执行以下命令。

    $ git diff head
    diff --git a/README.md b/README.md
    index e69de29..b52d3b6 100644
    --- a/README.md
    +++ b/README.md
    @@ -0,0 +1,2 @@
    +# Git教程
    +
    

    养成一个好习惯,在执行git commit 命令之前,先执行git commit HEAD命令查看本次提交的和上次提交的区别,确认之后再提交。这里的HEAD 是指向当前分支中最新一次提交的指针。

    运行 git commit

    $ git commit -m "Add index"
    [master 75816af] Add index
     1 file changed, 2 insertions(+)
    

    查看提交日志,确认是否提交成功。

    $ git log
    commit 75816af90c5a4e840330882f23a5e1d347f1b965 (HEAD -> master)
    Author: JiangCheng <zhou19970407@126.com>
    Date:   Thu Jul 2 23:20:25 2020 +0800
    
        Add index
    
    commit 1ba27d6c696f5c1fe66c1b40df7397db9e55f83d
    Author: JiangCheng <zhou19970407@126.com>
    Date:   Wed Jul 1 22:53:10 2020 +0800
    
        first commit
    
        try git commit
    
    commit a2695eaa5d8af91565ef675937b4b27d3acef53a
    Author: JiangCheng <zhou19970407@126.com>
    Date:   Wed Jul 1 22:41:32 2020 +0800
    
        first commit
    
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容