Git基礎

一、git install


Ubuntu

sudo apt-get install git

Centos

yum install git  

Mac

brew install git

二、git initialization configure


git config --global user.name "Your Name"

git config --global user.email "[email protected]"
注意git config命令的--global參數,用了這個參數,表示你這臺機器上所有的Git倉庫都會使用這個配置,當然也可以對某個倉庫指定不同的用戶名和Email地址

三、git create repository

  • git版本庫,也叫倉庫,英文爲repository,可以理解爲一個目錄,目錄裏的文件都可以被git管理起來,每個文件的修改、刪除Git都能跟蹤,以便任何時候都能夠追蹤歷史,或者將來在某個時刻可以還原。
1、創建一個目錄

$ mkdir gitrepo

$ cd gitrepo

$ pwd

/Users/antony/gitrepo

注意:目錄不要包含中文

2、通過 git init 命令把這個目錄變成Git可以管理的倉庫

$ git init

Initialized empty Git repository in /Users/antony/gitrepo/.git/

瞬間Git就把倉庫建好了,而且告訴你是一個空的倉庫(empty Git repository),細心的讀者可以發現當前目錄下多了一個.git的目錄,這個目錄是Git來跟蹤管理版本庫的,沒事千萬不要手動修改這個目錄裏面的文件,不然改亂了,就把Git倉庫給破壞了。

四、提交一個文件到倉庫

1、創建一個文件

$ vim readme.txt

This is a test file

Git is a opensource app

此文件一定要放在剛纔創建的倉庫下,即gitrepo

2、用命令git add將文件添加到倉庫

$ git add readme.txt
3、用命令git commit告訴Git,把文件提交到git倉庫

$ git commit -m "up a readme file"

[master (root-commit) ba4d5a3] up a readme file

 1 file changed, 2 insertions(+)

 create mode 100644 readme.txt

-m 添加描述,一般用來告訴此次提交的說明

爲什麼添加文件需要add,commit兩步呢? 因爲commit一次可以提交多個文件,所以你可以多次add不同文件,比如


$ git add file1

$ git add file2 file3

$ git commit -m "file2 file3"

五、Git Status、diff(狀態、對比)

1、Status

修改文件


$ vim readme.txt #在最後一行新添加一行

New line

查看狀態


$ 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 status 時刻查看文件的狀態,通過返回信息告訴我們文件被修改了

2、diff(查看具體修改內容)

$ git diff readme.txt

diff --git a/readme.txt b/readme.txt

index 194380c..e8e4b8b 100644

--- a/readme.txt

+++ b/readme.txt

@@ -1,2 +1,3 @@

 This is a test file

 Git is a opensource app

+New line

(END)
3、查看add及commit後的狀態

git add後的狀態


$ 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後的狀態


$ git commit -m "add new line"

[master a782d05] add new line

 1 file changed, 1 insertion(+)

$ git status

On branch master

nothing to commit, working tree clean

六、版本回退

先修改一次文件


$ vim readme.txt

New line 2

提交文件


$ git add readme.txt

$ git commit -m "New line 2"

[master def33d7] New line 2

 1 file changed, 1 insertion(+)
1、版本提交歷史記錄git log

$ git log

commit def33d73a1ded1b673f2b994bddedce9f8409a33

Author: ZAntony <[email protected]>

Date:   Sun Dec 11 22:36:53 2016 +0800

    New line 2

commit a782d05f438ca21a2d2336e97256bf9d7e35c058

Author: ZAntony <[email protected]>

Date:   Sun Dec 11 22:19:43 2016 +0800

    add new line

commit ba4d5a316c0e915e768d0a793999ff9f5f41447b

Author: ZAntony <[email protected]>

Date:   Sun Dec 11 22:00:13 2016 +0800

    up a readme file

git 最近顯示的是New line 2,上一次是add new line,第一次是up a readme file

可以通過--pretty=oneline參數控制輸出內容的數量


$ git log --pretty=oneline

def33d73a1ded1b673f2b994bddedce9f8409a33 New line 2

a782d05f438ca21a2d2336e97256bf9d7e35c058 add new line

ba4d5a316c0e915e768d0a793999ff9f5f41447b up a readme file

前面的一串數字是16進制的commit id

2、git reset 版本回退

首先,Git必須知道當前版本是哪個版本,在Git中,用HEAD表示當前版本,也就是最新的提交def33d........9a33(注意我的提交ID和你的肯定不一樣),上一個版本就是HEAD^,上上一個版本就是HEAD^^,當然往上100個版本寫100個^比較容易數不過來,所以寫成HEAD~100


$ git reset --hard HEAD^

HEAD is now at a782d05 add new line

This is a test file

Git is a opensource app

New line

# 此時版本已經回退
3、當返回到上一個版本後,突然想返回到爲返回前的版本,即New line 2

查看日誌,發現並未找到回退前的版本的commit id


$ git log

commit a782d05f438ca21a2d2336e97256bf9d7e35c058

Author: ZAntony <[email protected]>

Date:   Sun Dec 11 22:19:43 2016 +0800

    add new line

commit ba4d5a316c0e915e768d0a793999ff9f5f41447b

Author: ZAntony <[email protected]>

Date:   Sun Dec 11 22:00:13 2016 +0800

    up a readme file
當通過git reset --hard HEAD^恢復到add new line時,在想恢復到之前的New line 2,就必須找到他的commit id,可以通過查看之前的命令歷史來查找,也可以通過git reflog來查看commit id

git reflog


$ git reflog # 通過命令來查看commit id,id是不完整的,但是可以使用,只要保證id唯一性。

a782d05 HEAD@{0}: reset: moving to HEAD^

def33d7 HEAD@{1}: commit: New line 2

a782d05 HEAD@{2}: commit: add new line

ba4d5a3 HEAD@{3}: commit (initial): up a readme file

恢復版本


$ git reset --hard def33d7

HEAD is now at def33d7 New line 2

$ cat readme.txt

This is a test file

Git is a opensource app

New line

New line 2
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章