首页 > 代码库 > git合并远端分支到本地分支的两种方式

git合并远端分支到本地分支的两种方式

  作者:zhanhailiang 日期:2014-11-21

在使用版本工具提交修改之前,都需要通过update先将本地代码更新到最新版本。SVN通过svn update就可以实现,那么git如何实现呢?

1. 首先通过从远程的origin的master主分支下载最新的版本到本地origin/master分支上,然后比较区别,最后合并到当前分支;

[root@~/wade/nodejs/express-routing]# git fetch git@github.com:billfeller/express-routing.git master
From github.com:billfeller/express-routing
 * branch            master     -> FETCH_HEAD
[root@~/wade/nodejs/express-routing]# git log 
commit c7f2ae18cd3648d32fa3dcd50060fa7f13a093b1
Author: billfeller <billfeller@gmail.com>
Date:   Fri Nov 21 01:41:40 2014 +0800
 
    express-routing test
 
commit dd5923229abe4e1da706ce53ad4b41154cb89ea7
Author: billfeller <billfeller@gmail.com>
Date:   Fri Nov 21 01:07:03 2014 +0800
 
    Merge branch ‘master‘, remote branch ‘origin‘
 
commit d1aee713ca98e5cdead1978dcfcf7a55974a2851
Author: billfeller <billfeller@users.noreply.github.com>
Date:   Fri Nov 21 01:05:12 2014 +0800
 
    Create README.md
 
commit 5aa66ffc06b95aeb8ef42627fe4b97c95cb660b9
Author: billfeller <billfeller@users.noreply.github.com>
Date:   Fri Nov 21 01:03:05 2014 +0800
 
    Initial commit
[root@~/wade/nodejs/express-routing]# git merge origin/master
Updating c7f2ae1..8d3058b
Fast-forward
 README.md |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

2. 通过git pull一步到位,相当于是从远程获取最新版本并merge到本地,即第一种方法的快捷命令。

[root@~/wade/nodejs/express-routing]# git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From github.com:billfeller/express-routing
   8d3058b..82a02eb  master     -> origin/master
Updating 8d3058b..82a02eb
Fast-forward
 README.md |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

在实际开发中,还是推荐使用第一种方式,因为git fetch更安全一些,可以通过git fetch后查看更新情况,再决定是否进行合并。

git合并远端分支到本地分支的两种方式