Git,是Linus花了兩周時間用C寫的一個分布式版本控制系統。牛該怎么定義?
其實,很多人都不care誰寫了Git,只在乎它是免費而且好用的!So do I!
下面開始我們的學習:
1.Git安裝(略)。
2.創建版本庫
首先,選擇一個合適的地方(我選擇了D盤,我的電腦是Win 7),常見一個空目錄:
$ mkdir Git $ cd Git $ pwd//顯示當前的路徑 /d/Git
注:Windows下,路徑名不要包含中文,因為Git對中文支持不給力!
第二步,通過git init
命令把這個目錄變成Git可以管理的倉庫:
$ git init Initialized empty Git repository in /d/Git/.git/
這樣就創建了你的Git倉庫。
接下來,我們上傳一個文件到Git。編輯一個readme.txt文件,內容如下:
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將文件添加到倉庫:
$ git add readme.txt
(2)使用git commit將文件提交到倉庫:
git commit -m "wrote a readme file" [master 48b9a84] wrote a readme file 1 file changed, 2 insertions(+)
注:git commit
命令,-m
后面輸入的是本次提交的說明,可以輸入任意內容,當然最好是有意義的,這樣你就能從歷史記錄里方便地找到改動記錄。
commit可以一次提交多個文件:
$ git add file1.txt $ git add file2.txt $ git add file3.txt $ git commit -m "add 3 files."
3.Git的命令很多,下面再學習幾個吧!
繼續修改readme.txt文件:
Git is a distributed version control system. Git is free software.
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: 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
這個命令看看:
$ 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命令:
$ 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
………………………………………………………………………………
………………………………………………………………………………
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
參數:
$ 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:
$ git reset --hard HEAD^
--hard
參數有啥意義?這個后面再講,現在你先放心使用。
查看文件命令cat readme.txt:
$ cat readme.txt Git is a distributed version control system. Git is free software distributed under the GPL.
git reflog記錄了每一次命令:
$ 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>:
$ git reset --hard 3628164
感謝廖叫獸:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137402760310626208b4f695940a49e5348b689d095fc000