git-flow工具之命令行操作教程(一)
1、git flow介绍
git的优点就不多说了,诚然如果单纯使用git来做项目的开发的话必然有诸多限制,比如git分支多了之后如何管理?世间之事大抵都应该是复杂的事简单做,有迹可寻才可以掌控一切。所以git也不例外,需要有一套规范的流程来辅助git进行大项目的开发以及迭代维护。因此Vincent Driessen在2010年提出了一套完整的git开发流程,使用这套流程来管理项目不论是小的周期短的还是大的迭代很多周期很长的项目都不在话下,因为有了很清晰的规范,所以git flow很快得到推广。
该套规范流程图如下:
关于该流程的介绍可以参考原文:http://nvie.com/posts/a-successful-git-branching-model/。 中文可以参考:http://deshui.wang/%E6%95%8F%E6%8D%B7/2015/10/27/sourcecode-management。
因为有了这流程便有人会开发对应的工具,所以这次我们要讲的便是git flow工具,在ubuntu下安装git flow工具即可(apt-get install git-flow
)。
2、git flow工具使用教程
我们会通过生动的图文来帮助大家熟悉这款工具,如果中间有什么遗漏的大家可以留言补充,多谢。
2.1、下载代码到本地
测试代码以https://github.com/linxiaowu66/testForGitFlow.git为例子:
首先git clone 代码到本地:
root@linguang-VirtualBox:/home/nodejsTest# git clone https://github.com/linxiaowu66/testForGitFlow.git
Cloning into 'testForGitFlow'...
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
Checking connectivity... done.
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# git branch
* master
本着我们不在master主干上修改代码的原则下,(其实还有一个原则那就是但凡是在master上提交的代码都应该有对应的tag)。开始使用git flow帮助规范整个代码管理:
git flow init
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# git flow init
Which branch should be used for bringing forth production releases?
- master
Branch name for production releases: [master]
Branch name for "next release" development: [develop]
How to name your supporting branch prefixes?
Feature branches? [feature/]
Bugfix branches? [bugfix/]
Release branches? [release/]
Hotfix branches? [hotfix/]
Support branches? [support/]
Version tag prefix? []
Hooks and filters directory? [/home/nodejsTest/testForGitFlow/.git/hooks]
全部选择默认即可。 接着可以查看到现在本地有了两个分支。并且git的config文件多了一些信息
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# git branch
* develop
master
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = https://github.com/linxiaowu66/testForGitFlow.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[gitflow "branch"]
master = master
develop = develop
[gitflow "prefix"]
feature = feature/
bugfix = bugfix/
release = release/
hotfix = hotfix/
support = support/
versiontag =
[gitflow "path"]
hooks = /home/nodejsTest/testForGitFlow/.git/hooks
如此操作之后的分支图:
接着在正式开发之前我们还应该将develop分支提交到remote上去,保证远端也有一致的develop分支。
git push origin develop
如此操作之后的分支图:
对应于gitHub上的:
这时候的本地远程的代码都是一模一样的。
2.2、feature开发
假设我们现在有一个需求,那么我们就需要进行该feature的开发:
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# git flow feature start feature_test1
Switched to a new branch 'feature/feature_test1'
Summary of actions:
- A new branch 'feature/feature_test1' was created, based on 'develop'
- You are now on branch 'feature/feature_test1'
Now, start committing on your feature. When done, use:
git flow feature finish feature_test1
使用git flow命令的每一次操作都会打印出该命令执行了哪些操作,所以你可以对照着这些命令进一步熟悉git flow的工作原理。
此时分支图如下:
2.2.1、修改feature分支的代码
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# echo "## test1" >> README.md
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# git status
On branch feature/feature_test1
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# echo "continue test1" >> README.md
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# git commit -am 'feature test1 develop finish'
[feature/feature_test1 0667ee1] feature test1 develop finish
1 file changed, 1 insertion(+)
经过这么修改,分支图变成:
2.2.2、上传分支到远程以便共享(可选)
如果此时你的feature需要别的同事参与,那么需要将该feature push到remote上去,所以:
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# git flow feature publish feature_test1
Username for 'https://github.com': linxiaowu66
Password for 'https://linxiaowu66@github.com':
Counting objects: 6, done.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 592 bytes | 0 bytes/s, done.
Total 6 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/linxiaowu66/testForGitFlow.git
* [new branch] feature/feature_test1 -> feature/feature_test1
Branch feature/feature_test1 set up to track remote branch feature/feature_test1 from origin.
Already on 'feature/feature_test1'
Your branch is up-to-date with 'origin/feature/feature_test1'.
Summary of actions:
- The remote branch 'feature/feature_test1' was created or updated
- The local branch 'feature/feature_test1' was configured to track the remote branch
- You are now on branch 'feature/feature_test1'
查看github上可以看到对应的分支。
远程和本地指向同一个代码位置。
别的同事想要开发的话就可以直接git flow feature pull origin feature_test1
2.2.3、feature开发完毕
接着假设协作开发没有冲突的前提下(当然这种概率很小,所以一旦有了共享就需要解决冲突之后再提交代码),该feature开发完毕,那么:
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# git pull origin develop
From https://github.com/linxiaowu66/testForGitFlow
* branch develop -> FETCH_HEAD
Already up-to-date.
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# git flow feature finish feature_test1
Switched to branch 'develop'
Merge made by the 'recursive' strategy.
README.md | 3 +++
1 file changed, 3 insertions(+)
Username for 'https://github.com':
Password for 'https://github.com':
remote: Anonymous access to linxiaowu66/testForGitFlow.git denied.
fatal: Authentication failed for 'https://github.com/linxiaowu66/testForGitFlow.git/'
Fatal: Could not delete the remote feature/feature_test1 in origin.
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# git flow feature finish feature_test1
Branches 'develop' and 'origin/develop' have diverged.
And local branch 'develop' is ahead of 'origin/develop'.
Already on 'develop'
Already up-to-date.
Username for 'https://github.com': linxiaowu66
Password for 'https://linxiaowu66@github.com':
To https://github.com/linxiaowu66/testForGitFlow.git
- [deleted] feature/feature_test1
Deleted branch feature/feature_test1 (was 0667ee1).
Summary of actions:
- The feature branch 'feature/feature_test1' was merged into 'develop'
- Feature branch 'feature/feature_test1' has been locally deleted; it has been remotely deleted from 'origin'
- You are now on branch 'develop'
此时变化如下:
远端的feature分支也被删掉了: 注意:
- 记得执行
feature finish
命令之前保证develop分支是最新的,所以必须git pull origin develop。 - 记得删除远程对应的feature分支,因为gitflow不会去帮你删除。删除远程分支可以使用这个命令:
git push origin :feature/feature_test1
,该命令可以告诉Git该远程分支不会再有任何push操作了,所以git会为你自动删除该远程分支
2.3、release分支开发
开发完所有的feature之后可能需要发布一个正式版本,所以接下去需要:
2.3.1、开始Release开发
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# git pull origin develop
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# git flow release start release_v0.0.1
Branches 'develop' and 'origin/develop' have diverged.
And local branch 'develop' is ahead of 'origin/develop'.
Switched to a new branch 'release/release_v0.0.1'
Summary of actions:
- A new branch 'release/release_v0.0.1' was created, based on 'develop'
- You are now on branch 'release/release_v0.0.1'
Follow-up actions:
- Bump the version number now!
- Start committing last-minute fixes in preparing your release
- When done, run:
git flow release finish 'release_v0.0.1'
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# git branch
develop
master
* release/release_v0.0.1
所以有:
同样的如果该Release分支需要同事一块实现,或者bug需要别的同事解决,那么该release也是需要publish到远程:
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# git flow release publish release_v0.0.1
Username for 'https://github.com': linxiaowu66
Password for 'https://linxiaowu66@github.com':
Counting objects: 7, done.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 742 bytes | 0 bytes/s, done.
Total 7 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To https://github.com/linxiaowu66/testForGitFlow.git
* [new branch] release/release_v0.0.1 -> release/release_v0.0.1
Branch release/release_v0.0.1 set up to track remote branch release/release_v0.0.1 from origin.
Already on 'release/release_v0.0.1'
Your branch is up-to-date with 'origin/release/release_v0.0.1'.
Summary of actions:
- The remote branch 'release/release_v0.0.1' was created or updated
- The local branch 'release/release_v0.0.1' was configured to track the remote branch
- You are now on branch 'release/release_v0.0.1'
即:
和:
现在你可以在Release这个分支上修改一些bug,比如:
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# echo "## release_v0.0.1" >> README.md
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# git commit -am 'fix a bug in release_v0.0.1'
[release/release_v0.0.1 4503b48] fix a bug in release_v0.0.1
1 file changed, 1 insertion(+)
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# echo "## release_v0.0.1 finish" >> README.md
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow# git commit -am 'fix a bug in release_v0.0.1 and release it'
[release/release_v0.0.1 5440b52] fix a bug in release_v0.0.1 and release it
1 file changed, 1 insertion(+)
root@linguang-VirtualBox:/home/nodejsTest/testForGitFlow#
在本地体现如图:
公众号关注一波~
网站源码:linxiaowu66 · 豆米的博客
Follow:linxiaowu66 · Github
关于评论和留言
如果对本文 git-flow工具之命令行操作教程(一) 的内容有疑问,请在下面的评论系统中留言,谢谢。