Git 基本操作

git init 初始化仓库

$ mkdir Hello-Git
$ cd Hello-Git
$ git init
Initialized empty Git repository in /Users/william/git/Hello-GitHub/Hello-Git/.git/

git status 查看仓库的状态

$ touch README.md
$ git status
On branch master

Initial commit

Untracked files:
  (use "git add ..." to include in what will be committed)

    README.md

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

git add 向暂存区中添加文件

$ git add README.md 
$ git status
On branch master

Initial commit

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

    new file:   README.md

git commit 保存仓库的历史记录

$ git commit -m "First commit"
[master (root-commit) e57ce85] First commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md

如果希望记录详细信息,在使用 commit 指令时,不加 -m 即可,注释的通用写法是:

  • 第一行为简述
  • 第二行是留空
  • 第三行开始写详细描述,如修改原因,修改内容等

如果希望中止提交,将提交信息留空,直接关闭编辑器即可


git log 查看提交日志

$ git log
commit e57ce85ae7ce74318e7424e05bc508b06ea7c6dc
Author: JannyHo 
Date:   Tue May 16 11:00:38 2017 +0800

    First commit
  • --pretty=short 只查看一行提交信息
  • -p filename 查看某个文件的改动

git diff 查看更改前后的差别

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

  • 如果 diff 后面不带参数,查看的是 工作树和暂存区的差别,如果执行了 git add 命令把工作树的内容添加到暂存区后,再执行 diff 命令,将会看不到任何信息:
$ git add README.md 
$ git diff
$ 

这个时候,需要执行 git diff HEAD 命令查看 工作树与上次提交之间的差别 :

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

你可能感兴趣的:(Git 基本操作)