这个软件用起来就应该像这个样子,能记录每次文件的改动:
版本 用户 说明 日期
1 张三 删除了软件服务条款 5 7/12 10:38
2 张三 增加了License人数限制 7/12 18:09
3 李四 财务部门调整了合同金额 7/13 9:51
4 张三 延长了免费升级周期 7/14 15:17
|
安装Vundle插件管理器 //可以让vim鼠标定位和显示错误信息
$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
$ cd ~/
$ mv .vimrc vimrc.bak
$ git clone https://github.com/meihao1203/vimrc
$ cd vimrc/
$ cp vimrc ~/.vimrc
$ vim
:PluginInstall然后等他下载完就可以了
:BundleUpdate更新插件
:BundleClean清除不再使用的插件
:BundleList列出所有插件
:BundleSearch查找插件
|
$ mkdir learngit
$ cd learngit
$ pwd
/Users/michael/learngit
|
git init
命令把这个目录变成Git可以管理的仓库
$ git init Initialized empty Git repository in /Users/michael/learngit/.git/ |
.git
目录,那是因为这个目录默认是隐藏的,用
ls -ah
命令就可以看见。
readme.txt
文件,内容如下:
learngit
目录下(子目录也行),因为这是一个Git仓库,放到其他地方Git再厉害也找不到这个文件
Git is a version control system. Git is free software. |
git add
告诉Git,把文件添加到仓库:
$ git add readme.txt |
git commit
告诉Git,把文件提交到仓库:
$ git commit -m "wrote a readme file" //冒号里面的是注释 [master (root-commit) cb926e7] wrote a readme file 1 file changed, 2 insertions(+) create mode 100644 readme.txt |
-m
后面输入的是本次提交的说明,可以输入任意内容,当然最好是有意义的,这样你就能从历史记录里方便地找到改动记录。
git commit
命令执行成功后会告诉你,1个文件被改动(我们新添加的readme.txt文件),插入了两行内容(readme.txt有两行内容)。
//可以add多个文件,一次提交 $ git add file1.txt $ git add file2.txt file3.txt $ git commit -m "add 3 files." |
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 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 add readme.txt |
git status
看看当前仓库的状态:
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: readme.txt # |
//git status
告诉我们,将要被提交的修改包括readme.txt
$ git commit -m "add distributed" [master ea34578] add distributed 1 file changed, 1 insertion(+), 1 deletion(-) |
$ git status # On branch master nothing to commit (working directory clean) |
•要随时掌握工作区的状态,使用git status命令。 •如果git status告诉你有文件被修改过,用git diff可以查看修改内容。 |
Git is a distributed version control system. Git is free software distributed under the GPL. |
$ git add readme.txt $ git commit -m "append GPL" [master 3628164] append GPL 1 file changed, 1 insertion(+), 1 deletion(-) |
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 |
//git log
命令显示从最近到最远的提交日志,我们可以看到3次提交,最近的一次是
append GPL
,上一次是
add distributed
,最早的一次是
wrote a readme file
。
--pretty=oneline
参数:
$ git log --pretty=oneline 3628164fb26d48395383f8f31179f24e0882e1e0 append GPL ea34578d5496d7dd233c827ed32a8cd576c5ee85 add distributed cb926e7ea50ad11b8f9e909c05226233bf755030 wrote a readme file |
$ git reset --hard HEAD^ HEAD is now at ea34578 add distributed |
$ cat readme.txt Git is a distributed version control system. Git is free software. |
$ git log 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 |
append GPL
已经看不到了!
append GPL
的
commit id
是
3628164...
,于是就可以指定回到未来的某个版本:
//版本号没必要写全,前几位就可以了,Git会自动去找 $ git reset --hard 3628164 HEAD is now at 3628164 append GPL |
$ cat readme.txt Git is a distributed version control system. Git is free software distributed under the GPL. |
Git的版本回退速度非常快,因为Git在内部有个指向当前版本的
HEAD
指针,当你回退版本的时候,Git仅仅是把HEAD从指向
append GPL
:
改为指向
add distributed
:
然后顺便把工作区的文件更新了。所以你让
HEAD
指向哪个版本号,你就把当前版本定位在哪。
当你用$ git reset --hard HEAD^
回退到
add distributed
版本时,再想恢复到
append GPL
,就必须找到
append GPL
的commit id。Git提供了一个命令
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 |
append GPL
的commit id是
3628164,这样上面的命令窗口关闭,不知道前面的版本号也可以查看
learngit
文件夹就是一个工作区:
.git
,这个不算工作区,而是Git的版本库。
master
,以及指向
master
的一个指针叫
HEAD
。
readme.txt
中添加了一行:
$ cat 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. My stupid boss still prefers SVN. |
$ 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 checkout -- file
可以丢弃工作区的修改:
$ git checkout -- readme.txt |
命令
git checkout -- readme.txt
意思就是,把
readme.txt
文件在工作区的修改全部撤销,这里有两种情况:
一种是
readme.txt
自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
一种是
readme.txt
已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
总之,就是让这个文件回到最近一次
git commit
或
git add
时的状态。
现在,看看
readme.txt
的文件内容:
$ cat 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. |
$ git add test.txt $ git commit -m "add test.txt" [master 94cdc44] add test.txt 1 file changed, 1 insertion(+) create mode 100644 test.txt |
$ rm test.txt |
//Git知道你删除了文件,因此,工作区和版本库就不一致了,
git status
命令会立刻告诉你哪些文件被删除了:
$ git status # On branch master # Changes not staged for commit: # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # deleted: test.txt # no changes added to commit (use "git add" and/or "git commit -a") |
确实要从版本库中删除该文件,那就用命令
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 |