您的位置:首页 > 其它

GIT版本控制 — 简介与安装 (一)

2015-02-14 15:46 253 查看

简介

GIT与SVN的区别

作为当前最流行的版本控制系统,Git和SVN的几个主要不同之处在于:

(1) Git是分布式的版本控制系统,SVN是集中式的版本控制系统。Git可以先把修改提交到本地仓库中,

等到可以上网或方便的时候,再提交到远程仓库中。这无疑更利于项目开发,同时也可减轻服务器负担。

(2) SVN使用增量文件系统(Delta Storage Systems),即它存储的是每次提交(commit)之间的差异。

Git正好相反,它会把你的每次提交文件的全部内容(snapshot)都记录下来。

(3) Git对于分支有更好的支持,能自动记录分支的合并信息。SVN只能手动追踪,很不方便。

(4) SVN每个目录下都存在.svn目录,而Git的信息只存在项目根目录下的.git目录。

(5) Git的速度更快,不管是本地操作还是远程操作。

对象类型

Git有四种类型的对象:blob、tree、commit、tag。

对象名称用哈希值来表示,SHA1哈希值有40个字符,但是一般用前6个就可以找到相应的对象了。

(1) blob

一个blob用来存储文件的内容。

一个blob对象就是一块二进制数据,它没有指向任何东西或带有任何其它属性。

blob对象和其所对应的文件所在路径、文件名是否被改变都完全没关系,它只是一个数据块而已。

(2) tree

一个tree用于表示内容之间的目录层次关系,相当于文件夹。

A tree is a simple object that has a bunch of pointers to blobs and other trees。

(3) commit

commit对象指向一个tree对象,并且带有相关的描述信息。

The commit object links a physical state of a tree with a description of how we got there and why.

commit的生成:

A commit is usually created by git commit, which creates a commit whose parent is normally the

current HEAD, and whose tree is taken from the content currently stored in the index.

(4) tag

tag存储一个指针,指向某个commit。

Git存储内容的组织结构如下:

每个目录都创建了tree对象(包括根目录),每个文件都创建了一个对应的blob对象。最后只有一个commit对象

来指向根tree对象,这样我们就可以追踪项目每一项提交(commit)的内容。而tag则是特殊的commit。

git目录

每一个项目只有一个Git目录(.git),这和SVN的每个子目录中都有此类目录不同,它位于项目的根目录下。

The Git Directory is the directory that stores all Git's history and meta information for your project -

including all of the objects (commits, trees, blobs, tags), all of the pointers to where different branches

are and more.

在.git目录中,有以下重要文件:

HEAD:pointer to your current branch

config:your configuration preferences

description:description of your project

index:index file,暂存区

logs:a history of where your branches have been

objects:your objects (commits, trees, blobs, tags)

refs:pointers to your branches

工作目录

The Working Directory is the directory that holds the current checkout of the files you are working on.

当你在项目的不同分支间切换时,工作目录里面的文件经常会被替换和删除。所有历史信息都保存在Git目录中,

工作目录只用来临时保存签出(checkout)的文件,你可以编辑工作目录的文件直到下次提交为止。

暂存区

暂存区就是.git/index。

The Git index (.git/index) is used as a staging area between your working directory and your repository.

有了它,你可以把许多内容的修改一起提交(commit)。如果你创建了一个提交,那么提交的是当前暂存区(index)

里面的内容,而不是工作目录中的内容,这又是一个和SVN的不同之处。

下图说明一个commit是怎么产生的:



使用git status可以查看暂存区(index)的内容,包括:

哪些文件被暂存了(staged)

哪些文件被修改了但是没有被暂存(modified, unstaged)

哪些文件没有被跟踪(untracked)

安装

下载地址列表:http://code.google.com/p/git-core/downloads/list

源码包:git-1.9.0.tar.gz

事先需要安装的库:expat、curl、zlib、openssl、zlib-devel

# tar -zxvf git-1.8.0.tar.gz

# cd git-1.8.0

# ./configure --prefix=/usr/local/git

# make && make install

然后设置PATH

在/etc/profile或者~/.bashrc中添加:

export PATH=/usr/local/git/bin:/usr/local/libexec/git-core:$PATH

然后使配置生效:

# . /etc/profile

配置

使用Git的第一件事就是设置你的名字和email,这些就是你在提交commit时的签名。

Git的配置信息分为全局和项目两种,--global参数意味着在进行全局配置,它会影响本机上的每一个Git项目。

全局配置文件:/root/.gitconfig

# git config --global user.name zhangskd

# git config --global user.email
zhangskd@xxx.com

项目配置文件:项目/.git/config

# git config user.name zhangskd

# git config user.email
zhangskd@xxx.com

也可以直接编辑config文件:

[core]

repositoryformatversion = 0

filemode = true

bare = false

logallrefupdates = true

[user]

name = zhangskd

email =
zhangskd@xxx.com

Author

zhangskd @ csdn blog
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: