圣泉山人 后端开发工程师

Git

2016-01-03

Git是什么?

Git是 Linus 花了两周时间用C语言编写的一款非常优秀的分布式版本控制系统。其诞生充满了传奇色彩和开源精神。而GitHub 网站为开源项目免费提供Git存储。

和传统集中式版本控制系统,比如SVN的区别:

PK git SVN
优点 任何人每次提取操作,实际上都是一次对代码仓库的完整备份,因此近乎所有的操作都可以在本地执行,速度很快;断网也可以频繁的进行提交更新,等到有网络的时候再上传到远程的镜像仓库就可以了;分支模型很优秀 有一个单一的集中管理的服务器,使用简单
缺点 每个人都可以获取完整代码,不利于核心代码的保密 由于太依赖版本库的服务器,丢失数据风险大

Git的安装

Ubuntu下的安装:

 $ apt-get install git   

Centos下的安装:

yum -y install git

# 查看版本号
$ git --version
git version 1.7.1

windows下的安装:

  • 下载git
  • 选择默认选项即可完成安装
  • 安装完成后在开始菜单里找到Git->Git Bash,能弹出一个命令行窗口即表示安装成功

安装完成后的配置:

# 配置用户名和邮箱,自己定义即可, --global 会让当前电脑上的所有git仓库都使用此配置  
git config --global user.name "mayy"
git config --global user.email "532123@qq.com"

Git的理解

  • 仓库: :能被git管理的目录,使用git init就可以得到一个仓库
  • 工作区: git仓库里面的项目文件区域
  • 版本库: 创建一个git仓库后生成.git文件夹
  • 暂存区: 使用git add <file>后会把文件添加到暂存区
  • master分支: git自动创建的第一个分支使用git commit后才会把暂存区的文件提交到分支去进行管理
  • HEAD: 类似指针,用来管理版本,比如回退版本

img

Git的使用

仓库管理

本地仓库

仓库:能被git管理的目录,git可以对在仓库的文件的删除、修改操作进行管理,追踪等。

相关命令

  • git init : 初始化仓库
  • git status : 看看仓库的当前状态
  • git add <file> : 将工作文件修改提交到本地暂存区
  • git add . : 将所有修改过的工作文件提交暂存区
  • git rm <file> : 从版本库中删除文件
  • git commit -m : 提交文件到分支
  • git log : 显示从最近到最远的提交日志

USAGE

#初始化仓库
git init

#设置用户名和邮箱
git config --global user.name "myy"
git config --global user.email "545825@qq.com"

#添加文件到暂存区
git add test.php

#提交到分支
git commit -m 'add test.php'

#误删或者修改错了工作区的test.php,从版本库里面修复出来,注意:-- 
git checkout -- test.php

#删除版本库里面的文件(比如工作区文件已被删除后)
git rm test.php

注意: 如果在window下编辑的文本文件,在添加时可能会出现如下错误:

$ git add test.php
warning: LF will be replaced by CRLF in test.php.
The file will have its original line endings in your working directory.

是因为windows中的换行符为 CRLF, 而在linux下的换行符为LF,解决方法:

$ rm -rf .git  // 删除.git  
$ git config --global core.autocrlf false  //禁用自动转换

//删除后重新操作即可
$ git init
$ git add test.php

远程仓库

关联远程仓库

  • 在本地创建一个仓库,用来同步远程仓库,比如myyphp,记得设置用户名邮箱
  • github上面创建好一个仓库,myyshop
  • 关联远程仓库(使用github创建仓库后的提示命令即可)git remote add origin https://github.com/myyphp/myyshop.git
  • 把已经commit到本地仓库的文件推到远程仓库:git push -u origin master,根据提示输入github的用户名和密码即可

克隆远程仓库

把github上面的远程仓库克隆到本地:git clone https://github.com/myyphp/myyshop.git

版本管理

版本的回退或者返回都是依靠HEAD指针的移动,比如当前HEAD指向的是最新的版本3

image

想要回退到版本2,需要执行命令:

$ git reset --hard HEAD^    # ^ 表示上一个版本,^^上两个版本,多个的可以使用HEAD~n  (n是具体的数字)

image

分支管理

分支管理原理图:

img

相关命令

  • git branch : 查看所有分支
  • git branch <name> :创建分支
  • git checkout <name> :切换分支
  • git checkout -b <name> :创建并且切换分支
  • git merge <name> :合并某分支到当前分支
  • git branch -d <name> :删除分支

USAGE

#创建一个分支:test
$ git branch test

#查看当前所有分支,在当前分支前会添加星号标识
$ git branch
* master
  test
  
#切换到test分支
$ git checkout test
Switched to branch 'test'

#上面两步操作(创建并切换分支)可以用一条命令完成
$ git checkout -b test

#此时在test分支下开发了一些东西后,把代码合并回master分支,需要先切到master分支
$ git checkout master
Switched to branch 'master'

$ git merge test
Updating bcae98f..722e73f
Fast-forward
 v1.php | 1 +
 1 file changed, 1 insertion(+)

#在分支上面的开发完成后删除该分支
$ git branch -d test
Deleted branch test (was 722e73f).

合并时发生冲突:

$ git merge test
Auto-merging v1.php
CONFLICT (content): Merge conflict in v1.php
Automatic merge failed; fix conflicts and then commit the result.

1、查看冲突文件内容:

<?php
<<<<<<< HEAD
echo 'hello z';
=======
echo 'hello zzdad';
>>>>>>> test
echo 'world';

2、手动修改冲突,合并代码

<?php
echo 'hello z';
echo 'world';

3、解决冲突后,把修改后的文件提交到分支中去

$ git add v1.php
$ git commint -m 'test'

使用git merge合并某开发分支时,git通常会使用Fast forward模式进行合并,该模式直接把master指向分支的当前提交,这时,如果删除了该开发分支,就会丢失该分支的信息。

合并时使用--no-ff参数,禁用Fast forward模式。Git就会在merge时生成一个新的commit,这样,从分支历史上就可以看出分支信息。例如:

$ git merge --no-ff -m "merge with no-ff" test

标签管理

commit后,会生成一串唯一的没有规律的、难以记忆的id号,但是又非常重要,因为版本切换的时候,全靠该id号来定位。此时就可以通过给这个id “取别名” 的方式来记录。

Git的标签(tag) 本质上是指向某个 commit 的指针

相关命令

  • git tag <name> :新建一个标签,默认为HEAD,也可以指定一个commit id
  • git tag -a <tagname> -m "blablabla..." : 可以指定标签信息
  • git tag -s <tagname> -m "blablabla..." : 可以用PGP签名标签
  • git tag :查看所有标签
  • git show <tagname> : 查看标签详细信息
  • git tag -d <tagname> : 删除标签
  • git push origin <tagname> :推送一个本地标签
  • git push origin --tags : 推送全部未推送过的本地标签
  • git push origin :refs/tags/<tagname> : 删除一个远程标签

USAGE

#当前已经提交状态
$ git log
commit 6be72ead7256f784ae5496c17e72a36d82d396d0
Author: mayy <myyd@outlook.com>
Date:   Mon Aug 8 21:59:28 2016 +0800

    add a php file

#创建标签,可以使用 -m 指定备注信息
$ git v1.0

#查看所有标签
$ git tag
v1.0

#查看指定标签
$ git show v1.0
commit 6be72ead7256f784ae5496c17e72a36d82d396d0
Author: mayy <myyd@outlook.com>
Date:   Mon Aug 8 21:59:28 2016 +0800

    add a php file

diff --git a/test.php b/test.php
new file mode 100644
index 0000000..cf8d474
--- /dev/null
+++ b/test.php
@@ -0,0 +1,2 @@
+<?php
+echo 'hello world';

#删除标签
$ git tag -d v1.1
Deleted tag 'v1.1' (was 10cde99)

自定义配置

如果不想某些文件被提交,可以忽略这些文件:

  • 在工作区根目录创建 .gitignore 文件
  • .gitignore 文件里面配置要忽略的文件

USAGE

# 忽略所有txt格式的文件
$ vi .gitignore

*.txt

github提提供的一些编写好的配置参考:https://github.com/github/gitignore

附录:Git 命令速查

  • master: 默认开发分支
  • origin: 默认远程版本库
  • Head: 默认开发分支
  • Head^: Head的父提交

创建版本库

  • git clone <url> :克隆远程版本库
  • git init :初始化本地版本库

修改和提交

  • git status :查看状态
  • git diff :查看变更内容
  • git add . :跟踪所有改动过的文件
  • git add <file> :跟踪指定的文件
  • git mv <old><new> :文件改名
  • git rm<file> :删除文件
  • git rm --cached<file> :停止跟踪文件但不删除
  • git commit -m "commit messages" :提交所有更新过的文件
  • git commit --amend :修改最后一次改动

查看提交历史

  • git log :查看提交历史
  • git log -p <file> :查看指定文件的提交历史
  • git blame <file> :以列表方式查看指定文件的提交历史

撤销

  • git reset --hard HEAD :撤销工作目录中所有未提交文件的修改内容
  • git checkout HEAD <file> :撤销指定的未提交文件的修改内容
  • git revert <commit> :撤销指定的提交
  • git log --before="1 days" :退回到之前1天的版本

分支与标签

  • git branch :显示所有本地分支
  • git checkout <branch/tag> :切换到指定分支和标签
  • git branch <new-branch> :创建新分支
  • git branch -d <branch> :删除本地分支
  • git tag :列出所有本地标签
  • git tag <tagname> :基于最新提交创建标签
  • git tag -d <tagname> :删除标签

合并与衍合

  • git merge <branch> :合并指定分支到当前分支
  • git rebase <branch> :衍合指定分支到当前分支

远程操作

  • git remote -v :查看远程版本库信息
  • git remote show <remote> :查看指定远程版本库信息
  • git remote add <remote> <url> :添加远程版本库
  • git fetch <remote> :从远程库获取代码
  • git pull <remote> <branch> :下载代码及快速合并
  • git push <remote> <branch> :上传代码及快速合并
  • git push <remote> :<branch/tag-name> :删除远程分支或标签
  • git push --tags :上传所有标签

参考: http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000


上一篇 LAMP环境搭建

Comments

Content