fatal: refusing to merge unrelated histories 解决方案

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。


当本地分支与远程分支没有共同祖先时,会出现 fatal: refusing to merge unrelated histories 的问题。


可以使用 rebase 的方式来进行合并。

git pull --rebase origin master



mkdir rebaseTmp
cd rebaseTmp
echo "hello a line" > tmp.txt
git init
git add .
git commit -m "Local first commit"
git remote add origin  https://github.com/HustLion/java_console_log4j.git
git pull --rebase origin master


warning: no common commits
remote: Counting objects: 32, done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 32 (delta 3), reused 28 (delta 2), pack-reused 0
Unpacking objects: 100% (32/32), done.
From https://github.com/HustLion/java_console_log4j
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
First, rewinding head to replay your work on top of it...
Applying: Local first commit


├── README.md
├── pom.xml
├── src
│   ├── main
│   │   └── java
│   │       ├── com
│   │       │   └── hustlion
│   │       │       └── java_console_log4j
│   │       │           └── App.java
│   │       └── log4j2.properties
│   └── test
│       └── java
│           └── com
│               └── hustlion
│                   └── java_console_log4j
│                       └── AppTest.java
└── tmp.txt

可以看到我们的本地 commit 带来的 tmp.txt 与远程项目通过 rebase 很好地合并在了一起。





Problem DescriptionnThere are N number,they are respectively from 1 to N.nnfor a set of numbers, we set a problem as flow:nnwe assume that the initial position of each number is the corresponding positon on number axis.nnThen what we have to do is to make the minimum and maximum number of meet in minimum time.nnWe can operate these numbers as flow:nnEach operation, we can select two adjacent numbers on the number axis and exchange them.multiple operations can be performed at the same time.nnthe time it spend is the distance between two position.nnWhen the minimum and maximum number went to the adjacent positions,the time of they meet is distance/2.nnHere is an example:nn1..4......11....16..19nnWe want 1 and 19 meet.nnLet 1,4 and 16,19 exchange, they can be performed at the same time and end at the same time.It spend 3 units time,and becomes the following:nn4..1......11....19..16nnThen there are two Plan:nnA Plan:nnlet 1 and 11 exchange with 7 units time first.And it becomes the following:nn4..11......1....19..16nnThen exchange 1 and 19 with 2.5 units time.nnthe total times is:3+7+2.5=12.5nnPlan B:nnLet 11 and 19 exchange with 5 units time first.it becomes the following:nn4..1......19....11..16nnlet 1 and 19 exchanged with a time of 3.5 units.nntotal:3+5+3.5=11.5nnClearly, B Plan work better.nnNow everyone seems to understand the meaning of the problem.nnSo the question now is like the followingnnat the beginning, there are N sets, the i-th set contains and contains only i.nnThen N-1 operations flow: nnMerge two set.nnAnd you should calculate the minimum time of making the biggest and smallest number meet after merging in this set.n nnInputnThe first line contains a number T(T<=5), indicating T cases of data.nnfor each case of data:nnThe first line contains a number N.nnThen N-1 lines flow, nneach line contains two different integers u, v.nnthey Represents we merge two set where u and v are.nnwe promise that u and v are not at the same set. nn* 1≤N≤300000n nnOutputnfor each case of data:nfor each line, output the minimum time after merging, accurated to 0.1.n nnSample Inputn1n3n1 2n1 3n nnSample Outputn0.5n1.5 问答