git工具的优势
DVCS,distribution version control system,分布式版本控制,优于集中式版本控制系统,方便修改,易于保存,随时随地办公,数据更安全
1. ubuntu安装git
If you’re on a Debian-based distribution like Ubuntu, try apt-get:
$ sudo apt-get install git-all
2. 源码安装git需要软件包
If you do want to install Git from source, you need to have the following libraries that Git depends on: autotools, curl, zlib, openssl, expat, and libiconv
源码安装时,需要commit各种格式的文件到仓库时,需要以下软件包
In order to be able to add the documentation in various formats (doc, html, info), these additional dependencies are required (Note: users of RHEL and RHEL-derivatives like CentOS and Scientific Linux will have to [enable the EPEL repository]
$ sudo apt-get install asciidoc xmlto docbook2x getopt
3. 首次使用git command的设置
利用命令git config来进行设置,全局设置加上--global
(1) 设置用户名
git config --global user.name "jrg"
(2) 设置email
git config --global user.email xxxxxxxx@qq.com
(3) 设置默认文件编辑器
git config --global core.editor vim
(4) 设置完成后查看
git config --list
git设置
4. 创建本地git仓库
$ cd /home/jrg/jrgrepository/$ git init生成.git文件夹,利用
ls -Fl命令可看到其中的内容
.git文件夹
其中:
description: is only used by the GitWeb program, so don’t worry about it. config: contains your project-specific configuration options
info:directory keeps a global exclude file for ignored patterns that you don’t want to track in a .gitignore file.
hooks: contains your client- or server-side hook scripts
This leaves four important entries:
**HEAD and (yet to be created) index files, and the objects and refs directories. These are the core parts of Git. **The objects directory stores all the content for your database, the refs directory stores pointers into commit objects in that data (branches), the HEAD file points to the branch you currently have checked out, and the index file is where Git stores your staging area information. You’ll now look at each of these sections in detail to see how Git operates.
objects文件夹存放着数据仓库里的所有内容
refs文件夹存放指向数据库中已存储对象的指针
HEAD指向刚刚检查过的git分支
Git基础
Git仓库的一次commit记录目录下所有文件的snapshots,提交并不是简单的复制,Git会把提交压缩成从代码库的一个version到下一个version的变化合集,也叫增量(delta)
Git分支
- 创建分支
Git分支轻量化,创建分支没有存储和内存的开销,常用分支
git branch <name>创建分支
git checkout <name>切换分支,默认分支master的指向并没有移动 - 合并分支
一个简单例子:两个分支距离共同的父提交只有一次提交的情况的合并(较简单)
方法1:
如图初始的版本
最初版本
命令:$ git branch bugFix创建新分支
git branch bugFix
命令:$ git checkout bugFix切换到bugFix分支(注意*号位置,也即HEAD指针指向位置)
git checkout bugFix
命令:$ git commit提交一次,bugFix往前移动
git commit
命令:$ git checkout master切换至master分支,HEAD指针移动
git checkout master
命令:$ git commit再次提交,出现分叉
git commit
命令:$ git merge bugFix合并bugFix分支
git merge bugFix
合并分支的另一种方法:git rebase <target name>
原始版本
$ git branch bugFix创建分支bugFix
git branch bugFix
$ git commit提交一次
git commit
$ git checkout bugFix切换至bugFix
git checkout bugFix
$ git commit再次提交
git commit
$ git rebase master将bugFix分支工作直接移接到master分支下
git rebase master
$ git checkout master
git checkout master
$ git rebase bugFix更新master分支
git rebaser bugFix
Git的几个特性
- 分离HEAD
HEAD 总是指向最近一次提交记录,表现为当前工作树
git checkout <hash值或者是分支名称> - 相对引用
git checkout HEAD^往上移动一级
git checkout HEAD^^往上移动二级 - 相对引用
“~<数值>”
git checkout HEAD~3往上移动3级 - 撤销更改
本地撤销:git reset HEAD
远端撤销:git revert HEAD
Git复制(rebase也可以)
git cherry-pick <hash值>
原始版本
执行命令
$ git cherry-pick C3 C4 C7
git cherry-pick C3 C4 C7
如果复制的时候不知道<hash值>(实际hash值会是一长串字符,而不是此处的C3之类的代号)
利用rebase -i命令
交互式 rebase 指的是 rebase 后跟一个参数: -i
如果你包含了这个选项, Git 会开启一个 UI 并 展示出将要被复制到目标的提交对象, 它也会显示它们的提交 hash 和信息
真实的 Git, UI 窗口指的是在类似于 Vim 的文本编辑器中打开一个文件
例子:
原始状态
执行命令
$ git rebase -i HEAD~4
git rebase -i HEAD~4
重新排序并反选C2,得到如下结果
结果
打标签
git tag <标签>
服务器上的Git
protocols:
Local,Secure Shell(SSH),Git,and HTTP(s)
https协议需要到ca申请证书,一般免费证书很少,需要交费。
http是超文本传输协议,信息是明文传输,https 则是具有安全性的ssl加密传输协议 http和https使用的是完全不同的连接方式用的端口也不一样:前者是80,后者是443。
http的连接很简单,是无状态的 HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、身份认证的网络协议 要比http协议安全












网友评论