在个人进行开发软件开发的时候,使用git的分支可以很好的进行管理自己工程进度.使用master和dev两个分支策略可以更好的控制开发版本和稳定版本,master分支用于存放软件的稳定版本,dev分支用于存放开发的版本.master和dev分支的配合也是个人开发常用的策略.下面对分支的创建以及两个分支之间的切换进行详述,更多的操作可参考以前的博客学习Git&学习GitHub
git clone git@github.com:xxxxxxx/xxxxxxx.git
git branch -a
➜ getBlogVisitorNum git:(master) git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
通过该命令可以查看所有的分支信息,以上面的例子为例,该项目在github上只有一个分支,下面就创建一个新的分支dev
git branch <branchname>
git branch
The name of the branch to create or delete. The new branch name must pass all checks defined by git-check-ref-format(1).
Some of these checks may restrict the characters allowed in a branch name.
viagit branch --help
该命令创建一个新的分支
命令的应用如下所示
➜ getBlogVisitorNum git:(master) git branch dev
➜ getBlogVisitorNum git:(master) git branch
dev
* master
上面命令可以看到创建了新的分支dev
,但是在master分支的前面看到一个*
.通过上面命令创建的dev分支还只是一个本地分支,在远程仓库并不存在dev分支.下面的发表dev分支到github上面.
git push origin dev:dev
➜ getBlogVisitorNum git:(master) git push origin dev:dev
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:idealcitier/getBlogVisitorNum.git
* [new branch] dev -> dev
➜ getBlogVisitorNum git:(master) git branch
dev
* master
通过上述的命令在把本地的dev分支发布到了github上面,当前的分支仍是master分支.
git checkout dev
➜ getBlogVisitorNum git:(master) git checkout dev
Switched to branch 'dev'
➜ getBlogVisitorNum git:(dev) git branch
* dev
master
通过上述的命令,切换到dev分支上,可以看到dev分支的抢房有一个*
日常在dev分支进行代码的编写,在测试代码没有什么问题之后可以把dev分支与master进行合并.
git checkout master # 在合并之前切换到master分支
git merge dev # 把dev分支的代码合并到master分支上
git push # 提交刚刚的操作到远端github仓库
git checkout dev # 切回dev分支进行下一步的开发
下面依旧举一个例子
e.g.
➜ getBlogVisitorNum git:(dev) git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
➜ getBlogVisitorNum git:(master) git branch
dev
* master
➜ getBlogVisitorNum git:(master) git merge dev
Already up-to-date.
➜ getBlogVisitorNum git:(master) git branch
dev
* master
➜ getBlogVisitorNum git:(master) git push
warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.
Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
Everything up-to-date
➜ getBlogVisitorNum git:(master) git branch
dev
* master
➜ getBlogVisitorNum git:(master) git checkout dev
Switched to branch 'dev'
下面的命令请谨慎操作
git push orgin :dev # 删除远程的dev分支
git checkout master # 切换到master分支
git branch -d dev # 删除本地的dev分支
上述的命令没有试过,在网上搜的,我还没有想删除dev分支,逃~~~