Git開發實戰(二)之Git的幾個重要位置的概念

1、Git的幾種位置概念git

      1.Git中的幾種位置概念和流轉過程github

     

      (1)本地代碼:當咱們新建立的文件,尚未進行commit,且沒有add到待提交列表時的狀態;bash

      (2)待提交列表:當想對某個文件進行提交時,咱們首先須要將該文件add到待提交列表中,而後才能進行下一步的commit操做;gitlab

      (3)本地倉庫:就是在本地保存代碼版本信息的倉庫,執行commit操做後,代碼會放入到本地倉庫,可是還不會push到遠程倉庫中;學習

      (4)遠程倉庫:通常就是在一些代碼託管平臺(好比github,gitlab,碼雲)上保存的代碼版本信息的倉庫,在執行push操做後,纔會到達遠程倉庫中。版本控制

      案例:日誌

      (1)咱們仍是使用最開始建立的test_git目錄,首先咱們使用touch命令建立一個README文件,而後使用git status查看該文件的狀態,咱們能夠看到新建立的README文件是尚未放進待提交列表中的,只在本地代碼中,因此顯示爲紅色,仍是untracked狀態;code

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ touch README

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git status
On branch master

Initial commit

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

        README

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

      (2)咱們使用git add命令,將本地代碼中的README文件加到待提交列表中,咱們再次查看狀態,能夠看到本是紅色的README文件變成了綠色,如今README文件就變成待提交文件了;it

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git add README

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git status
On branch master

Initial commit

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

        new file:   README

      (3)咱們使用git commit -m命令將README文件提交到本地倉庫,-m是提交的備註信息參數,而後再次查看文件狀態,咱們會發現沒有untracked文件和待提交的文件了,是乾淨的;io

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git commit -m 'my first commit'
[master (root-commit) 786d191] my first commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git status
On branch master
nothing to commit, working tree clean

      (4)咱們再使用git log命令就能夠看到咱們的代碼提交記錄了;

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git log
commit 786d19198abd2c337041705ea5e7601a9f183c9d
Author: kevinShaw <aibinxiao@126.com>
Date:   Mon Oct 23 16:03:28 2017 +0800

    my first commit

      (5)咱們再建立一個history文件,而後將它提交到本地倉庫,而後將它刪除,咱們查看它的狀態,咱們能夠看到提示說,咱們刪除了一個文件可是尚未提交這個修改,因此git本地倉庫能夠和本地代碼進行對比,肯定咱們作出了哪些修改,咱們再次提交,咱們就看不到這個提示了。

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ touch history

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git add history

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git commit -m 'commit the histoty file'
[master 2a34170] commit the histoty file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 history

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git rm history
rm 'history'

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    history

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git commit -m 'delete history file'
[master a1df991] delete history file
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 history

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git status
On branch master
nothing to commit, working tree clean

2、Git的基本操做命令

      1.經過前面的學習和案例,咱們已經接觸了一些命令,咱們詳細的介紹下這些命令:

     (1)git init 表示將當前目錄加入到git的版本控制中去,前提時建立一個目錄,而且切換到這個目錄,而後使用' git init .  '命令,git是命令,init是git的二級命令,‘.’ 表示當前目錄;

      (2)git add 表示將本地代碼添加到待提交列表;

      (3)git commit 表示將待提交列表的文件提交到本地倉庫,實現git對該文件的版本控制;

      (4)git status 表示查看當前的狀態,能夠看到有沒有新建文件尚未add到待提交列表,有沒有待提交列表的文件尚未commit到本地倉庫,或者是修改了某個文件尚未commit到本地倉庫;

      (5)git log 查看提交的日誌,就能夠看到你的每一次提交的日誌信息

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git log
commit fa0f90dba090654136054d7ef029635f1b22dbd1
Author: kevinShaw <aibinxiao@126.com>
Date:   Mon Oct 23 16:49:19 2017 +0800

    提交文件message

commit 9cfc652c8d449fc6e5af9d1d5873a8018b457272
Author: kevinShaw <aibinxiao@126.com>
Date:   Mon Oct 23 16:48:41 2017 +0800

    刪除log

commit b4d75dd631122e7d76ec59743a419d11acf16e08
Author: kevinShaw <aibinxiao@126.com>
Date:   Mon Oct 23 16:47:03 2017 +0800

    提交log

commit a1df9915d4615ff8ec444bdf5f7d3833e43ff9d2
Author: kevinShaw <aibinxiao@126.com>
Date:   Mon Oct 23 16:11:03 2017 +0800

    delete history file

commit 2a341709777eef278dde116ab521161997923602
Author: kevinShaw <aibinxiao@126.com>
Date:   Mon Oct 23 16:07:37 2017 +0800

    commit the histoty file

commit 786d19198abd2c337041705ea5e7601a9f183c9d
Author: kevinShaw <aibinxiao@126.com>
Date:   Mon Oct 23 16:03:28 2017 +0800

    my first commit

      (6)git show +commit的隨機數 能夠查看具體某條提交的具體信息(只要隨機數前面幾位不相同的狀況下,可使用前6位進行查看),好比:

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git show fa0f90dba090654136054d7ef029635f1b22dbd1
commit fa0f90dba090654136054d7ef029635f1b22dbd1
Author: kevinShaw <aibinxiao@126.com>
Date:   Mon Oct 23 16:49:19 2017 +0800

    提交文件message

diff --git a/message b/message
new file mode 100644
index 0000000..e69de29


aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git show fa0f90
commit fa0f90dba090654136054d7ef029635f1b22dbd1
Author: kevinShaw <aibinxiao@126.com>
Date:   Mon Oct 23 16:49:19 2017 +0800

    提交文件message

diff --git a/message b/message
new file mode 100644
index 0000000..e69de29

      (7)git checkout 能夠將上一次提交的內容進行回退

      git checkout命令的使用:

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ touch message

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git add message

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git commit -m '提交文件message'
[master fa0f90d] 提交文件message
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 message

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ rm message

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    message

no changes added to commit (use "git add" and/or "git commit -a")

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git checkout message

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git status
On branch master
nothing to commit, working tree clean

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ ls
message  README

 

本文爲原創文章,若是對你有一點點的幫助,別忘了點贊哦!比心!如需轉載,請註明出處,謝謝!

相關文章
相關標籤/搜索