Git的基本操作

2020-07-01  本文已影响0人  JiangCheng97

知识来源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 diff ——查看更改前后的差别

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

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

上一篇 下一篇

猜你喜欢

热点阅读