1、git基础 2、git安装使用 ================================================================================================================================================ 1、what is git? git is directory content Management system. tree history storage system. stupid content tracker. 内容版本管理系统 delta storage:只保存变化的数据 local centralized distributed DAG storage:保存变化了的整个文件 local centralized distributed 2、工作方式 在工作目录下有一个 .git 的目录,工作目录下的所有文件都在 .git 目录下有一份副本。当前的工作目录反映的是某一个时间点的数据。 (git direcroty) config file # 当前git仓库的配置文件 hooks # 类似于触发器或侦听器,当外部事件发生变化时,执行什么操作 index # 从工作目录到仓库目录建立映射关系 object database # 对象数据库 references # 3、object database 1、对象存储,文件数据和元数据存放在一起。对象存储的方式为:new_content = type + ' ' + content.size + \0 + content # {type + ' ' + content.size + \0} :此部分内容称为 "header" # 将所有内容组合起来即 new_content 做哈希计算,得到一个哈希值 # 将数据进行压缩 # 文件名的哈希值的前两位作为一级子目录名 2、4类object tree # 保存目录结构 blob # 保存文件内容。二进制大对象,保存项目本身内容 commit # 提交。每一个提交都是一个哈希值 tag # commit别名,便于引用对象,标签 每一次的提交时,如果项目内容发生变化了的内容,则新生成bolb且提交指向变化了的内容,如果未发生变化,则指向旧版本的内容。 每一次提交,则都指向前次的提交。 5、git目录结构 gittest/ # 工作目录 ├── file.txt └── .git # 版本库 ├── branches ├── config ├── description ├── HEAD # 指向当前的分支 ├── hooks │   ├── applypatch-msg.sample │   ├── commit-msg.sample │   ├── post-update.sample │   ├── pre-applypatch.sample │   ├── pre-commit.sample │   ├── prepare-commit-msg.sample │   ├── pre-push.sample │   ├── pre-rebase.sample │   └── update.sample ├── index # 暂存区,stage。存放最近一次纳入版本库的文件的文件状态 ├── info │   └── exclude ├── objects # 本地版本库 │   ├── 10 │   │   └── 6287c47fd25ad9a0874670a0d5c6eacf1bfe4e │   ├── 97 │   │   └── 46ff81c5c0a531b5b33c48f4fbf3cf636eeec8 │   ├── info │   └── pack └── refs ├── heads # 当前分支的名称 └── tags 6、git配置文件 仓库特有:REPO/.git/config 用户全局:~/.gitconfig --global 系统:/etc/git/gitconfig --system 7、公共仓库支持通信的协议 基于网络协议:http/https, ssh, git,local Git服务器: 协议:本地协议(local)、HTTP/HTTPS协议、SSH协议、Git协议; 1、本地协议: URL: /path/to/repo.git file:///path/to/repo.git 2、Git协议 由git-daemon程序提供,监听在tcp的9418端口;仅支持“读”操作,无任何认证功能; URL: git://host/path/to/repo.git git://host/~user/path/to/repo.git 3、SSH协议 URL ssh://[USER@]host[:port]/path/to/repo.git ssh://[USER@]host[:port]/~USERNAME/path/to/repo.git URL2 [USER@host]/hostpath/to/repo.git git pull:git fetch, git merge git push: 4、HTTP/HTTPS协议 1.6.5-:哑http协议 1.6.6+:智能http协议 读/写/认证 URL: http://host/path/to/repo.git ================================================================================================================================================ git安装使用 1、安装 yum install git 2、git命令 git init # 初始化仓库 git status # 查看当前git状态 git config -l # 列出当前配置 3、git配置命令 man git-config git config --global user.name xuekaixin git config --global user.email jinweiayy@gmail.com 4、提交 git commit -m "version 1.0" # 提交 git log # 查看提交日志 git tag v1 185692b23e0c9676cd851339f18c36b1fd863dd5 # 给分支添加一个标签 git tag -l # 查看当前分支的标签 git teset --soft # 只撤销commit操作,暂存区的内容还是原样 git teset --mixed # commit和暂存区都撤销 git teset --hard # commit、暂存区、工作目录都撤销到上一次提交的时刻 # 创建新分支 git checkout -b story2 git branch branch_name # 显示分支 git branch --list # 切换分支 git checkout master # 合并分支 git merge story2 # 克隆远程仓库 git clone git clone git://172.18.26.1/ops.git # 创建远程仓库,将.git目录复制到 /usr/lib/git/目录下 yum install git-daemon # 显示远程分支 git remote ============================================================================================================ 将目录下的所有文件上传,也可以将“.”换成具体的文件名 git add . 将项目提交到本地仓库 git commit -m "注释语句" 将本地的代码关联到github上 git remote add origin 项目的github地址 上传代码到github之前需要先pull git pull origin master 上传代码到远程git仓库 git push -u origin master =============================================================================================================