转自:http://www.cnblogs.com/jackluo/p/3499731.html

开发者user1 负责用getopt 进行命令解析的功能,因为这个功能用到getopt 函数,于是将这个分支命名为user1/getopt.
(1)确保是在开发者user1的工作区中
cd /home/jackluo/workspace/user1/workspace/hello-world
(2)开发者user1 基于当前HEAD创建分支user1/getopt.
git branch user1/getopt
(3)使用 git branch创建分支,并不会自动切换.查看当前分支可以看到仍然工作在master分支(用星号"*"标识)中.
[root@localhost hello-world]# git branch
* master
  user1/getopt
(4)执行git checkout 命令切换到新分支上
[root@localhost hello-world]# git checkout user1/getopt
已经位于 'user1/getopt'
(5)再次查看分支列表,当前工作分支的标记符(星号)已经落在user1/getopt分支上.
[root@localhost hello-world]# git branch
  master
* user1/getopt
分支实际上是创建在目录.git/refs/heads 下的引用 ,版本库初始时创建的master分支就是在该目录下.

查看一下.git/refs/heads 目录下的引用 .可以在该目录 下看到master文件,和一个user1目录.而在user1目录下是文件getopt。

[root@localhost hello-world]# ls -F .git/refs/heads/
master  user1/

[root@localhost hello-world]# ls -F .git/refs/heads/user1/
getopt

引用文件 .git/refs/heads/user1/getopt记录的是一个提交ID.

[root@localhost hello-world]# cat .git/refs/heads/user1/getopt 
d901dd8170f67fec607828905d5fbd91e3272400

因为分支user1/getopt是基于头指针HEAD创建的,因此当前该分支和master分支的指向是一致的.

[root@localhost hello-world]# cat .git/refs/heads/master 
d901dd8170f67fec607828905d5fbd91e3272400

===============================

创建分支user2/i18n

创建分支:执行git branch <branchname>命令创建新分支

切换分支:执行git checkout <branchname>命令切换到新分支

git checkout -b <new_branch> [<start_point>]

检出命令git checkout通过参数-b <new_branch> 实现了创建分支和切换分支两个动作的合二为一,下面是

开发者user2就使用git checkout 命令来创建分支,

(1)进入到开发者user2的工作目录 ,并和上游同步一次

[root@localhost workspace]# cd user2/workspace/hello-world/
[root@localhost hello-world]# git pull

(2).执行git checkout -b 命令,创建并切换到新分支user2/i18n上.

[root@localhost hello-world]# git checkout -b user2/i18n
切换到一个新分支 'user2/i18n'

(3)查看本地分支列表,会看到已经创建 并切换到user2/i18n分支上了.

[root@localhost hello-world]# git branch
master
* user2/i18n

开发者user1完成功能开发

开发者user1开始在user1/getopt 分支中工作,重构hello-world 中的命令行参 数解析的代码,重构时采用getopt_long 函数.

也可以试着更改,不过在hello-world中已保存了一份改好的代码,可以直接检出.

(1)确保是在user1的工作区中

 cd ../../../user1/workspace/hello-world/

(2)执行下面的命令,用里程B jx/v2.0标记的内容(已实现用getopt 进行命令行解析的功能)替换暂存区和工作区.

下面的git checkout 命令的最后是一个点"."因此检出只更改了暂存区和工作区,

而没有修改头指针.

git checkout jx/v2.0 -- .

(3)查看状态,会看到分支仍保持为user1/getopt,但文件src/main.c 被修改了.

[root@localhost hello-world]# git status
# 位于分支 user1/getopt
# 要提交的变更:
# (使用 "git reset HEAD <file>..." 撤出暂存区)
#
# 修改: src/Makefile
# 修改: src/main.c
#

(4)比较暂存区和HEAD的文件差异,可以看到为实现用getopt进行命令行解析功能而对代码 的改动

[root@localhost hello-world]# git diff --cached

(5)开发者user1提交代码,完成任务 .

[root@localhost hello-world]# git commit -m "Refactor: use getopt_long for arguments parsing."

(6).提交完成之后,可以看到这时 user1/getopt分支和master分支的指向不同了。

[root@localhost hello-world]# git rev-parse user1/getopt master
733dcf67eba976a61d0dc6396c9d23cb23568591
d901dd8170f67fec607828905d5fbd91e3272400
(7)编译运行hello-world.
注意输出中的版本号显示.
[root@localhost src]# make clean
rm -f hello main.o version.h
[root@localhost src]# make
version.h.in => version.h
cc -c -o main.o main.c
cc -o hello main.o
[root@localhost src]# ./hello
Hello world.
(version: v1.0-1-g733dcf6)

将user1/getopt分支合并到主线

(1),为将分支合并到主线,首先user1将工作区切换到主线,master分支.

[root@localhost src]# git checkout master
切换到分支 'master'

(2)然后执行git merge命令以合并user1/getopt 分支.

[root@localhost src]# git merge user1/getopt
更新 d901dd8..733dcf6

(3)本次合并非常顺利,实际上合并后master分支和user1/getopt指向同一个提交 ,这是因为合并前的master的提交就是user/getopt分支的父提交,所以此次合并相当于将分支master重置到user1/getopt分支

[root@localhost src]# git rev-parse user1/getopt master
733dcf67eba976a61d0dc6396c9d23cb23568591
733dcf67eba976a61d0dc6396c9d23cb23568591

(4)查看状态信息可以看到本地和远程分支的跟踪关系 .

[root@localhost src]# git status
# 位于分支 master
# 您的分支领先 'origin/master' 共 1 个提交。
# (使用 "git push" 来发布您的本地提交)
#
无文件要提交,干净的工作区

(5)上面的状态输出中显示本地master分支比远程共享版本库的master分支领先.可以运行git cherry命令查看喜好些提交领先(未被推送到上游跟踪分支中).

[root@localhost src]# git cherry
+ 733dcf67eba976a61d0dc6396c9d23cb23568591

(6)执行推送操作,完成本地分支向远程分支的同步

[root@localhost src]# git push
warning: push.default 未设置,它的默认值将会在 Git 2.0 由 'matching'
修改为 'simple'。若要不再显示本信息并在其默认值改变后维持当前使用习惯,
进行如下设置: git config --global push.default matching 若要不再显示本信息并从现在开始采用新的使用习惯,设置: git config --global push.default simple 参见 'git help config' 并查找 'push.default' 以获取更多信息。
('simple' 模式由 Git 1.7.11 版本引入。如果您有时要使用老版本的 Git,
为保持兼容,请用 'current' 代替 'simple' 模式) Counting objects: 21, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 588 bytes | 0 bytes/s, done.
Total 5 (delta 3), reused 1 (delta 1)
To /home/jackluo/workspace/repos/hello-world.git
d901dd8..733dcf6 master -> master

(7)删除 user1/getopt分支.

隐然特性分支user1/getopt 已经合并到主线上了,那么分支完成了历史命,可以放心地将其删除.

[root@localhost src]# git branch -d user1/getopt
已删除分支 user1/getopt(曾为 733dcf6)。

git 创建branch分支【转】的更多相关文章

  1. git 创建branch分支

    开发者user1 负责用getopt 进行命令解析的功能,因为这个功能用到getopt 函数,于是将这个分支命名为user1/getopt.(1)确保是在开发者user1的工作区中cd /home/j ...

  2. git 创建远程分支和删除 master 分支

    . . . . . 最近需要将不同的客户的代码分开管理,所以需要为这些代码分别创建分支. 目前版本库中分支结构如下: [yuhuashi@local:Project]$ git branch -a* ...

  3. git创建本地分支以及推送本地分之至远程分支

    Git分支策略 实际开发中,应当按照以下几个基本原则进行管理: 首先,master分支应该是非常稳定的,也就是仅用来发布新版本,平时不能再上边干活. 那在哪干活呢?干活都在dev分支上,也就是说,de ...

  4. git 创建本地分支与远程分支

    早上抽空整理了下git常用操作,偶尔看看加深下印象吧: 如果github上已经有master分支 和dev分支 在本地 git checkout -b dev 新建并切换到本地dev分支 git pu ...

  5. git创建新分支

    1.创建本地分支 git branch 分支名,例如:git branch 2.0.1.20120806 注:2.0.1.20120806是分支名称,可以随便定义.   2.切换本地分支 git ch ...

  6. Git创建本地分支并关联远程分支

    创建本地分支git branch 分支名 例如:git branch dev,这条命令是基于当前分支创建的本地分支,假设当前分支是master(远程分支),则是基于master分支创建的本地分支dev ...

  7. git创建新分支推送到远程

    1.创建本地分支 git branch 分支名,例如:git branch 2.0.1.20120806 注:2.0.1.20120806是分支名称,可以随便定义.   2.切换本地分支 git ch ...

  8. Git创建本地分支并关联远程分支(二)

    创建本地分支git branch 分支名 例如:git branch dev,这条命令是基于当前分支创建的本地分支,假设当前分支是master(远程分支),则是基于master分支创建的本地分支dev ...

  9. Git创建本地分支并关联远程分支(一)

    默认,git项目只有一个分支,就是master,我们当然可以在本地创建多个分支,并推送到远程git管理平台上,或者将远程git管理平台上的其他分支拉取到自己电脑上. 一.查看本地已有的分支 进入到项目 ...

随机推荐

  1. SL410K 在Ubuntu禁用触摸板

    由于之前把系统自带的恢复去了,然后TouchPad一直不能禁用,而后我的410k就只装上ubuntu,想不到在ubuntu上,禁用/启用 触摸板这么方便. http://askubuntu.com/q ...

  2. POJ 2960 博弈论

    题目链接: http://poj.org/problem?id=2960 S-Nim Time Limit: 2000MS Memory Limit: 65536K 问题描述 Arthur and h ...

  3. idea maven添加jar包

    在“项目结构“里设置 选择libaray,添加jar包

  4. 2014ACM/ICPC亚洲区广州站 北大命题

    http://acm.hdu.edu.cn/showproblem.php?pid=5131 现场赛第一个题,水题.题意:给水浒英雄排序,按照杀人数大到小,相同按照名字字典序小到大.输出.然后对每个查 ...

  5. 来自平时工作中的css知识的积累---持续补充中

    ① 现代浏览器中,<img>元素默认情况下底部会有空白,那么这个空白到底是从哪里来的? 解惑: method-one:猛戳 来自知乎的解答 method-two: 延伸阅读 what is ...

  6. [百度空间] [转]关于Direct3D多窗口编程的一篇翻译

    Introduction In DirectX 8, support for rendering to multiple windows is provided through the creatio ...

  7. jQuery中的height()、innerheight()、outerheight()的区别总结

    在前端jQuery代码中突然看到outerheight(),第一感觉就是,这是什么鬼?然后仔细查阅了一下,居然发现还有这么多相似的东西. 在jQuery中,获取元素高度的函数有3个,它们分别是heig ...

  8. 分布式数据存储-MySQL主从复制

    前言 一.主从复制过程 MySQL的主从复制能力是通过三个线程来实现的,两个在Slave端的I/O和SQL两个线程,还有一个在Master端I/O线程: Binlog dump thread:Mast ...

  9. EditText 属性

    android:layout_gravity="center_vertical" 设置控件显示的位置:默认top,这里居中显示,还有bottom android:hint=&quo ...

  10. SQL技术内幕-5 比较特殊 insert into 数据的写法

    ---比较特殊,第一次看到这种写法,记录下来 create table Student --学生成绩表 ( id int, --主键 Grade int, --班级 Score int --分数 ) ...