本文git为1.9.6版本:git
1、git全局配置bash
2、git初始化本地仓库服务器
3、git文件状态详解yii
4、git文件撤销、恢复操做
ide
git提交流程层次以下:测试
git repository 视为线上code集中管理服务器;也就是咱们的code最后的位置;ui
git staging area 视为咱们本地的code集中管理服务器,可认为code的中间的暂留区(以相对repository来讲);
spa
git working directory 视为咱们本地编辑的code,也就是代码编辑处;3d
1:全局配置版本控制
leo@LEO-PC /d/User/leo/Desktop/git (master) $ git config --global user.name "lansgg" leo@LEO-PC /d/User/leo/Desktop/git (master) $ git config --global user.email "coffee_lanshan@sina.com"
这里配置 --global选项其实就是在修改家目录下的.getconfig文件
如个人:
C:\Users\leo\.getconfig
内容:
[user] name = lansgg email = coffee_lanshan@sina.com [color] ui = true [core] autocrlf = true excludesfile = C:\\Users\\leo\\Documents\\gitignore_global.txt
当咱们修改此文件,好比将name=test (内容已经修改)
$ git config --get user.name
这里的用户名及邮件地址都是在提交代码的时候进行标识的,显示提交人的信息;
2:初始化本地git仓库
本地新建一个目录;执行git init ;
$ mkdir git $ git init
执行后的变化就是多了一个.git目录;
leo@LEO-PC /d/User/leo/Desktop/git (master) $ ls -a .git . COMMIT_EDITMSG config hooks info objects .. HEAD description index logs refs leo@LEO-PC /d/User/leo/Desktop/git (master) $
.git 目录,其中存放的是咱们所提交的文档索引内容,Git 可基于文档索引内容对其所管理的文档进行内容追踪,从而实现文档的版本控制。.git目录位于工做目录内。
index(索引):将工做目录下全部文件(包含子目录)生成快照,存放到一个临时的存储区域,Git 称该区域为索引。
三、git文件状态详解
3.一、如今新建咱们的code;
leo@LEO-PC /d/User/leo/Desktop/git (master) $ touch README.txt leo@LEO-PC /d/User/leo/Desktop/git (master) $ cat hello.rb puts "hello world!"
咱们要将he.rb 、README 提交到本地git repository;
3.二、首先查看文件的状态
$ git status
或
$ git status -s
根据输出信息能够看出,README.txt hello.rb 两个文件处于master branch(主分支),这两个文件处于未追踪文件,也就当咱们执行git commit(用于提交到图中的仓库)命令的时候不会将他们提交到git repository;
上图 -s 表示简要信息;( ?? )两个问号也有很重要的意义;
第一个 ? 主要表示staging area 和 repository 两个区域间的文件变化,通常会有两个字母来表示(A、M <绿色>);A 表示此文件已是在追踪文件,M 表示此文件已经在staging area区域修改,尚未提交到repository。
第二个 ? 主要表示working directory 和 staging area 两个区域间的文件变化,M <红色> 表示此文件已经working directory区域修改,尚未提交到staging area
下面开始演示:git add 表示将文件进行追踪;
$ git add README.txt $ git add hello.rb
再次查看文件状态
$ git status -s
能够看到两个文件已经处于 A 状态 (追踪)如今这两个文件处于staging area区域; changes to be committed 能够进行提交了;输出信息提示,能够使用git reset HEAD <file> 将文件恢复于未追踪状态;
恢复到已经追踪的状态,进行提交测试;
$ git commit -m "first commit" #-m 表示提交此代码时进行描述;咱们这里描述为“first commit”
能够看到wording directory clean 已经将此两个文件提交到repository;(从staging area区域),并查看文件状态时,不在输出任何东西
3.2 修改本地code,再查看文件状态
$ echo 'puts "hello world!" '>> hello.rb
$ git status -s
第二个 ? 的地方出现了M <红色> 表示此文件已经在working directory区域修改;输出信息提示,hello.rb文件已经modified ; 命令 git add && git checkout && git commit -a (图示都有) 下面讲;
如何将此文件提交到repository
$ git add hello.rb $ git commit -m "second commit" hello.rb
或者使用:
$ git commit -a -m "second commit"
此命令将跳过git add 这一步~
四、撤销、恢复
4.一、修改本地working directory 的code ;而后撤销修改,也就说从staging area区域取出此code覆盖当前working directory的code;
测试以下:
$ echo 'puts "hello world!"' >> hello.rb #修改本地code $ git status -s #查看文件的状态,有差别 $ git checkout hello.rb #从staging area区域取出此code $ git status -s #再次查看该文件的状态,无差别,而且代码恢复到了以前的代码
4.二、修改本地working directory的code,而且进行追踪(add 到 staging area区域);而后咱们想撤销本地code的修改,那咱们能够从repository仓库拉出此code覆盖到staging area,而后再从staging area区域取出覆盖到working directory区域;测试以下:
$ echo 'puts "hello world!"' >> hello.rb $ git status -s $ git add hello.rb $ git status -s $ git reset hello.rb $ git status -s $ git checkout hello.rb $ git status -s # 每一步都进行了文件状态差别的核对;
咱们也能够不用这么麻烦,能够直接从 repository 区域拉取出来直接覆盖到 working directory 区域:
$ echo 'puts "hello world!"' >> hello.rb $ git status -s $ git add hello.rb $ git status -s $ git checkout HEAD hello.rb $ git status -s
能够看到已经从git repository 拉取此文件并进行了覆盖~