設置
$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"
name = Fristname Lastname
email = [email protected]
git config命令的–global參數,這個參數,表示你這臺機器上所有的Git倉庫都會使用這個配置
創建文件夾
$ mkdir test
$ cd test
$ pwd
分別爲
創建一個叫test的文件夾(中文名Windows系統可能會出現各種莫名其妙的問題)
切換到當前文件夾中
顯示當前路徑
把這個目錄作爲Git可管理的倉庫
$ git init
必須切換到當前目錄
添加文件
編寫一個test.txt文件,內容爲 (放到test目錄下)
ABCDEFD
添加文件 git add
$ git add test.txt
執行之後是沒有反應就是成功了
然後commit
$ git commit -m "write a test"
[master (root-commit) e3f073c] write a test
1 file changed, 1 insertion(+)
create mode 100644 test.txt
-m後面輸入的是本次提交的說明,可以輸入任意內容
git commit命令執行成功後會告訴你,1個文件被改動(我們新添加的test.txt文件),插入了一行內容(test.txt有一行內容)。
這樣就提交到倉庫了。
查看當前狀態和不同
修改test.txt內容爲(建議使用notepad++)
ABCDEFG
查看當前狀態 git status
$ 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: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
git status命令可以讓我們時刻掌握倉庫當前的狀態,上面的命令告訴我們,test.txt被修改過了,但還沒有準備提交的修改。
查看不同 git diff
$ git diff
diff --git a/test.txt b/test.txt
index 5ee15cd..325c6c6 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-ABCDEFD
\ No newline at end of file
+ABCDEFG
\ No newline at end of file
$ git diff test.txt
diff --git a/test.txt b/test.txt
index 5ee15cd..325c6c6 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-ABCDEFD
\ No newline at end of file
+ABCDEFG
\ No newline at end of file
兩種方法都可以
查看兩次的不同,顯示的格式是Unix通用的diff格式
提交文件
提交文件 git add
git add test.txt
再次查看當前狀態 git status
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: test.txt
意思爲將要被提交的修改包括test.txt
可以放心地提交了
$ git commit -m "add G"
[master 9859fc8] add G
1 file changed, 1 insertion(+), 1 deletion(-)
然後查看當前狀態
$ git status
On branch master
nothing to commit, working tree clean
Git告訴我們當前沒有需要提交的修改,而且,工作目錄是乾淨(working tree clean)的。
還原版本
再次修改test.txt文件爲
ABCDEFGH
嘗試提交:
$ git add test.txt
$ git commit -m "add H"
[master 71c56fd] add H
1 file changed, 1 insertion(+), 1 deletion(-)
現在在Git倉庫裏有許多版本,可以用 git log 查看:
$ git log
commit 71c56fdced4403930c9238334ab11c48e40c39c6 (HEAD -> master)
Author: somliy <1187525390@qq.com>
Date: Fri Jan 5 19:47:03 2018 +0800
add H
commit 9859fc8aeec24ce0115af6e016d0b4b1807fd840
Author: somliy <1187525390@qq.com>
Date: Fri Jan 5 19:40:51 2018 +0800
add G
commit e3f073c77ed6fbcfc2fb03f05215ecf40a076e39
Author: somliy <1187525390@qq.com>
Date: Fri Jan 5 19:20:44 2018 +0800
write a test
有創建,添加G,添加H,幾個歷史版本
如果嫌輸出信息太多,看得眼花繚亂的,可以試試加上–pretty=oneline參數:
$ git log --pretty=oneline
71c56fdced4403930c9238334ab11c48e40c39c6 (HEAD -> master) add H
9859fc8aeec24ce0115af6e016d0b4b1807fd840 add G
e3f073c77ed6fbcfc2fb03f05215ecf40a076e39 write a test
前面一大堆字符串是commit id(16進制的版本號)
現在我們退回上一個版本
ABCDEFG
使用 git reset
$ git reset --hard HEAD^
HEAD is now at 9859fc8 add G
–hard參數有啥意義?這個後面再講,現在你先放心使用。
HEAD相當於一個指針,上一個版本就是HEAD^,上上一個版本就是HEAD^^,當然往上100個版本寫100個^比較容易數不過來,所以寫成HEAD~100。
先看一下內容:
$ cat test.txt
ABCDEFG
已經沒有H了,現在可以用 git log 看以前的版本,已經不是3個了
$ git log --pretty=oneline
9859fc8aeec24ce0115af6e016d0b4b1807fd840 (HEAD -> master) add G
e3f073c77ed6fbcfc2fb03f05215ecf40a076e39 write a test
也可以使用commit id退回制定版本:
$ git reset --hard e3f073c
HEAD is now at e3f073c write a test
$ cat test.txt
ABCDEFD
當你用$ git reset –hard HEAD^回退到add G版本時,再想恢復到add H,就必須找到add H的commit id。Git提供了一個命令git reflog用來記錄你的每一次命令:
$ git reflog
e3f073c (HEAD -> master) HEAD@{0}: reset: moving to e3f073c
9859fc8 HEAD@{1}: reset: moving to 9859
9859fc8 HEAD@{2}: reset: moving to HEAD^
71c56fd HEAD@{3}: commit: add H
9859fc8 HEAD@{4}: commit: add G
e3f073c (HEAD -> master) HEAD@{5}: commit (initial): write a test
版本庫
工作區有一個隱藏目錄.git,這個不算工作區,而是Git的版本庫。
Git的版本庫裏存了很多東西,其中最重要的就是稱爲stage(或者叫index)的暫存區,還有Git爲我們自動創建的第一個分支master,以及指向master的一個指針叫HEAD。
我們把文件往Git版本庫裏添加的時候,是分兩步執行的:
第一步是用git add把文件添加進去,實際上就是把文件修改添加到暫存區;
第二步是用git commit提交更改,實際上就是把暫存區的所有內容提交到當前分支。
因爲我們創建Git版本庫時,Git自動爲我們創建了唯一一個master分支,所以,現在,git commit就是往master分支上提交更改。
多次改動分別添加,一次提交或者多次提交
比如我們再兩次修改文件內容
ABCDEFD
E
$ git add test.txt
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: test.txt
ABCDEFD
EF
$ git commit -m "add F"
[master 07605be] add F
1 file changed, 2 insertions(+), 1 deletion(-)
$ 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: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
怎麼第二次的修改沒有被提交?
回顧一下操作過程:
第一次修改 -> git add -> 第二次修改 -> git commit
你看,我們前面講了,Git管理的是修改,當你用git add命令後,在工作區的第一次修改被放入暫存區,準備提交,但是,在工作區的第二次修改並沒有放入暫存區,所以,git commit只負責把暫存區的修改提交了,也就是第一次的修改被提交了,第二次的修改不會被提交。
撤銷修改
$ git checkout -- test.txt
命令git checkout – test.txt意思就是,把readme.txt文件在工作區的修改全部撤銷,這裏有兩種情況:
一種是readme.txt自修改後還沒有被放到暫存區,現在,撤銷修改就回到和版本庫一模一樣的狀態;
一種是readme.txt已經添加到暫存區後,又作了修改,現在,撤銷修改就回到添加到暫存區後的狀態。
總之,就是讓這個文件回到最近一次git commit或git add時的狀態。
刪除文件
$ rm test.txt
這個時候,Git知道你刪除了文件,因此,工作區和版本庫就不一致了
現在你有兩個選擇,
一是確實要從版本庫中刪除該文件,那就用命令git rm刪掉,並且git commit:
$ git rm test.txt
rm 'test.txt'
$ git commit -m "remove test.txt"
[master d17efd8] remove test.txt
1 file changed, 1 deletion(-)
delete mode 100644 test.txt
另一種情況是刪錯了,因爲版本庫裏還有呢,所以可以很輕鬆地把誤刪的文件恢復到最新版本:
$ git checkout -- test.txt