Git实践指南——合并分支至master

Git作为版本控制工具,相信开发、测试或运维人员都不会陌生,尤其是在团队合作过程中,Git+Github/Gitlab/Bitbucket等组合在分支管理、代码维护等方面起着非常重要的作用。因此作为菜鸟级别的萌新,有必要学习一些常规的、基本的操作以便快速上手。这篇文章与大家分享一下在分支管理中使用作为频繁的场景之一即如何将本地非master分支的代码合并至master。

 总所周知,在项目开发过程中,往往会同是运维多套版本,这些版本通常会包括主分支Master、开发分支Develop、临时性分支、预发布分支、修补bug分支等。所有提供给用户使用的正式版本,都在主分支上发布,因此其他分支的改动,最终基本上都会合并到主分支,这里以test分支为例简单介绍一下如何将本地非master分支的代码合并至master。

首先通过git branch查看当前所处分支,可以看到我们当前处于test_bm分支

$ git branch
  master
* test_bm

然后我们可以如下命令先将已经修改过的内容先提交到远程代码仓库test_bm分支下

git  add .
git  commit -m 'test_bm'
git push -u origin test_bm

如果我们要把修改的内容在合并到master分支下,那将需要如何操作呢?

首先使用git checkout命令从当前分支切换到master分支下

git  checkout master

注意如果是团队合作,多人提交代码的话,为了避免冲突,建议首先使用git pull命令远程master上的代码pull下来,然后使用git merge命令把test_bm分支的代码合并到master上

git pull origin master
git  merge dev

我们通过git status命令可以查看提交状态,如下内容表示有8个commit 需要推送到远程master上

$ git status
On branch master
Your branch is ahead of 'origin/master' by 8 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

最后执行push命令即可实现使用git将分支内容合并到主分支master上。

最后的彩蛋:切忌抱着金山要饭吃,使用git help基本上可以满足我们日常工作中的需求

$ git help
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone     Clone a repository into a new directory
   init      Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add       Add file contents to the index
   mv        Move or rename a file, a directory, or a symlink
   restore   Restore working tree files
   rm        Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect    Use binary search to find the commit that introduced a bug
   diff      Show changes between commits, commit and working tree, etc
   grep      Print lines matching a pattern
   log       Show commit logs
   show      Show various types of objects
   status    Show the working tree status

grow, mark and tweak your common history
   branch    List, create, or delete branches
   commit    Record changes to the repository
   merge     Join two or more development histories together
   rebase    Reapply commits on top of another base tip
   reset     Reset current HEAD to the specified state
   switch    Switch branches
   tag       Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch     Download objects and refs from another repository
   pull      Fetch from and integrate with another repository or a local branch
   push      Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system.

参考:Git分支管理策略:http://www.ruanyifeng.com/blog/2012/07/git.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值