git 仓库原理

时间:2022-08-26 22:22:55
2017年01月13日 21:07:202399人阅读 评论(0) 收藏 举报
git 仓库原理 分类:
Git(4) git 仓库原理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenxiqilin/article/details/54408817

git 工作原理图

git 仓库原理

如上图所示,有三个区域Working Directory、stage、master。 
名词解释:

工作区(Working Directory) 
在我们直接编辑文件(文件夹)的根目录,如下图: 
git 仓库原理

在G盘Git目录下就是工作区

版本库(Repository) 
版本库才是git正式工作的地方,在工作区下隐藏目录里,如下图: 
git 仓库原理 
版本库主要包括两个区,如上图,包括“stage”和“master”。

  • master 
    master区管理了我们每次提交后的文件版本以及相关信息,是git最重要的仓库。在master区,有一个head指针(见图1),指向最新提交的版本。

  • stage 
    stage是工作区到master区的缓存区,在做小的修改时我们可以先提交到stage,确定没有问题了,或者当天的工作完成了,再把缓存区的内容最终提交到master。

工作原理 
上篇博客里讲了命令行:git add、git commit 
其中执行git add的时候,是把在“Working Directory”去修改(增加或删除)的内容提交到“stage”

执行git commit之类的时候,把“stage”的内容最终提交到“master”区。

git status 
命令行git status 能够帮助我们快速的了解git的当前状态。 
例如,我们现在工作区新增一个test3.txt文件(先不要添加到版本库),然后执行git status。

chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed) test3.txt nothing added to commit but untracked files present (use "git add" to track)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

从上面的代码我们可以看出,有在工作区新文件(test3.txt)待提交,但从stage区到master区没有新内容提交。 
所以根据上篇博客的知识,先用git add把test3.txt提交到stage。执行指令git add后,再使用git status查当前git状态,如一下代码:

chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git add test3.txt chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage) new file: test3.txt chenxi@chenxi_pc MINGW64 /G/Git (master)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

从上面的的结果知道已经成功添加到status,准备好等待我们提交到master,执行git commit可以完成提交任务,如下图:

chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git commit -m "add test3.txt file"
[master 39e0dba] add test3.txt file
1 file changed, 1 insertion(+)
create mode 100644 test3.txt chenxi@chenxi_pc MINGW64 /G/Git (master) chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git status
On branch master
nothing to commit, working directory clean chenxi@chenxi_pc MINGW64 /G/Git (master)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

版本回退

通过前面我们已经掌握了提交新版本,如果发现我修改错了东西,怎样返回新版本呢? 
我们修改test.txt文件,添加新内容,如下: 
git 仓库原理

执行git add、git commit指令,如以下代码:

chenxi@chenxi_pc MINGW64 /G/Git (master)
$ 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: test.txt no changes added to commit (use "git add" and/or "git commit -a") chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git add test.txt chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git commit -m "add hello world to test.txt"
[master 78db795] add hello world to test.txt
1 file changed, 6 insertions(+), 1 deletion(-) chenxi@chenxi_pc MINGW64 /G/Git (master)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

再在上面的基础上添加新内容,如下图: 
git 仓库原理

并提交:

chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git add test.txt chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git commit -m "add hello CSDN to test.txt"
[master 8f4ae6d] add hello CSDN to test.txt
1 file changed, 2 insertions(+) chenxi@chenxi_pc MINGW64 /G/Git (master)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

好,现在我们使用log指令查一下我们都执行了哪些操作:

chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git log
commit 8f4ae6dc10a67a0a5726f449c6c65e288d39f57f
Author: chenximcm <1178898205@qq.com>
Date: Fri Jan 13 20:23:18 2017 +0800 add hello CSDN to test.txt commit 78db795dabdeb752d635ed2960b87ec49bb13943
Author: chenximcm <1178898205@qq.com>
Date: Fri Jan 13 20:15:46 2017 +0800 add hello world to test.txt commit 39e0dba93090a1f01887180e7bedbb7dda1fe5b3
Author: chenximcm <1178898205@qq.com>
Date: Fri Jan 13 19:36:40 2017 +0800 add test3.txt file commit 63d890ed08ee938dc4e83ad14446727dc9d55da3
Author: chenximcm <1178898205@qq.com>
Date: Fri Dec 30 17:43:55 2016 +0800 add test1.txt and test2.txt files commit 489b113d392d1bc930f6eef1b6f0135d6bd6e0d3
Author: chenximcm <1178898205@qq.com>
Date: Fri Dec 30 12:13:05 2016 +0800 add panoramaProjects folder :...skipping...
commit 8f4ae6dc10a67a0a5726f449c6c65e288d39f57f
Author: chenximcm <1178898205@qq.com>
Date: Fri Jan 13 20:23:18 2017 +0800 add hello CSDN to test.txt commit 78db795dabdeb752d635ed2960b87ec49bb13943
Author: chenximcm <1178898205@qq.com>
Date: Fri Jan 13 20:15:46 2017 +0800 add hello world to test.txt commit 39e0dba93090a1f01887180e7bedbb7dda1fe5b3
Author: chenximcm <1178898205@qq.com>
Date: Fri Jan 13 19:36:40 2017 +0800 add test3.txt file commit 63d890ed08ee938dc4e83ad14446727dc9d55da3
Author: chenximcm <1178898205@qq.com>
Date: Fri Dec 30 17:43:55 2016 +0800 add test1.txt and test2.txt files commit 489b113d392d1bc930f6eef1b6f0135d6bd6e0d3
Author: chenximcm <1178898205@qq.com>
Date: Fri Dec 30 12:13:05 2016 +0800 add panoramaProjects folder commit 664376a8783c533c90731dd00e4093d8bff9e97b
Author: chenximcm <1178898205@qq.com>
Date: Fri Dec 30 12:10:09 2016 +0800 add test.txt file
~
~ chenxi@chenxi_pc MINGW64 /G/Git (master)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73

好多啊,是不是觉得输出太多信息了,没事,我们可以把每次的信息只输出精简信息,我们只需要在git log 后面添加参数“–pretty=oneline”即可。

chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git log --pretty=oneline
8f4ae6dc10a67a0a5726f449c6c65e288d39f57f add hello CSDN to test.txt
78db795dabdeb752d635ed2960b87ec49bb13943 add hello world to test.txt
39e0dba93090a1f01887180e7bedbb7dda1fe5b3 add test3.txt file
63d890ed08ee938dc4e83ad14446727dc9d55da3 add test1.txt and test2.txt files
489b113d392d1bc930f6eef1b6f0135d6bd6e0d3 add panoramaProjects folder
664376a8783c533c90731dd00e4093d8bff9e97b add test.txt file chenxi@chenxi_pc MINGW64 /G/Git (master)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

是不是把目前不需要的信息都省掉了?每条信息只给出版本号跟我们添加的日志描述信息。如果你觉得最后一次操作有问题(添加的hello CSDN),想返回到前面的按个版本,怎么办?

只需要执行指令:“git reset –hard HEAD^”

HEAD前面说过有个HEAD指针指向最新提交的版本,HEAD^表示当前版本的前面那个版本,HEAD^^表示前前个版本,如果想返回前面第100个版本,是不是可以写100个“^”。

O(∩_∩)O哈哈~,让我想起了以前小时候老师讲的一个故事,一个小学生学习了中文的一、二、三,他就说后面的他都会了,就没去上课,回家他老爸让他写一个万字,他写了一个下午。(小插曲)

当然上面的方法也可以,不过我们都没那么蠢吧。我们可以使用HEAD~100代替。 
git 仓库原理 
如上图,我们执行了git reset –hard HEAD^后,是不是返回到前面那个版本了“78db795dabdeb752d635ed2960b87ec49bb13943 add hello world to test.txt 
” 
再去查看一下我们的test.txt文件是不是也回到前面那个版本,如下图: 
git 仓库原理

惨了,我们前面那个版本了(add hello CSDN…),怎么办? 
不慌不慌,我们还可以通过前面的版本ID号返回去呢。版本号辣么长,我要晕了…放心,git不会这么麻烦的,你只需要输入其中连续的几个版本号字符就可以了(一般6位),这样git就能辨别了。 
如下图: 
git 仓库原理 
又回来了,开不开心?O(∩_∩)O哈哈~!当然你也可以通过版本号返回到其他的版本。

git 仓库原理

在master去,有个HEAD指针,该指针指向当前的版本,返回到第n个版本就是通过修改HEAD指针的值实现,如上图。

删除文件

小结

  • git版本库主要的三个工作区域工作区、stage、master。
  • git status查询当前状态
  • 通过git reset –hard HEAD^返回以前版本的信息
  • 通过git reset –hard 版本号返回版本号对应的版本

git 仓库原理的更多相关文章

  1. 从&period;git文件夹探析git实现原理

    git是一款分布式代码版本管理工具,通过git能够更加高效地协同编程.了解git的工作原理将有助于我们使用git工具更好地管理项目.通过了解.git文件夹中的文件组成,我们可以从一个角度去窥探git的 ...

  2. Git详解之九:Git内部原理

    Git 内部原理 不管你是从前面的章节直接跳到了本章,还是读完了其余各章一直到这,你都将在本章见识 Git 的内部工作原理和实现方式.我个人发现学习这些内容对于理解 Git 的用处和强大是非常重要的, ...

  3. android Git命令家底儿及Git数据通信原理详解

    声明:本文为CSDN原创投稿文章,未经许可,禁止任何形式的转载. 现在大部分使用的都是SVN,也有一部分迁移了Git,虽然挺好的,不过还有其它很多版本控制的工具,并没有谁最好用,最重要的是适合自己的公 ...

  4. GIT仓库如何恢复到前一次提交

    GIT仓库如何恢复到前一次提交   通过使用Git版本恢复命令reset,可以回退版本.reset命令有3种方式: git reset –mixed:此为默认方式,不带任何参数的git reset,即 ...

  5. Git的原理简介和常用命令

    Git和SVN是我们最常用的版本控制系(Version Control System, VCS),当然,除了这二者之外还有许多其他的VCS,例如早期的CVS等.顾名思义,版本控制系统主要就是控制.协调 ...

  6. git内部原理

    Git 内部原理 无论是从之前的章节直接跳到本章,还是读完了其余章节一直到这——你都将在本章见识到 Git 的内部工作原理 和实现方式. 我们发现学习这部分内容对于理解 Git 的用途和强大至关重要. ...

  7. Git详解之九 Git内部原理

    以下内容转载自:http://www.open-open.com/lib/view/open1328070620202.html Git 内部原理 不管你是从前面的章节直接跳到了本章,还是读完了其余各 ...

  8. &lbrack;原创&rsqb;SSH密钥访问Git仓库配置

    SSH密钥并非为了解决拉取git仓库代码时,需要频繁输入密码的问题. SSH是一种比较安全的协议,可以用来免去远程登录Linux等服务器时需要输入密码的繁琐过程. 命令: ssh user@serve ...

  9. Git 内部原理 - &lpar;7&rpar;维护与数据恢复 &lpar;8&rpar; 环境变量 (9)总结

    维护与数据恢复 有的时候,你需要对仓库进行清理 - 使它的结构变得更紧凑,或是对导入的仓库进行清理,或是恢复丢失的内容. 这个小节将会介绍这些情况中的一部分. 维护 Git 会不定时地自动运行一个叫做 ...

随机推荐

  1. 模拟提交API数据Pyqt版

    其实这个模拟提交数据之前已经写过篇: Python requests模拟登录 因为现在在做的项目中需要一个debug请求调试API,用PHP的CURL写了一个,又因Pyqt更能直观灵活的显示请求的参数 ...

  2. 阐述ArrayList、Vector、LinkedList的存储性能和特性。

    答:ArrayList 和Vector都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内存操作,所以索引数据快 ...

  3. C&num; Arraylist &plus; struct 综合练习 枚举ENUE 递归

    枚举类型 一组常量的组合, 在不制定任何索引的情况下,默认第一个字段从0开始,之后的依次+1 在指定了某个索引的情况下,之后的依次+1 若之前定义的某字段的索引指向了之后的某个默认字段,那么他俩完全相 ...

  4. Muduo 多线程模型对比

    本文主要对比Muduo多线程模型方案8 和方案9 . 方案8:reactor + thread pool ,有一个线程来充当reactor 接受连接分发事件,将要处理的事件分配给thread pool ...

  5. I Think I Need a Houseboat 分类: POJ 2015-06-11 17&colon;52 12人阅读 评论&lpar;0&rpar; 收藏

    I Think I Need a Houseboat Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 92090   Acce ...

  6. Python顺序集合之 List

    Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用一个list表示: >>> ['Jen ...

  7. &lbrack;翻译&rsqb;在Windows版或MacOS版的Microsoft Edge上安装一个谷歌浏览器拓展

    原文:Install a Chrome Web Store extension on Microsoft Edge for Windows and MacOS 拓展阅读:What to expect ...

  8. 凉凉了,Eureka 宣布闭源,Spring Cloud 何去何从?

    今年 Dubbo 活了,并且被 Apache 收了.同时很不幸,Spring Cloud 下的 Netflix Eureka 组件项目居然宣布闭源了.. 已经从 Dubbo 迁移至 Spring Cl ...

  9. 关于poi导出excel方式HSSFWorkbook&lpar;xls&rpar;&period;XSSFWorkbook&lpar;xlsx&rpar;&period;SXSSFWorkbook&period;csv的总结

    1.HSSFWorkbook(xls) import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermo ...

  10. linux系统调用的三种方法

    通过glibc提供的库函数 [23:02:14] gcc chmodtest.c [23:02:17] ls -l kali //记得先创建这个文件 -rwxrwxrwx. 1 root root 0 ...