当前位置: 移动技术网 > 科技>操作系统>Linux > GitHub-暂存区与版本回退

GitHub-暂存区与版本回退

2018年10月03日  | 移动技术网科技  | 我要评论

 

 

参考博文:廖雪峰git教程

 

1. 工作区和暂存区

       git和其他版本控制系统如svn的一个不同之处就是有暂存区的概念。

 

1.1. 工作区(working directory)

       就是你在电脑里能看到的目录.比如:zhangtest

1 [root@mini05 zhangtest]# pwd
2 /opt/git_repository/zhangtest
3 [root@mini05 zhangtest]# ll
4 total 8
5 -rw-r--r-- 1 root root 96 sep 23 20:54 readme.md
6 -rw-r--r-- 1 root root 12 sep 17 23:27 test.info

 

1.2. 版本库(repository)

       工作区有一个隐藏目录.git,这个不算工作区,而是git的版本库。

       git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有git为我们自动创建的第一个分支master,以及指向master的一个指针叫head

 

       前面讲了我们把文件往git版本库里添加的时候,是分两步执行的:

       第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;

       第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。

 

       你可以简单理解为,需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改。

 

测试实践

       对readme.md追加一些内容;新建license并添加内容。

 1 [root@mini05 zhangtest]# ll
 2 total 12
 3 -rw-r--r-- 1 root root  16 sep 29 19:17 license
 4 -rw-r--r-- 1 root root 104 sep 29 19:18 readme.md
 5 -rw-r--r-- 1 root root  12 sep 17 23:27 test.info
 6 [root@mini05 zhangtest]# 
 7 [root@mini05 zhangtest]# git status
 8 # on branch master
 9 # your branch is ahead of 'origin/master' by 2 commits.
10 #   (use "git push" to publish your local commits)
11 #
12 # changes not staged for commit:
13 #   (use "git add <file>..." to update what will be committed)
14 #   (use "git checkout -- <file>..." to discard changes in working directory)
15 #
16 #    modified:   readme.md
17 #
18 # untracked files:
19 #   (use "git add <file>..." to include in what will be committed)
20 #
21 #    license
22 no changes added to commit (use "git add" and/or "git commit -a")

       由上可知:git非常清楚地告诉我们,readme.txt被修改了,而license还从来没有被添加过,所以它的状态是untracked

 

       现在,使用两次命令git add,把readme.txtlicense都添加后,用git status再查看一下:

 1 [root@mini05 zhangtest]# git add readme.md
 2 [root@mini05 zhangtest]# git add license
 3 [root@mini05 zhangtest]# 
 4 [root@mini05 zhangtest]# git status
 5 # on branch master
 6 # your branch is ahead of 'origin/master' by 2 commits.
 7 #   (use "git push" to publish your local commits)
 8 #
 9 # changes to be committed:
10 #   (use "git reset head <file>..." to unstage)
11 #
12 #    new file:   license
13 #    modified:   readme.md
14 #

 

       现在,暂存区的状态就变成这样了:

 

       所以,git add命令实际上就是把要提交的所有修改放到暂存区(stage),然后,执行git commit就可以一次性把暂存区的所有修改提交到分支。

1 [root@mini05 zhangtest]# git commit -m "understand how stage works"
2 [master 53f0f2e] understand how stage works
3  2 files changed, 6 insertions(+)
4  create mode 100644 license

 

       一旦提交后,如果你又没有对工作区做任何修改,那么工作区就是“干净”的:

1 [root@mini05 zhangtest]# git status
2 # on branch master
3 # your branch is ahead of 'origin/master' by 3 commits.
4 #   (use "git push" to publish your local commits)
5 #
6 nothing to commit, working directory clean

 

       现在版本库变成了这样,暂存区就没有任何内容了:

 

 

2. 版本回退

       在readme.md追加了如下内容,并进行了提交。

1 git is a distributed version control system.
2 git is free software distributed under the gpl.

 

       提交

 1 [root@mini05 zhangtest]# vim readme.md 
 2 # zhangtest
 3 zhangtest
 4 张三
 5 git is a distributed version control system.
 6 git is free software.
 7 git is a distributed version control system.
 8 git is free software distributed under the gpl.
 9 [root@mini05 zhangtest]# git add .  # 添加到暂存区
10 [root@mini05 zhangtest]# git commit -m "append gpl"  # 提交到仓库

 

2.1. 查看版本日志信息

 1 # 使用 git add . 就是针对当前目录
 2 [root@mini05 zhangtest]# git log readme.md   # 针对readme.md文件
 3 commit 9f27dce0f57cf811a8e3bdab545e8b98ca9bd41f
 4 author: zhang san <zhanglianghhh@163.com>
 5 date:   sun sep 23 20:38:53 2018 +0800
 6 
 7     append gpl
 8 
 9 commit 65a58f2661c4d73dc0dc9c2e5bff4c350c42c98e
10 author: zhang san <zhanglianghhh@163.com>
11 date:   mon sep 17 23:31:25 2018 +0800
12 
13     add info
14 
15 commit d4fb57e8a892060db07fe862058a1a8477be49aa
16 author: 张亮 <zhanglianghhh@163.com>
17 date:   mon sep 17 23:10:57 2018 +0800
18 
19     update readme.md
20 
21 commit e7306765445375e4c1b52ebde07a666da5517b22
22 author: 张亮 <zhanglianghhh@163.com>
23 date:   mon sep 17 22:34:57 2018 +0800
24 
25     initial commit

 

2.2. 简化版本日志

1 [root@mini05 zhangtest]# git log --pretty=oneline readme.md
2 9f27dce0f57cf811a8e3bdab545e8b98ca9bd41f append gpl
3 65a58f2661c4d73dc0dc9c2e5bff4c350c42c98e add info
4 d4fb57e8a892060db07fe862058a1a8477be49aa update readme.md
5 e7306765445375e4c1b52ebde07a666da5517b22 initial commit

 

2.3. 版本回退到65a58f2661

       在git中,用head表示当前版本,也就是最新的提交9f27dce...(注意我的提交id和你的肯定不一样),上一个版本就是head^,上上一个版本就是head^^,当然往上100个版本写100个^比较容易数不过来,所以写成head~100

 1 # git reset --hard 65a58f2661  回退到指定版本,好处就是不用计算到底回退几个版本
 2 [root@mini05 zhangtest]# git reset --hard head^    # 回退到上一个版本
 3 head is now at 65a58f2 add info
 4 [root@mini05 zhangtest]# cat readme.md  # 查看内容,可见以回退到上一个版本
 5 # zhangtest
 6 zhangtest
 7 张三
 8 git is a distributed version control system.
 9 git is free software.
10 [root@mini05 zhangtest]# git log --pretty=oneline readme.md  # 根据提交日志,也可知已回退
11 65a58f2661c4d73dc0dc9c2e5bff4c350c42c98e add info
12 d4fb57e8a892060db07fe862058a1a8477be49aa update readme.md
13 e7306765445375e4c1b52ebde07a666da5517b22 initial commit

 

2.4. 回退原理

       git的版本回退速度非常快,因为git在内部有个指向当前版本的head指针,当你回退版本的时候,git仅仅是把head从指向append gpl

  

 

改为指向add info

  

 

       然后顺便把工作区的文件更新了。所以你让head指向哪个版本号,你就把当前版本定位在哪。

 

2.5. 记录每一次命令

       回退到了某个版本,想恢复到新版本怎么办?找不到新版本的commit id怎么办?

       git提供了一个命令git reflog用来记录你的每一次命令:

       用git reflog查看命令历史,以便确定要回到未来的哪个版本。

1 [root@mini05 zhangtest]# git reflog  
2 65a58f2 head@{0}: reset: moving to 65a58f2661
3 9f27dce head@{1}: reset: moving to 9f27dce0f57cf
4 65a58f2 head@{2}: reset: moving to head^
5 9f27dce head@{3}: commit: append gpl
6 65a58f2 head@{4}: commit: add info
7 d4fb57e head@{5}: pull git@github.com:zhanglianghhh/zhangtest.git: fast-forward
8 e730676 head@{6}: clone: from git@github.com:zhanglianghhh/zhangtest.git

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网