其實,不少人都不care誰寫了Git,只在意它是免費並且好用的!So do I!html
下面開始咱們的學習:linux
1.Git安裝(略)。git
2.建立版本庫app
首先,選擇一個合適的地方(我選擇了D盤,個人電腦是Win 7),常見一個空目錄:學習
?spa
1.net 23d 3日誌 4code |
$ mkdir Git $ cd Git $ pwd //顯示當前的路徑 /d/Git |
注:Windows下,路徑名不要包含中文,由於Git對中文支持不給力!
第二步,經過git init
命令把這個目錄變成Git能夠管理的倉庫:
?
1 2 |
$ git init Initialized empty Git repository in /d/Git/.git/ |
這樣就建立了你的Git倉庫。
接下來,咱們上傳一個文件到Git。編輯一個readme.txt文件,內容以下:
?
1 2 3 4 |
Git is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes of files. |
將其放到/d/Git目錄下,由於這是一個Git倉庫,放到其餘地方Git再厲害也找不到這個文件。
將一個文件放到Git倉庫須要兩步:
(1)使用git add將文件添加到倉庫:
?
(2)使用git commit將文件提交到倉庫:
?
1 2 3 |
git commit -m "wrote a readme file" [master 48b9a84] wrote a readme file 1 file changed, 2 insertions(+) |
注:git commit
命令,-m
後面輸入的是本次提交的說明,能夠輸入任意內容,固然最好是有意義的,這樣你就能從歷史記錄裏方便地找到改動記錄。
commit能夠一次提交多個文件:
?
1 2 3 4 |
$ git add file1.txt $ git add file2.txt $ git add file3.txt $ git commit -m "add 3 files." |
3.Git的命令不少,下面再學習幾個吧!
繼續修改readme.txt文件:
?
1 2 |
Git is a distributed version control system. Git is free software. |
git status
命令看看結果:
?
1 2 3 4 5 6 7 8 9 |
$ 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查看倉庫當前的狀態,上面的命令告訴咱們,readme.txt被修改過了,但尚未準備提交的修改。
雖然Git告訴咱們readme.txt被修改了,但若是能看看具體修改了什麼內容,天然是很好的。好比你休假兩週從國外回來,第一天上班時,已經記不清上次怎麼修改的readme.txt,因此,須要用git diff
這個命令看看:
?
1 2 3 4 5 6 7 8 9 |
$ git diff readme.txt diff --git a/readme.txt b/readme.txt index 46d49bf..9247db6 100644 --- a/readme.txt +++ b/readme.txt @@ - 1 , 2 + 1 , 2 @@ -Git is a version control system. +Git is a distributed version control system. Git is free software. |
git diff查看不一樣!
在工做中,咱們可能提交了幾千個文件,若是想看歷史記錄,可使用git log命令:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$ git log commit 3628164fb26d48395383f8f31179f24e0882e1e0 Author: Michael Liao <askxuefeng @gmail .com> Date: Tue Aug 20 15 : 11 : 49 2013 + 0800 append GPL commit ea34578d5496d7dd233c827ed32a8cd576c5ee85 Author: Michael Liao <askxuefeng @gmail .com> Date: Tue Aug 20 14 : 53 : 12 2013 + 0800
add distributed commit cb926e7ea50ad11b8f9e909c05226233bf755030 Author: Michael Liao <askxuefeng @gmail .com> Date: Mon Aug 19 17 : 51 : 55 2013 + 0800
wrote a readme file<br>………………………………………………………………………………<br>……………………………………………………………………………… |
commit 0f71dba115d8830212fd1736a02a077ce2e91699
Author: lixiaolun <303041859@qq.com>
Date: Thu Jan 15 22:06:05 2015 +0800
wrote a readme file
(END)
注:最後你可能會碰到這個(END),此後你怎麼點都沒有用。那麼如今你要輸入:wq或:q退出。這個命令同linux指令。
git log
命令顯示從最近到最遠的提交日誌。若是嫌輸出信息太多,看得眼花繚亂的,能夠試試加上
--pretty=oneline
參數:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$ git log --pretty=oneline fae7920797cbe9057e7e5ced1fdc79d1eb592758 commit a file readme 33cff68fd77dcbfdb8644a8d3f1c34175830b1a6 text1.txt commit 48b9a84010813eecb1e3acca555d1b704c9d5930 wrote a readme file 2d874d572e805c1825200458a8a8aa9e55429d8f 2015 - 1 - 30 upload 86edb2f2f658578f993532c83c5c368d2c4a7c4c local_gitgub 7d3197611468b3d7dd6b861829e19de626c22bc8 remove text1.txt d9ee12aeca6cacf25f6b02095d03d5a9f03d3c5e remove text.txt d79f7ec6a470f0efb1afee1accb28fae3ef3a995 add test.txt 24a93f3894fec142cbc11bc508f2407635380c81 git tracks changes c22b22edea6b0f9fff3c4d73b3351c49e966a85e add 3 text.txt 57c62b9d4e94c19a9484ca6c6c6e84f18965b41a understand how stage f2bbf87ef050bb70d98390cf8ca1680ed0dff297 modify reamde.txt fe829f988f43647933edb35f347171c54187af4a add a new word distr 0f71dba115d8830212fd1736a02a077ce2e91699 wrote a readme file |
友情提示:你看到的一大串相似3628164...882e1e0
的是commit id
(版本號),和SVN不同,Git的commit id
不是1,2,3……遞增的數字,而是一個SHA1計算出來的一個很是大的數字,用十六進制表示。
時光穿梭之版本回退!!
若是你提交的一個文件,發現還不如你你上一個版本好,趕忙回退!怎麼作呢?
首先,Git必須知道當前版本是哪一個版本,在Git中,用HEAD
表示當前版本,也就是最新的提交3628164...882e1e0
(注意個人提交ID和你的確定不同),上一個版本就是HEAD^
,上上一個版本就是HEAD^^
,固然往上100個版本寫100個^
比較容易數不過來,因此寫成HEAD~100
。
回退到上一個版本的命令git reset:
?
1 |
$ git reset --hard HEAD^ |
--hard
參數有啥意義?這個後面再講,如今你先放心使用。
查看文件命令cat readme.txt:
?
1 2 3 |
$ cat readme.txt Git is a distributed version control system. Git is free software distributed under the GPL. |
git reflog記錄了每一次命令:
?
1 2 3 4 5 |
$ git reflog ea34578 HEAD@{ 0 }: reset: moving to HEAD^ 3628164 HEAD@{ 1 }: commit: append GPL ea34578 HEAD@{ 2 }: commit: add distributed cb926e7 HEAD@{ 3 }: commit (initial): wrote a readme file |
前面的數字是commit id。知道commit id能夠回退上一次執行的命令,回退命令爲git reset --hard <commit id>:
?
1 |
$ git reset --hard 3628164 |