git 快速入门笔记
## 第1章 给心急者
### 1.1 git是什么
git是一种版本控制器.
更直白说,团队开发时,管理代码用的软件.
面试时,容易被问到的一个东西.
### 1.2 安装
git在Linux,Mac,Win下都可以安装.
本文是以Win7系统为环境编写的.
Window环境:
到 https://git-scm.com/download 下载软件, 双击,一路"Next",安装完毕.
到开始菜单找"git bash",如下图
![](./images/1.png)
看到如下界面:
![](./images/2.png)
Linux环境安装git:
```bash
# ubuntu,debian#
$ sudo apt-get install git
```
centos,redhat系统
```bash
# yum install git
```
### 1.3 报家门
人在江湖,岂能没有名号.
开源教主Richard Matthew Stallman的江湖名号RMS.
在你用git之前,要先报家门,否则代码不能提交.
```bash
$ git config --global user.name #你是谁
$ git config --global user.email #怎么联系你
```
![](./images/3.png)
### 1.4 代码管理
#### 1.4.1 创建版本库
```bash
$ cd E:/
$ mkdir test
$ git init
```
**注意:**
+ 不要把仓库建在中文目录下,可能出问题.
+ `.git`是个隐藏目录,不要乱碰.(你的每一次代码修改它都帮你记录着呢)
#### 1.4.2 添加文件
在E:/test目录下,用你喜欢的编辑器(sublime/editplus/notepad,vim等),
开发你的程序. 比如,`index.php`
```php
echo 'hello git';
```
![](./images/4.png)
编辑PHP文件后, `# git status` , 查看仓库状态
实例如下:
```bash
$ git status
```
可见,此时git发现有一个新文件,但并没有把此文件纳入管理.
我们需要两步,让git仓库管理index.php
+ `git add index.php`
把`index.php`提交到**暂存区**
+ `git add .`
把所有文件提交到**版本库**
+ `git commit -m "新建index.php"`
把`index.php`提交到**版本库**
实例如下:
```bash
$ git add index.php 添加单个文件
$ git add . 添加所有文件
$ git commit -m "新建index.php"
```
![](./images/5.png)
#### 1.4.3 修改文件
如果修改了文件,也不要忘记提交到版本库
这个过程**和添加文件是一样的**
一样是需要两步,让git仓库记录此次改变
+ `git add index.php`
把`index.php`提交到**暂存区**
+ `git commit -m "改了第2行"`
把`index.php`提交到**版本库**
实例:
```
$ git add index.php
$ git commit -m "改了第2行"
```
![](./images/6.png)
#### 1.4.4 删除文件
用rm命令删除文件,并直接commit,提交到版本库
例:先创建一个foo.php,供练习删除用
实例如下:
```
$ touch foo.php # 创建foo.php
$ git add foo.php
$ git commit -m "练习删除用"
$ ls
foo.php index.php
# 开始删除
$ git rm foo.php
rm 'foo.php'
$ git commit -m "删除foo.php"
[master e4dc37c] 删除foo.php
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 foo.php
$ ls
index.php
```
![](./images/7.png)
#### 1.5 版本回退
灵活切换到之前的版本记录
例:先创建一个index.php,供练习版本回退用
使用到命令:
git log 查看提交历史记录
git reset --hard 前八位版本记录字符
git reflog 查看历史版本
实例如下:
```
cz@XDL-20170621QCO MINGW64 /c/a (master)
$ mkdir test3
cz@XDL-20170621QCO MINGW64 /c/a (master)
$ cd test3
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ git init
Initialized empty Git repository in C:/a/test3/.git/
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ cd test3
bash: cd: test3: No such file or directory
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ git init
Reinitialized existing Git repository in C:/a/test3/.git/
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ touch index.php
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ git add index.php
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ git commit -m "add index.php"
[master (root-commit) 1bd9fea] add index.php
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 index.php
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ git status
On branch master
nothing to commit, working tree clean
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ git log
commit 1bd9fea788acdf6fcac678e8a3901306426dec2f (HEAD -> master)
Author: caijun <987985143@qq.com>
Date: Mon Aug 13 10:01:12 2018 +0800
add index.php
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ touch admin.php
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ git add admin.php
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ git commit -m "add admin.php"
[master f369884] add admin.php
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 admin.php
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ git log
commit f3698843ae74b9f3bb7c44978b3d08009f0476ec (HEAD -> master)
Author: caijun <987985143@qq.com>
Date: Mon Aug 13 10:02:20 2018 +0800
add admin.php
commit 1bd9fea788acdf6fcac678e8a3901306426dec2f
Author: caijun <987985143@qq.com>
Date: Mon Aug 13 10:01:12 2018 +0800
add index.php
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ git reset --hard 1bd9fea7
HEAD is now at 1bd9fea add index.php
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ ls
index.php
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$
此刻关闭git 命令行,执行git log 只发现只有index.php 提交记录,想回到admin.php 版本
命令:git reflog
实例如下:
cz@XDL-20170621QCO MINGW64 /c/a (master)
$ cd test3
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ ls
index.php
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ git log
commit 1bd9fea788acdf6fcac678e8a3901306426dec2f (HEAD -> master)
Author: caijun <987985143@qq.com>
Date: Mon Aug 13 10:01:12 2018 +0800
add index.php
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ git reflog
1bd9fea (HEAD -> master) HEAD@{0}: reset: moving to 1bd9fea7
f369884 HEAD@{1}: commit: add admin.php
1bd9fea (HEAD -> master) HEAD@{2}: commit (initial): add index.php
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ git reset --hard f369884
HEAD is now at f369884 add admin.php
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ ls
admin.php index.php
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$
```
![](./images/gitb.png)
#### 1.6 git 忽略追踪
在项目开发过程中,有些文件不需要进入到版本库当中,例如uploads
例:先创建一个uploads,供git忽略追踪用
使用到命令:
vim .gitignore 创建忽略追踪文件
实例如下:
```
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ mkdir uploads
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ ls
admin.php index.php uploads/
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ git status
On branch master
nothing to commit, working tree clean
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ cd uploads/
cz@XDL-20170621QCO MINGW64 /c/a/test3/uploads (master)
$ touch 1.jpg
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
uploads/
nothing added to commit but untracked files present (use "git ad d" to track)
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ vim .gitignore
在追加记录文件里 写忽略的追踪文件 /uploads/
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git ad d" to track)
cz@XDL-20170621QCO MINGW64 /c/a/test3 (master)
$
```
## 第2章 分支管理
### 2.1 分支有什么用?
在开发中,遇到这样的情况怎么办?
网站已有支付宝在线支付功能,要添加"微信支付".
修改了3个文件, `wechat.php`,`pay.php`
刚做到一半,突然有个紧急bug: 支付宝支付后不能修改订单状态.
你需要立即马上修改这个bug,需要修改的文件是,`ali.php`,`pay.php`.
问题是:`pay.php`,已经被你修改过,而且尚未完成.
直接在此基础上改,肯定有问题.
把`pay.php`倒回去? 那我之前的工作白费了.
此时你肯定会想: 在做"微信支付"时,能否把仓库复制一份,在此副本上修改,不影响原仓库的内容.修改完毕后,再把副本上的修改合并过去.
好的,这时你已经有了分支的思想.
前面见过的`master`,即是代码的主干分支,
事实上,在实际的开发中,往往不会直接修改和提交到`master`分支上.
而是创建一个`dev`分支,在`dev`分支上,修改测试,没问题了,再把`dev`分支合并到`master`上.
如果有了分支,刚才的难题就好解决了,如下图:
![](./images/17.png)
在做"微信支付"时,我们创建一个`wechat`分支.
把`wechat`分支`commit`,此时,`master`分支内容不会变,因为分支不同.
当遇到紧急bug时,创建一个`AliBug`分支.
修复bug后,把`AliBug`分支合并到`master`分支上.
再次从容切换到`wechat`分支上,接着开发"微信支付"功能,开发完毕后,
把`wechat`分支合并到`master`分支上.
### 2.2 查看分支
查看所有分支 `git branch`
例
```
git branch
* master # 说明只有master分支,且处于master分支.
```
### 2.3 创建分支
创建dev分支 `git branch dev`
```
git branch dev # 创建dev分支
git branch #查看分支
dev
* master # dev分支创建成功,但仍处于master分支
```
### 2.4 切换分支
切换到dev分支 `git checkout dev`
再次查看
```
$ git branch
* dev
master # 已切换到dev分支上
```
### 2.5 合并分支
当我们在dev上开发某功能,并测试通过后,可以把`dev`的内容合并到`master`分支.
例:
当前的readme.txt 内容为"so so",在`dev`分支下,添加一行"from dev"
并提交
```
git add readme.txt
git commit -m "mod in dev"
```
再次切换到`master`,查看readme.txt的内容,仍为'so so'
合并`dev`分支,`git merge dev`, 如下:
```
$ git merge dev
Updating c5364fe..412926b
Fast-forward
readme.txt | 1 +
1 file changed, 1 insertion(+)
```
再次查看readme.txt的内容,已变为"soso from dev";
### 2.6 删除分支
```
git branch -d dev
Deleted branch dev (was 412926b).
```
### 2.7 快速创建和切换分支
`git checkout -b dev` # 创建dev分支并立即切换到dev分支
即起到`git branch dev`和`git checkout dev`的共同作用.
实例:
cz@XDL-20170621QCO MINGW64 /c/a (master)
$ mkdir test4
cz@XDL-20170621QCO MINGW64 /c/a (master)
$ cd test4
cz@XDL-20170621QCO MINGW64 /c/a/test4 (master)
$ git init
Initialized empty Git repository in C:/a/test4/.git/
cz@XDL-20170621QCO MINGW64 /c/a/test4 (master)
$ touch index.php
cz@XDL-20170621QCO MINGW64 /c/a/test4 (master)
$ git add index.php
cz@XDL-20170621QCO MINGW64 /c/a/test4 (master)
$ git commit -m "add index.php"
[master (root-commit) 14d10c9] add index.php
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 index.php
cz@XDL-20170621QCO MINGW64 /c/a/test4 (master)
$ git branch dev
cz@XDL-20170621QCO MINGW64 /c/a/test4 (master)
$ git branch
dev
* master
cz@XDL-20170621QCO MINGW64 /c/a/test4 (master)
$ git checkout dev
Switched to branch 'dev'
cz@XDL-20170621QCO MINGW64 /c/a/test4 (dev)
$ ls
index.php
cz@XDL-20170621QCO MINGW64 /c/a/test4 (dev)
$ touch admin.php
cz@XDL-20170621QCO MINGW64 /c/a/test4 (dev)
$ git add admin.php
cz@XDL-20170621QCO MINGW64 /c/a/test4 (dev)
$ git commit -m "add admin.php"
[dev 26d93e0] add admin.php
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 admin.php
cz@XDL-20170621QCO MINGW64 /c/a/test4 (dev)
$ ls
admin.php index.php
cz@XDL-20170621QCO MINGW64 /c/a/test4 (dev)
$ git checkout master
Switched to branch 'master'
cz@XDL-20170621QCO MINGW64 /c/a/test4 (master)
$ ls
index.php
cz@XDL-20170621QCO MINGW64 /c/a/test4 (master)
$ git merge dev
Updating 14d10c9..26d93e0
Fast-forward
admin.php | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 admin.php
cz@XDL-20170621QCO MINGW64 /c/a/test4 (master)
$ ls
admin.php index.php
cz@XDL-20170621QCO MINGW64 /c/a/test4 (master)
$ git branch -d dev
Deleted branch dev (was 26d93e0).
cz@XDL-20170621QCO MINGW64 /c/a/test4 (master)
$ git branch
* master
cz@XDL-20170621QCO MINGW64 /c/a/test4 (master)
$ ls
admin.php index.php
cz@XDL-20170621QCO MINGW64 /c/a/test4 (master)
## 第3章 git的特点及诞生
### 3.1 分布式版本控制器
何为分布式? 与集中式相比有何特点?
以SVN为例:
![](./images/14.png)
中心的svn服务器中,存储着代码版本的变迁,及日志.
你想查看改动日志,请联网SVN服务器.
你想退回上个版本,请联网SVN服务器.
你想创建新的分支,请联网SVN服务器.
联网不说,万一SVN服务器要是坏了???后果你说呢.
而git是这样的:
![](./images/15.png)
每个开发者的电脑上,都有完整的版本,日志,及分支信息.
但开发者不依赖于服务器,可以查看日志,回退版本,创建分支.
当然,世界各地的开发需要交换最新的版本信息,
因此,git往往也需要服务器.
但是,本质的区别在于:
git服务器是供开发者"交换"代码,服务器数据丢了没关系,分分钟再建一台.
svn的服务器,不仅交换代码,还控制着日志,版本,分支.服务器数据丢了就完了.
### 3.2 发展历史
Linux之父Linus Torvalds在1991年创建了linux开源项目,并把项目放在互联网上,引来世界大量的黑客,大神为项目贡献代码.
问题是,这么多的人同时贡献代码,如何管理代码成了一件头疼的事.
随着linux内核的管理工作越来越吃力,linus选择了一款商业版本控制器-BitKeeper.
BitKeeper是BitMover公司旗下的产品.
公司的老大Larry也希望借机扩大产品的影响力,因此授权Linux社区免费使用BitKeeper.
这件事,在开源圈引起了不小的骚动.
因为,BitKeeper只是free(免费),而非free(自由).
开源教主RMS为这事儿还说过linus.
2002年2月,Linus 开始用它来管理Linux内核代码主线,Linus对BitKeeper的评价是the best tool for the job.
确实,自从Linus使用BitKeeper之后,Linux的开发步伐加快了两倍.
可惜的是,就像黑帮电影中,老大蒸蒸日上的事业,往往坏在一个不懂事的小弟手中.
这帮视free(自由)如信仰的牛人中,一个叫Andrew的,试图破解BitKeeper的协议,且被BitMover公司警告几次后仍不停手.
最终,出事了!
Linus在Andrew和Larry两人间费力调停,但没有成功.
既如此,Linus说:"我的兄弟只是做错事不是做坏事. 我扛!"
于是,10天后,git诞生了!
## 第4章 代码管理
### 4.1 工作区和版本库
如果你想更清晰的学习git,你必须要了解3个重要区域.
+ 工作区, 即开发者的工作目录.
+ 暂存区, 修改已被记录,但尚未录入版本库的区域.
+ 版本库, 存储变化日志及版本信息.
![](./images/16.png)
## 第5章 远程仓库管理之github
### github 定义?
github 是一个面向开源及私有软件项目的托管平台,简单的理解为,他是一个网站,网址为:https://github.com
### github 能做什么?
允许用户在平台上创建版本库,进行多人合作开发
### 5.1 注册github
准备常用邮箱 163或者qq邮箱,执行下一步下一步...
![](./images/github1.png)
创建项目:
![](./images/github2.png)
邮箱激活用户:
![](./images/github3.png)
### 5.2 github 基本使用1
创建远程仓库项目,通过git克隆到本地,添加文件后,推送到远程仓库里
`命令1:git clone 远程仓库地址`
`命令2:git push origin master` 把本地仓库内容推送到远程仓库里
例:
`git clone https://github.com/guocaijun/project3.git`
实例:
cz@XDL-20170621QCO MINGW64 /c/a (master)
$ git clone https://github.com/guocaijun/project3.git
Cloning into 'project3'...
warning: You appear to have cloned an empty repository.
cz@XDL-20170621QCO MINGW64 /c/a (master)
$ cd project3
cz@XDL-20170621QCO MINGW64 /c/a/project3 (master)
$ ls
cz@XDL-20170621QCO MINGW64 /c/a/project3 (master)
$ touch add.php
cz@XDL-20170621QCO MINGW64 /c/a/project3 (master)
$ ls
add.php
cz@XDL-20170621QCO MINGW64 /c/a/project3 (master)
$ git add add.php
cz@XDL-20170621QCO MINGW64 /c/a/project3 (master)
$ git commit -m "add add.php"
[master (root-commit) 9ee6f15] add add.php
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 add.php
cz@XDL-20170621QCO MINGW64 /c/a/project3 (master)
$ git push origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 204 bytes | 204.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/guocaijun/project3.git
* [new branch] master -> master
cz@XDL-20170621QCO MINGW64 /c/a/project3 (master)
$
### 5.3 github 基本使用2
本地创建项目,推送到远程仓库里
`命令1:git remote add origin https://github.com/guocaijun/project4.git` 添加
`命令2:git push -u origin master` 把本地仓库内容推送到远程仓库里
实例:
cz@XDL-20170621QCO MINGW64 /c/a (master)
$ cd project4
cz@XDL-20170621QCO MINGW64 /c/a/project4 (master)
$ git init
Initialized empty Git repository in C:/a/project4/.git/
cz@XDL-20170621QCO MINGW64 /c/a/project4 (master)
$ touch index.php
cz@XDL-20170621QCO MINGW64 /c/a/project4 (master)
$ git add index.php
cz@XDL-20170621QCO MINGW64 /c/a/project4 (master)
$ git commit -m "add index.php"
[master (root-commit) 0d3e908] add index.php
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 index.php
cz@XDL-20170621QCO MINGW64 /c/a/project4 (master)
$ git remote add origin https://github.com/guocaijun/project4.git
cz@XDL-20170621QCO MINGW64 /c/a/project4 (master)
$ git push -u origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 208 bytes | 208.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/guocaijun/project4.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
cz@XDL-20170621QCO MINGW64 /c/a/project4 (master)
$
### 5.4 github 多人开发合作
多人开发合作
申请另外一个开发者
邀请另外一个开发者 邀请链接必须在另外一个用户登录的前提下才有效果
![](./images/github4.png)
`命令1:git push -u origin master` 把本地仓库内容推送到远程仓库里
`命令2:git pull` 把github 远程仓库内容拉回到本地
实例:
cz@XDL-20170621QCO MINGW64 /c/a (master)
$ mkdir project5
cz@XDL-20170621QCO MINGW64 /c/a (master)
$ cd project5
cz@XDL-20170621QCO MINGW64 /c/a/project5 (master)
$ git init
Initialized empty Git repository in C:/a/project5/.git/
cz@XDL-20170621QCO MINGW64 /c/a/project5 (master)
$ touch index.php
cz@XDL-20170621QCO MINGW64 /c/a/project5 (master)
$ git add index.php
cz@XDL-20170621QCO MINGW64 /c/a/project5 (master)
$ git commit -m "add index.php"
[master (root-commit) b4cb34b] add index.php
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 index.php
cz@XDL-20170621QCO MINGW64 /c/a/project5 (master)
$ git remote add origin https://github.com/guocaijun/project5.git
cz@XDL-20170621QCO MINGW64 /c/a/project5 (master)
$ git push -u origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 207 bytes | 207.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/guocaijun/project5.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
cz@XDL-20170621QCO MINGW64 /c/a/project5 (master)
$ cd ..
cz@XDL-20170621QCO MINGW64 /c/a (master)
$ git clone https://github.com/guocaijun/project5.git guocaijun1
Cloning into 'guocaijun1'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
cz@XDL-20170621QCO MINGW64 /c/a (master)
$ cd guocaijun1
cz@XDL-20170621QCO MINGW64 /c/a/guocaijun1 (master)
$ ls
index.php
cz@XDL-20170621QCO MINGW64 /c/a/guocaijun1 (master)
$ touch admin.php
cz@XDL-20170621QCO MINGW64 /c/a/guocaijun1 (master)
$ git add admin.php
cz@XDL-20170621QCO MINGW64 /c/a/guocaijun1 (master)
$ git commit -m "add admin.php"
[master d493474] add admin.php
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 admin.php
cz@XDL-20170621QCO MINGW64 /c/a/guocaijun1 (master)
$ git push -u origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 236 bytes | 236.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To https://github.com/guocaijun/project5.git
b4cb34b..d493474 master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
cz@XDL-20170621QCO MINGW64 /c/a/guocaijun1 (master)
$ cd ..
cz@XDL-20170621QCO MINGW64 /c/a (master)
$ cd project5
cz@XDL-20170621QCO MINGW64 /c/a/project5 (master)
$ git pull
remote: Counting objects: 2, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 2 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), done.
From https://github.com/guocaijun/project5
b4cb34b..d493474 master -> origin/master
Updating b4cb34b..d493474
Fast-forward
admin.php | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 admin.php
cz@XDL-20170621QCO MINGW64 /c/a/project5 (master)
$
### 5.5 github 分支推送
将其他分支代码推送到远程github里
![](./images/分支推送.png)
`命令1:git push -u origin develop` 把本地仓库分支develop内容推送到远程仓库里
`命令2:git checkout -b develop` 创建develop和切换到develop里
实例:
cz@XDL-20170621QCO MINGW64 /c/a (master)
$ cd project5
cz@XDL-20170621QCO MINGW64 /c/a/project5 (master)
$ ls
admin.php index.php
cz@XDL-20170621QCO MINGW64 /c/a/project5 (master)
$ git checkout -b develop
Switched to a new branch 'develop'
cz@XDL-20170621QCO MINGW64 /c/a/project5 (develop)
$ ls
admin.php index.php
cz@XDL-20170621QCO MINGW64 /c/a/project5 (develop)
$ touch user.php
cz@XDL-20170621QCO MINGW64 /c/a/project5 (develop)
$ git add user.php
cz@XDL-20170621QCO MINGW64 /c/a/project5 (develop)
$ git commit -m "add user.php"
[develop e1f0902] add user.php
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 user.php
cz@XDL-20170621QCO MINGW64 /c/a/project5 (develop)
$ git push -u origin develop
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 244 bytes | 244.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To https://github.com/guocaijun/project5.git
* [new branch] develop -> develop
Branch 'develop' set up to track remote branch 'develop' from 'origin'.
cz@XDL-20170621QCO MINGW64 /c/a/project5 (develop)
$
### 5.6 github 分支拉取
将远程仓库分支代码拉取到本地仓库里
`命令1:git pull origin develop` 将远程仓库分支代码拉取到本地仓库里
实例:
cz@XDL-20170621QCO MINGW64 /c/a (master)
$ cd guocaijun1
cz@XDL-20170621QCO MINGW64 /c/a/guocaijun1 (master)
$ ls
admin.php index.php
cz@XDL-20170621QCO MINGW64 /c/a/guocaijun1 (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
cz@XDL-20170621QCO MINGW64 /c/a/guocaijun1 (master)
$ git checkout -b develop
Switched to a new branch 'develop'
cz@XDL-20170621QCO MINGW64 /c/a/guocaijun1 (develop)
$ git pull origin develop
remote: Counting objects: 2, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 2 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), done.
From https://github.com/guocaijun/project5
* branch develop -> FETCH_HEAD
* [new branch] develop -> origin/develop
Updating d493474..e1f0902
Fast-forward
user.php | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 user.php
cz@XDL-20170621QCO MINGW64 /c/a/guocaijun1 (develop)
$ ls
admin.php index.php user.php
cz@XDL-20170621QCO MINGW64 /c/a/guocaijun1 (develop)
$ touch center.php
cz@XDL-20170621QCO MINGW64 /c/a/guocaijun1 (develop)
$ git add center.php
cz@XDL-20170621QCO MINGW64 /c/a/guocaijun1 (develop)
$ git commit -m 'add center.php'
[develop 6cbd4a9] add center.php
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 center.php
cz@XDL-20170621QCO MINGW64 /c/a/guocaijun1 (develop)
$ git push -u origin develop
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 227 bytes | 227.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/guocaijun/project5.git
e1f0902..6cbd4a9 develop -> develop
Branch 'develop' set up to track remote branch 'develop' from 'origin'.
cz@XDL-20170621QCO MINGW64 /c/a/guocaijun1 (develop)
$ cd ..
cz@XDL-20170621QCO MINGW64 /c/a (master)
$ cd project5
cz@XDL-20170621QCO MINGW64 /c/a/project5 (develop)
$ git checkout develop
Already on 'develop'
Your branch is up to date with 'origin/develop'.
cz@XDL-20170621QCO MINGW64 /c/a/project5 (develop)
$ git pull origin develop
remote: Counting objects: 2, done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 2 (delta 1), reused 2 (delta 1), pack-reused 0
Unpacking objects: 100% (2/2), done.
From https://github.com/guocaijun/project5
* branch develop -> FETCH_HEAD
e1f0902..6cbd4a9 develop -> origin/develop
Updating e1f0902..6cbd4a9
Fast-forward
center.php | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 center.php
cz@XDL-20170621QCO MINGW64 /c/a/project5 (develop)
$ ls
admin.php center.php index.php user.php
cz@XDL-20170621QCO MINGW64 /c/a/project5 (develop)
$
### 5.7 github 分支合并 pullrequest 请求
将本地仓库分支代码推送到远程分支里 ,确定无误后,进行合并
`命令1:git push origin test` 将本地仓库分支代码推送到远程仓库里
实例:
cz@XDL-20170621QCO MINGW64 /c/a (master)
$ cd project5
cz@XDL-20170621QCO MINGW64 /c/a/project5 (develop)
$ git branch
* develop
master
cz@XDL-20170621QCO MINGW64 /c/a/project5 (develop)
$ git checkout -b test
Switched to a new branch 'test'
cz@XDL-20170621QCO MINGW64 /c/a/project5 (test)
$ touch shop.php
cz@XDL-20170621QCO MINGW64 /c/a/project5 (test)
$ git add shop.php
cz@XDL-20170621QCO MINGW64 /c/a/project5 (test)
$ git commit -m "add shop.php"
[test 02a821e] add shop.php
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 shop.php
cz@XDL-20170621QCO MINGW64 /c/a/project5 (test)
$ git push origin test
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 225 bytes | 225.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/guocaijun/project5.git
* [new branch] test -> test
cz@XDL-20170621QCO MINGW64 /c/a/project5 (test)
$
![](./images/github分支合并.png)