Git 的简单使用

创建版本库

  • 创建一个空文件夹作为 Git 版本库,可以以 pwd 查看当前目录:
    $ mkdir repository   
    $ cd repository/   
    $ pwd   
    /Users/Joker/repository
  • 初始化 Git ,会在目录下生成一个 .git 的隐藏文件夹,可以以 ls -ah 查看:

    $ git init   
    Initialized empty Git repository in /Users/Joker/repository/.git/   
    $ ls -ah   
    .    ..    .git
  • 新建一个 readme.txt 文件,并输入 “This is a readme file.” ,:wq 保存并退出,以 cat 查看文件:

    $ vi readme.txt   
    $ cat readme.txt   
    This is a readme file.
  • 将新建的 readme.txt 文件添加到工作区:

    $ git add readme.txt
  • git commit 告诉 Git ,把文件提交到仓库:

    $ git commit -m "Add a readme file."   
    [master (root-commit) 10593c0] Add a readme file.   
     1 file changed, 1 insertion(+)   
     create mode 100644 readme.txt   

查看修改状态

  • 继续修改 readme.txt 文件,添加 “Learn how to view the current status.”:

    $ vi readme.txt   
  • 查看当前状态:

    $ git status   
    On branch master   
    Changes not staged for commit:   
      (use "git add <file>..." to update what will be committed)   
      (use "git checkout -- <file>..." to discard changes in working directory)
    
      modified:   readme.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
  • 可以清楚地看到 Git 告诉我们 readme.txt 文件被修改了,git diff 查看具体修改内容:

    $ git diff   
    diff --git a/readme.txt b/readme.txt   
    index d344129..cff7032 100644   
    --- a/readme.txt   
    +++ b/readme.txt   
    @@ -1 +1,3 @@   
     This is a readme file.   
    +   
    +Learn how to view the current status.
  • 将 readme.text 提交到工作区:

    $ git add readme.txt   
  • 再次查看状态:

    $ git status   
    On branch master   
    Changes to be committed:   
      (use "git reset HEAD <file>..." to unstage)   
    
       modified:   readme.txt
  • 提示告知我们提交修改:

    $ git commit -m "Learn how to view the current status."   
    [master 1410245] Learn how to view the current status.   
     1 file changed, 2 insertions(+)  

版本回退

  • 再次修改 readme.txt 文件,添加 “Learn how to rollback the version.” 并提交:

    $ vi readme.txt   
    $ git add readme.txt   
    $ git commit -m "Learn how to rollback the version".  
    [master c2ae0fb] Learn how to rollback the version.   
     1 file changed, 2 insertions(+)
  • 查看日记:

    $ git log   
    commit c2ae0fbc27acf5463417d4ee2f3b1258337fe26f   
    Author: ZJQ-Joker <zjq_joker@163.com>   
    Date:   Wed Aug 10 19:57:22 2016 +0800   
    
       Learn how to rollback the version.
    
    commit 14102454a9156d43b13838f0c8a8dc5e5db417dd   
    Author: ZJQ-Joker <zjq_joker@163.com>   
    Date:   Wed Aug 10 19:52:36 2016 +0800
    
       Learn how to view the current status.
    
    commit 10593c028451209db0fea36598e21e51294a92bf   
    Author: ZJQ-Joker <zjq_joker@163.com>   
    Date:   Wed Aug 10 14:56:37 2016 +0800
    
       Add a readme file.
  • 查看单行日记:

    $ git log --pretty=oneline   
    c2ae0fbc27acf5463417d4ee2f3b1258337fe26f Learn how to rollback the version.   
    14102454a9156d43b13838f0c8a8dc5e5db417dd Learn how to view the current status.   
    10593c028451209db0fea36598e21e51294a92bf Add a readme file.

    前面的一大串数字是 commit id 而后面的则是你 commit 时添加的描述。

  • 回退至上一版本:

    $ git reset --hard HEAD^
    HEAD is now at 1410245 Learn how to view the current status.
  • 查看是否回退至上一版本:

    $ cat readme.txt 
    This is a readme file.
    
    Learn how to view the current status.
  • 再查看日志,之前的日志已经消失:

    $ git log
    commit 14102454a9156d43b13838f0c8a8dc5e5db417dd
    Author: ZJQ <Joker@a110-232-153-192.deploy.akamaitechnologies.com>
    Date:   Wed Aug 10 19:52:36 2016 +0800
    
        Learn how to view the current status.
    
    commit 10593c028451209db0fea36598e21e51294a92bf
    Author: ZJQ-Joker <zjq_joker@163.com>
    Date:   Wed Aug 10 14:56:37 2016 +0800
    
        Add a readme file.
    
  • 通过 commit id 回退至指定版本,commit id 可以简写:

    $ git reset --hard 10593
    HEAD is now at 10593c0 Add a readme file.
  • 再查看是否已回退至指定 commit id 的版本:

    $ cat readme.txt 
    This is a readme file.
    $ git log
    commit 10593c028451209db0fea36598e21e51294a92bf
    Author: ZJQ-Joker <zjq_joker@163.com>
    Date:   Wed Aug 10 14:56:37 2016 +0800
    
        Add a readme file.
    
  • 若是想看之前的 commit id ,回退至后面修改的版本可以:

    $ git reflog
    10593c0 HEAD@{0}: reset: moving to 10593
    1410245 HEAD@{1}: reset: moving to HEAD^
    c2ae0fb HEAD@{2}: commit: Learn how to rollback the version.
    1410245 HEAD@{3}: commit: Learn how to view the current status.
    10593c0 HEAD@{4}: commit (initial): Add a readme file.
  • 回退至 commit id 为 c2ae0 的版本:

    $ git reset --hard c2ae0
    HEAD is now at c2ae0fb Learn how to rollback the version.