git拉取远程分支并创建本地分支和Git中从远程的分支获取最新的版本到本地

声明:转载地址:https://blog.csdn.net/jtracydy/article/details/53174175


git拉取远程分支并创建本地分支

一、查看远程分支

使用如下Git命令查看所有远程分支:

git branch -r

查看包括本地和远程的所有分支:

               git  branch -a

二、拉取远程分支并创建本地分支

方法一

使用如下命令:

git checkout -b 本地分支名x origin/远程分支名x
[html]  view plain  copy
  1. $ git checkout -b hhhh master  

以master为模板,创建hhhh分支,并且切换到hhhh分支

使用该方式会在本地新建分支x,并自动切换到该本地分支x。

可能会报错:error: The following untracked working tree files would be overwritten by checkout

解决方法:进入本地版本仓库目录下,git clean -d -fx

参考:https://blog.csdn.net/tongxinxiao/article/details/43988773

方式二

使用如下命令:

git fetch origin 远程分支名x:本地分支名x

使用该方式会在本地新建分支x,但是不会自动切换到该本地分支x,需要手动checkout。

转自:http://blog.csdn.net/tterminator/article/details/52225720

Git中从远程的分支获取最新的版本到本地

Git中从远程的分支获取最新的版本到本地有这样2个命令:

1. git fetch:相当于是从远程获取最新版本到本地,不会自动merge
    
git fetch origin master
git log -p master..origin/master
git merge origin/master


    以上命令的含义:
   首先从远程的origin的master主分支下载最新的版本到origin/master分支上
   然后比较本地的master分支和origin/master分支的差别
   最后进行合并

   上述过程其实可以用以下更清晰的方式来进行:
   
git fetch origin master:tmp
git diff tmp 
git merge tmp

    
    从远程获取最新的版本到本地的test分支上
   之后再进行比较合并

2. git pull:相当于是从远程获取最新版本并merge到本地
   
git pull origin master


    上述命令其实相当于git fetch 和 git merge
    在实际使用中,git fetch更安全一些
   因为在merge前,我们可以查看更新情况,然后再决定是否合并


猜你喜欢

转载自blog.csdn.net/colorful_lights/article/details/80177706