一、建立新倉庫 linux
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 對象。
二、從現有倉庫克隆
git clone url
git clone git@github.com:torvalds/linux.git
如從gitHub上克隆一份linux的源碼,不只是克隆最新版本的源碼,還克隆了全部數據倉庫的歷史版本,每一個文件的每個版本,這個時候及時服務器github 發生故障,能夠用本地數據倉庫重建服務器上的倉庫。能夠回覆到從服務器克隆或最後更一次從服務器拉去的狀態。在.git 目錄中,已經保存了全部版本記錄,本地文件夾即工做目錄的全部文件刪除了,而後從中取出最新版本的文件拷貝。
三、檢查文件更新狀態
要求肯定當前工做區和暫存區文件的狀態,能夠經過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 把改文件添加的暫存區,納入的版本跟蹤管理。
四、 添加文件到暫存區
$ git add .
$ git status
# On branch master
# 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命令添加到暫存區,已加入跟蹤文件的範圍。
五、版本提交
$ 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對象。
六、查看當前提交日誌記錄
$ git log
commit d4a498a197c24421acee5c5ff96cfbc7e5c3be9e
Author: andy<andy@gmail.com>
Date: Sat Mar 8 14:23:37 2014 +0800
this is test commit
commit 80071e48614361dc282473d6482e3faa9fec17d9
Author:andy<andy@gmail.com>
Date: Sat Mar 8 13:35:13 2014 +0800
1st commit
git log 命令查看當前版本
七、文件差別比較
工做區和暫存區文件比較用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
$ 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 d
#
# modified: test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
$ git diff --staged
能夠發現工做區比暫存區test.txt文件多增長了22222222222222 內容。暫存區和數據倉庫內容是徹底相同的。同時看看當前工做區狀態。如今咱們吧剛剛修改的內容添加到暫存區,同時比較暫存區和數據倉庫文件差別。
$ git add test.txt
$ git diff
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: test.txt
#
把工做區修改的內容更新到暫存區後,能夠看出此時暫存區和工做區文件徹底相同。狀態是是已暫存,帶提交狀態。與此同時,咱們能夠比較暫存區和數據殘酷之間的差別和比較。
$ git diff --staged
diff --git a/test.txt b/test.txt
index 0147537..f33d264 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
11111111111111
+22222222222222
咱們能夠很清楚的看到當前暫存區和數據倉庫版本比較。暫存區test.txt 內容比最近一次提交內容多22222222222222 一行數據。提交數據到數據倉庫。咱們如今能夠把工做區目錄和數據倉庫比較,看看test.txt 直接的文件內容差別。
$ git diff head
diff --git a/test.txt b/test.txt
index 0147537..f33d264 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
11111111111111
+22222222222222
能夠很當即看出工做區文件內容比較倉庫有新修改的內容。此時咱們提交更新全部文件都沒有差別了。看看文件差別。
$ git commit -m "test git diff "
[master fc0166f] test git diff
Committer: andy<andy@gmail.com>
git commit --amend --reset-author
1 file changed, 1 insertion(+)
$ git diff
$ git diff --staged
$ git diff head
八、文件刪除和移動
全部的工做區rm xxx刪除後,能夠直接從數據倉庫獲取最近一次提交版本的內容 git rm xxx。直接從數據倉庫刪除此文件內容。
$ ls
h.txt test.txt
$ rm h.txt
$ ls
test.txt
$ git diff
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 diff --staged
$ git rm h.txt
rm 'h.txt'
$ git diff --staged
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
-
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: h.txt
#
經過刪除暫存區文件先後能夠比較出文件的差別。此時文件也不在跟蹤狀態。還有咱們僅僅但願刪除暫存區的內容,不刪除工做區的內容
git rm --cached -f h.txt 的方式來實現,特別是針對工做區有修改,要刪除暫存區內容。
$ git reset --hard
HEAD is now at fc0166f test git diff
$ ls
h.txt test.txt
$ git rm --cached h.txt
rm 'h.txt'
$ git diff
$ git diff head
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
-
$ git diff -cached
error: invalid option: -cached
$ git diff --cached h.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
移動
git 移動文件操做是是相對先移動複製一個新文件,刪除原文件,添加新文件到跟蹤範圍。
$ ls
h.txt test.txt
$ git mv h.txt d.txt 結果是等同於,至關於如下三條命令
$ ls
d.txt test.txt
$ mv d.txt to.txt
$ git rm d.txt
rm 'd.txt'
$ git add to.txt
九、查看提交歷史
$ git log
commit fc0166f53a45cfc4c17079e5e3454fb7e3136cb6
Author: andy<andy@gmail.com>
Date: Sat Mar 8 15:52:10 2014 +0800
test git diff
commit d6eab3a38aee0b25ac395899c82edd48723a2ea9
Author: andy<andy@gmail.com>
Date: Sat Mar 8 15:36:53 2014 +0800
enforce commit'
commit 85ec024140442df6db8625a07c2ee7369cceb704
Author: andy<andy@gmail.com>
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(後跟指定格式)
十、撤銷操做
能夠修改最後一次提交的內容,當你發現最近一次提交內容不正確時候,能夠經過 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
十一、取消文件修改
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^ 引用 工做區 暫存區 完全消除