我遇到的情况:新创建了一个项目,用优盘拷贝的源代码,后来其他人的代码进行git push,我需要pull 下来。

1,需要本地仓库,并git commit

2,此时git pull会报错:fatal: refusing to merge unrelated histories.

解决办法:git pull origin master --allow-unrelated-histories

注意:master是我需要pull的分支,根据自己需求

错误的解释:

* "git merge" used to allow merging two branches that have no common base by default, which led to a brand new history of an existing project created and then get pulled by an unsuspecting maintainer, which allowed an unnecessary parallel history merged into the existing project.  The command has been taught not to allow this by default, with an escape hatch "--allow-unrelated-histories" option to be used in a rare event that merges histories of two projects that started their lives independently.
* "git pull" has been taught to pass the "--allow-unrelated-histories" option to underlying "git merge".

避免这种错误的方法:不要用优盘或其他的方式拷贝源代码,需要一个人创建好项目后push到github或码云,然后通过fetch     或clone的方式

下面推荐一个写的挺好的merge branch  ,原文链接:https://www.cnblogs.com/forwill/p/6524185.html

Git merge 不同的branch

Git的优势是可以创建不同的branch,然后在每个branch上开发。那么问题是:如果不同的branch之间需要做同步,比如sourceBranch上做的修改也需要同步到targetBranch,改怎么做?

1. 如果一个branch是有远程Git server管理的,另一个branch是自己本地的

cd <your workspace>

git branch  //假定现在所在的branch是targetBranch,并最好保证没有未提交的修改,并且已经更新到最新

git checkout -b sourceBranch  //创建一个本地的sourceBranch并切换到sourceBranch

git commit  //把sourceBranch上做的修改先提交

git checkout targetBranch  //切换回targetBranch

git merge --no-ff sourceBranch  //把sourceBranch的修改merge到targetBranch。注意:建议merge的时候总是用 --no-ff 选项

    git status  //保证现在workspace是干净的

git push   //push到远程,如果远程有新的修改,先做一下git pull

2. 如果两个branch都是远程管理的,想把branchB的内容同步到branchA上

cd <your workspace>

git branch  //假定现在所在的branch是branchA,并最好保证没有未提交的修改,并且已经更新到最新

git checkout sourceBranch  //确保同一个workspace能在不同的branch直接切换,即保证 .git/config里 [remote "origin"] 的内容是 fetch = +refs/heads/*:refs/remotes/origin/*

git merge targetBranch

解决conflicts如果merge的结果里有显示conflicts

git commit  //解决冲突后先commit到sourceBranch

git checkout targetBranch  //切换到targetBranch

git merge --no-ff sourceBranch  //建议merge的时候总是用 --no-ff 选项

git push origin targetBranch   //把sourceBranch的修改merge到targetBranch之后,push到远程的targetBranch

git pull (merge远程分支到本地)相关推荐

  1. ​git拉取远程分支到本地 ​

    #查看远程分支 git branch -r#查看本地分支 git branch#拉取远程分支 git checkout -b 本地分支 origin/远程分支#拉取远程分支 git pull orig ...

  2. Git拉取远程分支到本地,修改并同步

    Git拉取远程分支到本地 本地新建一个空白文件夹folder: 进入folder目录,打开git bash,用命令行初始化git仓库: $ git init 与远程仓库建立连接: // `http:/ ...

  3. Git拉取远程分支到本地及推送代码到远程分支

    一.拉取远程某一分支到本地 在本地新建一个空文件夹 在空文件夹下右键 点击Git Bash Here打开Git 初始化: git init 建立连接: git remote add origin 远程 ...

  4. git 拉取远程分支到本地(两种方法)

    By: Ailson Jack Date: 2019.10.13 个人博客:http://www.only2fire.com/ 本文在我博客的地址是:http://www.only2fire.com/ ...

  5. git拉取远程分支到本地分支

    查看命令 查看远程所有分支 git branch -r # 查看远程所有分支 PS C:\Users\Lenovo\Desktop\tt\dc-biztable> git branch -ror ...

  6. git 拉取远程分支到本地及本地切换分支

    拉取远程分支到本地及本地切换分支 涉及的操作内容1.远程代码拉取到本地 - 2.本地合并其它分支代码 - 3.本地代码提交到远程指定仓库 - 4.本地切换分支 1.远程代码拉取到本地 首先确定要切换分 ...

  7. 使用Git命令-查看远程分支、本地分支、创建分支、删除分支的方法

    Git-查看远程分支.本地分支.创建分支 查看本地分支 查看远程分支 查看所有分支 切换远程分支 合并分支 撤消上一次commit的内容 git commit -m 注释换行 pull下所有分支 gi ...

  8. git拉取远程分支到本地

    一.查看远程分支 使用如下git命令查看所有远程分支: git branch -r 1 二.拉取远程分支并创建本地分支 方法一 使用如下命令: git checkout -b 本地分支名x origi ...

  9. git拉取远程分支到本地(VScode)

    一.怎么样查看本地分支 1.如果你使用的是vscode可以直接查看 2.使用git命令查看本地分支 git branch 二.查看远程分支 1.使用如下git命令查看所有远程分支: git branc ...

  10. 【转】git拉取远程分支到本地

    一.查看远程分支 使用如下git命令查看所有远程分支: git branch -r 二.拉取远程分支并创建本地分支 方法一 使用如下命令: git checkout -b 本地分支名x origin/ ...

最新文章

  1. android 屏保_Mac视频屏保随心换!
  2. 笔记-项目干系人管理
  3. mds聚类matlab,MDS图示聚类结果
  4. JavaScript中错误正确处理方式,你用对了吗?
  5. 「递归」第3集 | 向善的信念,让技术自带光芒
  6. PAT1047 编程团体赛 (20 分)
  7. duilib环境配置以及简单入门介绍
  8. urllib2:URLError与HTTPError
  9. 安全客2017季刊-第3期
  10. Web Components 系列(五)—— 详解 Slots
  11. 学会python能做什么-学会python可以做什么
  12. Silverlight 2 Beta 1学习资源
  13. linux查看磁盘内存cpu
  14. freenas搭建nas及san网络存储详解
  15. Win10安装Eclipse教程
  16. 神经网络控制系统的特点,神经网络控制的优点
  17. 如何使用DDMS Heap查看Android应用内存情况
  18. 什么是集体户口,优势、劣势
  19. 数学建模学习(90):改进的灰狼优化算法(I-GWO)对多元函数寻优
  20. 无法连接到宽带连接被远程计算机终止,无法连接到宽带连接?

热门文章

  1. b2c项目基础架构分析(一)b2c 大型站点方案简述 已补充名词解释
  2. 法线变换详解 和 3D 变换中法向量变换矩阵的推导
  3. 脑图工具MindNode附属节点是什么意思 图解
  4. 软件工程与计算机科学的区别
  5. 截取含有中文、Emoji表情、特殊符号的字符串
  6. Pandas 基础(16) - Holidays
  7. 中国最年轻的亿万富翁_我和一个亿万富翁一起吃饭。 他给我的建议是无价的。
  8. ubuntu下git搭建服务器(gitosis)
  9. 华为 P10 手机开始推送鸿蒙 HarmonyOS 2 内测版
  10. 浅谈STM32的三种Boot模式