其实,不少人都不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 |