本地版本库E:learngit
所以learngit文件夹是工作区
learngit文件夹下.git/文件夹是版本库:包含暂存区(stage)和指向master的指针(head)
master分支是唯一的,可以多次把工作区的文件git add添加到暂存区,然后一次性git commit提交暂存区修改
1.再次修改readme.txt,使得存在3个版本
$ git commit -m "append GPL"
版本1:wrote a readme file
Git is a version control system.
Git is free software.
版本2:add distributed
Git is a distributed version control system.
Git is free software.
版本3:append GPL
Git is a distributed version control system.
Git is free software distributed under the GPL.
2.学习如何从版本3回退到版本2
git log 查看版本历史记录
$ git log
commit af4d2e71afb5c6cbbb4086e32a7c4369b81e4dd0
Author: lao_wan <lao_wan@sina.cn>
Date: Tue Jan 12 23:26:01 2016 +0800
append GPL
commit 5e0e7b79dbe05b2231b11a32117ad22c949f0a0f
Author: lao_wan <lao_wan@sina.cn>
Date: Tue Jan 12 00:24:33 2016 +0800
Add distributed
commit ae5741ee0691c73f5f902378a0a025cb54639a00
Author: lao_wan <lao_wan@sina.cn>
Date: Mon Jan 11 23:40:43 2016 +0800
Add three files:file1.txt,file2.txt,file3.txt
commit 274dc892e7ff517c79c95661f29f6e19ff3e2524
Author: lao_wan <lao_wan@sina.cn>
Date: Mon Jan 11 23:36:32 2016 +0800
Write a readme.txt file
asus@asus-PC MINGW64 /e/learngit (master)
$
也可以使用git log --pretty=oneline简要输出
$ git log --pretty=oneline
af4d2e71afb5c6cbbb4086e32a7c4369b81e4dd0 append GPL
5e0e7b79dbe05b2231b11a32117ad22c949f0a0f Add distributed
ae5741ee0691c73f5f902378a0a025cb54639a00 Add three files:file1.txt,file2.txt,file3.txt
274dc892e7ff517c79c95661f29f6e19ff3e2524 Write a readme.txt file
上一个版本就是HEAD^
,上上一个版本就是HEAD^^
,当然往上100个版本写100个^
比较容易数不过来,所以写成HEAD~100
。
git reset --hard HEAD^
$ git reset --hard HEAD^
HEAD is now at 5e0e7b7 Add distributed
查看版本库状态:
$ git log --pretty=oneline
5e0e7b79dbe05b2231b11a32117ad22c949f0a0f Add distributed
ae5741ee0691c73f5f902378a0a025cb54639a00 Add three files:file1.txt,file2.txt,file3.txt
274dc892e7ff517c79c95661f29f6e19ff3e2524 Write a readme.txt file
2.如何从版本2回退到版本3:git reset --hard commit_id
Git提供了一个命令git reflog
用来记录你的每一次命令
$ git reflog
5e0e7b7 HEAD@{0}: reset: moving to HEAD^
af4d2e7 HEAD@{1}: commit: append GPL
5e0e7b7 HEAD@{2}: commit: Add distributed
ae5741e HEAD@{3}: commit: Add three files:file1.txt,file2.txt,file3.txt
274dc89 HEAD@{4}: commit (initial): Write a readme.txt file
找到版本3的commit ID:af4d2e7
$ git reset --hard af4d2e7
HEAD is now at af4d2e7 append GPL
#####################################################
工作区与暂存区---略