1、建立新倉庫
git init
touch test.txt
git add --a
git commit -m "fist commit"
初始化新倉庫,在目前目錄下由一個.git的目錄,所有git需要的資料和資源都放在這個目錄中,在當面目錄下添加檔案後,需要通過git add 添加到檔案追蹤管理(添加到暫存區,資料存放在.git/index 目錄索引,資料内部儲存在.git/objects 中), git commit -m "送出說明備注" 送出的資訊會送出到資料倉庫,資料送出到正式倉庫,具體儲存在.git/objects 中,如以上送出會包含一個commit,tree ,blob 對象。
2、從現有倉庫克隆
git clone url
git clone [email protected]:torvalds/linux.git
如從gitHub上克隆一份linux的源碼,不僅是克隆最新版本的源碼,還克隆了所有資料倉庫的曆史版本,每個檔案的每一個版本,這個時候及時伺服器github 發生故障,可以用本地資料倉庫重建伺服器上的倉庫。可以回複到從伺服器克隆或最後更一次從伺服器拉去的狀态。在.git 目錄中,已經儲存了所有版本記錄,本地檔案夾即工作目錄的所有檔案删除了,然後從中取出最新版本的檔案拷貝。
3、檢查檔案更新狀态
要求确定目前工作區和暫存區檔案的狀态,可以通過git status 指令。在工作區和暫存區的目錄狀态可以檢視。
git status
On branch master nothing to commit, working directory clean
目前在預設master 分支,目前工作目錄和暫存區沒有任何跟蹤的檔案,也沒有任何檔案送出後更改,也沒有新增加,未被跟蹤的檔案。
notepad test.txt
notepad t.txt
修改test.txt檔案,新添加一個t.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 di
#
# modified: test.txt
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
# t.txt
no changes added to commit (use "git add" and/or "git commit -a")
新增加的檔案t.txt 在未跟蹤檔案範圍Untracked files 範圍,需要通過git add 把改檔案添加的暫存區,歸入的版本跟蹤管理。
4、 添加檔案到暫存區
$ git add .
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
# modified: h.txt
# new file: test.txt
git add . 把目前所有目錄檔案所有新檔案和修改檔案添加到暫存區,如果test.txt 檔案提示是 Changes not staged for commit ,說明此跟蹤檔案已經發生修改,但是還未添加到暫存區,把修改的檔案通過git add 指令添加到暫存區後。提示Changes to be committed. 檔案已經暫存,随時可以送出到倉庫 。h.txt 新添加檔案從未跟蹤狀态Untracked files ,通過git add指令添加到暫存區,已加入跟蹤檔案的範圍。
5、版本送出
$ git commit -m "this is test commit"
[master d4a498a] this is test commit
git commit --amend --reset-author
2 files changed, 3 insertions(+)
create mode 100644 t.txt
通過git commit -m "xxx" 将目前暫存區的内容送出到倉庫,本次commit 送出檔案是在預設master分支,送出commit 對象存放在.git/objects 的d4/1498a... 的檔案中,該檔案指向一個樹tree對象。
6、檢視目前送出日志記錄
$ git log
commit d4a498a197c24421acee5c5ff96cfbc7e5c3be9e
Author: andy<[email protected]>
Date: Sat Mar 8 14:23:37 2014 +0800
this is test commit
commit 80071e48614361dc282473d6482e3faa9fec17d9
Author:andy<[email protected]>
Date: Sat Mar 8 13:35:13 2014 +0800
1st commit
git log 指令檢視目前版本
7、檔案差異比較
工作區和暫存區檔案比較用git diff 指令,暫存區和最近一天送出版本之間的差異,可以用git diff --cached/staged. 如下:
目前在test.txt 檔案隻有1111111 檔案内容,我在檔案test.txt中添加22222222等内容,比較目前暫存區和工作檔案差異
$ notepad test.txt
$ git diff
diff --git a/test.txt b/test.txt
index 0147537..f33d264 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
11111111111111
+22222222222222
# (use "git checkout -- <file>..." to discard changes in working d
$ git diff --staged
可以發現工作區比暫存區test.txt檔案多增加了22222222222222 内容。暫存區和資料倉庫内容是完全相同的。同時看看目前工作區狀态。現在我們吧剛剛修改的内容添加到暫存區,同時比較暫存區和資料倉庫檔案差異。
$ git add test.txt
把工作區修改的内容更新到暫存區後,可以看出此時暫存區和工作區檔案完全相同。狀态是是已暫存,帶送出狀态。與此同時,我們可以比較暫存區和資料殘酷之間的差異和比較。
我們可以很清楚的看到目前暫存區和資料倉庫版本比較。暫存區test.txt 内容比最近一次送出内容多22222222222222 一行資料。送出資料到資料倉庫。我們現在可以把工作區目錄和資料倉庫比較,看看test.txt 直接的檔案内容差異。
$ git diff head
可以很立即看出工作區檔案内容比較倉庫有新修改的内容。此時我們送出更新所有檔案都沒有差異了。看看檔案差異。
$ git commit -m "test git diff "
[master fc0166f] test git diff
Committer: andy<[email protected]>
1 file changed, 1 insertion(+)
8、檔案删除和移動
所有的工作區rm xxx删除後,可以直接從資料倉庫擷取最近一次送出版本的内容 git rm xxx。直接從資料倉庫删除此檔案内容。
$ ls
h.txt test.txt
$ rm h.txt
test.txt
diff --git a/h.txt b/h.txt
deleted file mode 100644
index 456f979..0000000
--- a/h.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-welcome to here
-very good
可以通過文某比較目前的工作目錄h.txt 檔案已經被删除了,工作區目錄和暫存區比較檔案差異也可以返現檔案删除模式。接下來删除暫存區目錄
$ git rm h.txt
rm 'h.txt'
-
# deleted: h.txt
通過删除暫存區檔案前後可以比較出檔案的差異。此時檔案也不在跟蹤狀态。還有我們僅僅希望删除暫存區的内容,不删除工作區的内容
git rm --cached -f h.txt 的方式來實作,特别是針對工作區有修改,要删除暫存區内容。
$ git reset --hard
HEAD is now at fc0166f test git diff
$ git rm --cached h.txt
$ git diff -cached
error: invalid option: -cached
$ git diff --cached h.txt
移動
git 移動檔案操作是是相對先移動複制一個新檔案,删除原檔案,添加新檔案到跟蹤範圍。
$ git mv h.txt d.txt 結果是等同于,相當于以下三條指令
d.txt test.txt
$ mv d.txt to.txt
$ git rm d.txt
rm 'd.txt'
$ git add to.txt
9、檢視送出曆史
commit fc0166f53a45cfc4c17079e5e3454fb7e3136cb6
Date: Sat Mar 8 15:52:10 2014 +0800
test git diff
commit d6eab3a38aee0b25ac395899c82edd48723a2ea9
Date: Sat Mar 8 15:36:53 2014 +0800
enforce commit'
commit 85ec024140442df6db8625a07c2ee7369cceb704
Date: Sat Mar 8 15:35:44 2014 +0800
com 3
git log 檢視送出曆史,git log -p -n 最近n 次送出的内能和差異。檢視曆史記錄統計資訊 git log --stat,檢視送出的曆史記錄格式可自由選擇。
$ git log --pretty=format:"%h - %an, %ar : %s"
fc0166f - yyf, 48 minutes ago : test git diff
d6eab3a - yyf, 63 minutes ago : enforce commit'
85ec024 - yyf, 65 minutes ago : com 3
d4a498a - unknown, 2 hours ago : this is test commit
80071e4 - unknown, 3 hours ago : 1st commit
列出了常用的格式占位符寫法及其代表的意義。
選項 說明
%H 送出對象(commit)的完整哈希字串
%h 送出對象的簡短哈希字串
%T 樹對象(tree)的完整哈希字串
%t 樹對象的簡短哈希字串
%P 父對象(parent)的完整哈希字串
%p 父對象的簡短哈希字串
%an 作者(author)的名字
%ae 作者的電子郵件位址
%ad 作者修訂日期(可以用 -date= 選項定制格式)
%ar 作者修訂日期,按多久以前的方式顯示
%cn 送出者(committer)的名字
%ce 送出者的電子郵件位址
%cd 送出日期
%cr 送出日期,按多久以前的方式顯示
%s 送出說明
$ git log --pretty=format:"%h %s" --graph
* 78d907a dev branch commit
* fc0166f test git diff
* d6eab3a enforce commit'
* 85ec024 com 3
* d4a498a this is test commit
* 80071e4 1st commit
選項 說明
-p 按更新檔格式顯示每個更新之間的差異。
--stat 顯示每次更新的檔案修改統計資訊。
--shortstat 隻顯示 --stat 中最後的行數修改添加移除統計。
--name-only 僅在送出資訊後顯示已修改的檔案清單。
--name-status 顯示新增、修改、删除的檔案清單。
--abbrev-commit 僅顯示 SHA-1 的前幾個字元,而非所有的 40 個字元。
--relative-date 使用較短的相對時間顯示(比如,“2 weeks ago”)。
--graph 顯示 ASCII 圖形表示的分支合并曆史。
--pretty 使用其他格式顯示曆史送出資訊。可用的選項包括 oneline,short,full,fuller 和 format(後跟指定格式)
10、撤銷操作
可以修改最後一次送出的内容,當你發現最近一次送出内容不正确時候,可以通過 git commit --amend 修改
$ git commit --amend -m "modify commit"
[master c660522] modify commit
$ git log --oneline
c660522 modify commit
fc0166f test git diff
d6eab3a enforce commit'
85ec024 com 3
d4a498a this is test commit
80071e4 1st commit
11、取消檔案修改
git reset | git reset head 将head指向的目錄樹重置的暫存區
git reset --soft head^ 工作區和暫存區不變,但是引用向前回退一次,當對最新送出不滿意的時候,撤銷最新送出以便更改
git commit -e -F .git/COMMIT_EDITMSG 以上兩個指令相當于git commit --amend
git reset head^
git reset --mixed head^ 暫存區和引用回退到上一次送出之前
git reset --hard head^ 引用 工作區 暫存區 徹底消除