Git refusing to merge unrelated histories on rebase
Asked Answered
L

33

3660

During git rebase origin/development the following error message is shown from Git:

fatal: refusing to merge unrelated histories
Error redoing merge 1234deadbeef1234deadbeef

My Git version is 2.9.0. It used to work fine in the previous version.

How can I continue this rebase allowing unrelated histories with the forced flag introduced in the new release?

Labored answered 21/6, 2016 at 7:23 Comment(8)
@Shishya With all due respect the most voted answer doesn't solve this question in a direct manner. The question asks for git-rebase situation while the answer gives a flag for git-mergeLabored
In case someone else made the same mistake, I got this error after accidentally using git pull [repo URL] instead of git clone [repo URL]Etamine
Possible duplicate of Allow merging unrelated histories in git rebaseRetaretable
A mess has been made here by the fact that the title doesn't specify that this is in the context of a rebase, so your question is drawing in Googlers who are getting this error in different contexts and upvoting an answer that doesn't actually apply to the question you've asked. It can't be easily cleaned up now, so the incoherent Q&A pair will remain on the site and high in the Google search results forever. The moral of the story is that question titles matter!Agace
@MarkAmery: "It can't be easily cleaned up now" Why can't we just fix the title to at least cut the wrong path of meme evolution? Am I missing something?Tops
The best answer to this question can actually be found in this SO question. It is safer, more accurate, and better explained that the top answer here.Incurvate
This questions is being discussed on meta, our site for discussing posts, matters of scope, support, and other items related to Stack Overflow.Newkirk
I still am not able to find the Git version 2.9! Latest version is: 2.42.1Logorrhea
S
4170

You can use --allow-unrelated-histories to force the merge to happen.

The reason behind this is that default behavior has changed since Git 2.9:

"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.

See the Git release changelog for more information.

More information can be found in this answer.

Spontoon answered 21/6, 2016 at 7:26 Comment(15)
Know the merge change but this option won't work with rebaseLabored
Is there any option which will turn on --allow-unrelated-histories permanently?Hyohyoid
@Hyohyoid "Because such a "two project merge" is a rare event, a configuration option to always allow such a merge is not added.". So no.Spontoon
I tried to merge a branch for a different repo this way but it created a new commit on my current branch and didn't keep history from the other repo. Then I checked out a local branch from the other repo and only then merged it and suddenly a normal merge commit appeared. Weird.Mabe
Excellent, works with git pull as well. Was in that "rare event that merges histories of two projects that started their lives independently". git --work-tree="." pull --allow-unrelated-historiesViehmann
This answer worked for me, even though I started with a rebase, I ended up doing a merge with this flag. I created a branch with --orphan because I wanted to create a pull request while actually it was all in the same project with the same sources...Tagalog
I was attempting to pull the latest upstream/master to my local master, & it failed with the above error. Once I used --allow-unrelated-histories it resulted in a merge conflict. What I really needed, therefore, in this case, was not git pull --allow-unrelated-histories upstream master, but rather was a hard reset (DANGER you'll overwrite local files) to make my local master once again fully track the upstream master: git reset --hard upstream/master was perfect. Source: stackoverflow.com/questions/1125968/…Cini
Oops: I forgot: do a git fetch --all first too. That makes the full answer git fetch --all to fetch the latest upstream master and update my remote tracking branches, such as upstream/master, and then git reset --hard upstream/master to reset my HEAD on my local, checked-out master to point to the remote-tracking branch I just fetched: upstream/master.Cini
2000+ upvotes on this question. I don't call that a "rare event".Reversal
@Hyohyoid in my opinion the best way to do that is to abbreviate the flag. create an environment variable export auh="--allow-unrelated-histories", then you could call git pull $auh. Otherwise maybe git config has on option for it, not sureBloodsucker
Oops...! using this command "git pull <remote> --allow-unrelated-histories" I got conflicts in my projectGad
as much as this answer have good information, you can find way more complete answer elsewhere https://mcmap.net/q/12921/-quot-refusing-to-merge-unrelated-histories-quot-failure-while-pulling-to-recovered-repositoryMorrow
@Hyohyoid Have you tried aliasing it git config --global alias.merge 'merge --allow-unrelated-histories'?Degauss
I usually got this issue when I try to push my local project to Github new repo, and which have an extra license or README.md file.Banerjee
I get this problem when I develop an application on one computer, push it to github, and try to pull to another computer on which I had started a local repo for the project.Themselves
O
2038

In my case, the error was just fatal: refusing to merge unrelated histories on every try, especially the first pull request after remotely adding a Git repository.

Using the --allow-unrelated-histories flag worked with a pull request in this way:

git pull origin branchname --allow-unrelated-histories

As per 2.9.0 release notes - git pull has been taught to pass the --allow-unrelated-histories option to underlying git merge

Often answered 18/10, 2016 at 12:13 Comment(11)
I always see this error if when I create a new Github repository with a README.md, then pull it to a local repository at the first time. So annoying.Yakutsk
@TienDo See this answer: stackoverflow.com/questions/39761024/…Often
For new repos, first pulls, it's typically better to start with a git clone.Vaticide
Great !, but what it does actually --allow-unrelated-histories ??Wollis
@PardeepJain Please see this github.com/git/git/blob/master/Documentation/RelNotes/…Often
This stopped me for several hours, before I realized there had to be an obvious resolution for merging files like this if it occurs for default files - I'm glad I"m not the only one who has had this problem at least!Zwick
In my case it happened because I added license file at github. The command mentioned above (and below, they are the same) worked.Arrestment
even the github docs doesn't include this --allow-unrelated-histories or the git clone tip. help.github.com/en/articles/…Flintshire
should been a comment or edit on the previous answer, to avoid redundancy and misinformation https://mcmap.net/q/12641/-git-refusing-to-merge-unrelated-histories-on-rebaseMorrow
Solved my problem but I did not understand the background or what the problem causes or whatever. Typical git error: I am just lucky that it works nowAnkle
@TienDo You should be able to instantiate a repo without a README.md in Gitlab and still push the local repo at first time . Or alternatively this command will push and create the project (if doesn't already exist) in 1 go : git push --set-upstream 10.XX.XXX.XXX/gitlab/login/root/demoproject.git masterLysimeter
C
744

Try the following command:

git pull origin master --allow-unrelated-histories

This should solve your problem.

Carothers answered 20/11, 2017 at 16:41 Comment(6)
how is this any better than the first answer?! https://mcmap.net/q/12641/-git-refusing-to-merge-unrelated-histories-on-rebase should've been a comment or edit on it, to avoid redundancy and misinformation.Morrow
cause it solves tl:drLeguminous
@Leguminous good luck with that. glancing over some things will need more luck than most people would ever like to admit.Morrow
This is the right solutionBerl
same as top-voted answer that was already in place earlier: should not have been addedLatticework
FYI, what this command does is pull/merge commits from the master branch into the current branch. Once that is done, the current branch and master branch will now have a shared history and you will now be able to open a pull request + merge from current branch to master branch.Radborne
P
342

I got this error when I set up a local repository first. Then I went to GitHub and created a new repository. Then I ran

git remote add origin <repository url>

When I tried to push or pull, I got the same fatal: unrelated_histories error every time.

Here is how I fixed it:

git pull origin master --allow-unrelated-histories
git merge origin origin/master
... add and commit here...
git push origin master
Pisci answered 8/6, 2017 at 17:41 Comment(5)
I think we were in the same boat. To add something: My problem was that there was already something on the remote repo. So in my folder, it deleted the .git folder, ran git init and did what Adithya said, except for the merge part.Horseplay
How to press INSERT button on mac? Actually, I have to type the commit message and do merge from the command line, But I don't know how to do it from the command line.Shluh
Does it open vim? If it does, it is just SHIFT + :Pisci
This is a really good answer. The point is that you have to force pull then merge local and remote repo.Chalice
FACED THE SAME SITUATION AND RESOLVED IT: First, I have created a project in local and continue on my development. Later, create a project and branch over there in bitbucket. I tried this approach and solved my issue. Thanks @AdithyaBhatJoachim
C
205

1. Solve the problem

Following the error when doing a git pull origin master:

fatal: refusing to merge unrelated histories

Run one of the below commands

 # It could be master
 git pull origin master --allow-unrelated-histories

 # Or main
 git pull origin main --allow-unrelated-histories

 # Or just try with origin
 git pull origin main --allow-unrelated-histories

If it opens the nano editor you can just save and close with Ctrl + X.

Now push the changes on local

 git push

Error during merge or Error on other git operations

Keep in mind that this error could happen in other operation performed between 2 different branches. For instance when trying to merge 2 branches which don't have related history which makes sense.

Eitherway, simply add --allow-unrelated-histories should again, do the trick

git merge some_branch --allow-unrelated-histories

2. Meaning

  • The error:

The “fatal: refusing to merge unrelated histories” Git error occurs when two unrelated projects are merged (i.e., projects that are not aware of each other’s existence and have mismatching commit histories).

By default, git merge command refuses to merge histories that do not share a common ancestor. This option can be used to override this safety when merging histories of two projects that started their lives independently. As that is a very rare occasion, no configuration variable to enable this by default exists and will not be added.

Enter image description here


References


More on Stack Overflow

Corrie answered 18/12, 2020 at 12:33 Comment(0)
I
170

For this, enter the command:

git pull origin branchname --allow-unrelated-histories

For example,

git pull origin master --allow-unrelated-histories

Reference:

GitHub unrelated histories issue

Islas answered 7/12, 2018 at 4:29 Comment(3)
thanks for first time pull its works for me "git pull origin master --allow-unrelated-histories"Mosenthal
should've been a comment or edit on it, to avoid redundancy and misinformation. https://mcmap.net/q/12641/-git-refusing-to-merge-unrelated-histories-on-rebaseMorrow
git merge main --allow-unrelated-histories I tried this for merge origin/main to live-origin/devCeasefire
C
132

I ran this command and the issue got resolved.

git pull origin branchName --allow-unrelated-histories

Check out this page for more information.

Catalpa answered 30/8, 2020 at 18:42 Comment(1)
git merge origin/main localBranchName --allow-unrelated-histories work for me.Hexagon
A
108

I had the same problem. Try this:

git pull origin master --allow-unrelated-histories 

git push origin master
Antitoxin answered 4/11, 2018 at 11:39 Comment(1)
should've been a comment or edit on it, to avoid redundancy and misinformation. https://mcmap.net/q/12641/-git-refusing-to-merge-unrelated-histories-on-rebaseMorrow
N
56

For Android Studio and IntelliJ:

First, commit everything and resolve any conflicts.

Then open the terminal from below of IDE and enter:

git pull origin master --allow-unrelated-histories

Now you can push.

Norbert answered 29/2, 2020 at 17:25 Comment(2)
Its an git command, unrelated to Android studio or IntelijiBoykins
@AlpitAnand Yes, because this problem is not related directly to android studio or IntelliJ, but I write the way of handling this issue in android studioNorbert
P
55

Try git pull --rebase development

Peavy answered 20/1, 2017 at 14:5 Comment(2)
This should probably be: git pull --rebase=preserve --allow-unrelated-histories developmentWatermelon
@RiccardoMurri Having just tried that, I wouldn't do that again. My new repo had some sample initialization files in it, and my local repo months worth of commits. Running this (with newOrigin branch rather than development) added the initial commit to the top of my local branch, effectively removing almost everything from it. I wanted the initial commit from the new remote to be at the bottom.Sykes
H
51

Since all the other answers are not actually answering the question, here is a solution inspired by this answer on a related question.

So you get your error doing git rebase:

$ git rebase origin/development
fatal: refusing to merge unrelated histories
Error redoing merge 1234deadbeef1234deadbeef

This error doesn't actually cancel the rebase, but you are now in the middle of it:

$ git status
interactive rebase in progress; onto 4321beefdead
Last command done (1 command done):
   pick 1234deadbeef1234deadbeef test merge commit

So you can now do the merge by hand. Find out the parent commits of the original merge commit:

$ git log -1 1234deadbeef1234deadbeef
commit 1234deadbeef1234deadbeef
Merge: 111111111 222222222
Author: Hans Dampf
Date:   Wed Jun 6 18:04:35 2018 +0200

    test merge commit

Find out which of the two merge parents is the one that was merged into the current one (probably the second one, verify with git log 222222222), and then do the merge by hand, copying the commit message of the original merge commit:

$ git merge --allow-unrelated 222222222 --no-commit
Automatic merge went well; stopped before committing as requested
$ git commit -C 1234deadbeef1234deadbeef
[detached HEAD 909af09ec] test merge commit
 Date: Wed Jun 6 18:04:35 2018 +0200
$ git rebase --continue
Successfully rebased and updated refs/heads/test-branch.
Humperdinck answered 22/6, 2018 at 9:4 Comment(0)
G
42

Warning: This will potentially overwrite the remote repository

This worked for me:

git push origin master --force
Guilbert answered 10/5, 2019 at 1:57 Comment(7)
But what actually happens with local and remote files?Zsazsa
As per I know and experienced, local files are intact. Remote files which you want to add in a specific folder gets added.Guilbert
Don't do this! This overwrites all remote files.Angele
Just include a disclaimer that this command overrides all files in master branch. Worked good for me. Thanks.Entreat
It works but is rather harsh, the --allow-unrelad-histories is more specific and appropriateIncult
This worked for me. It overwrites all remote files, but that is exactly what I wanted to doDeflective
why 'potentially'? If you are in the situation described in the answer it definitely will overwrite the remote repository. Also this doesn't answer the question at all.Ruck
A
32

Firstly, pull the remote changes to your local using the following command:

git pull origin branchname --allow-unrelated-histories

** branchname is master in my case.

When the pull command is done, the conflict occurs. You should solve the conflicts. I use Android Studio to solve conflicts.

Enter image description here

When the conflicts are solved, the merge is done!

Now you can safely push.

Athenian answered 25/3, 2020 at 17:26 Comment(1)
I've been searching for the button to Resolve Conflict in AS. Sometimes the right-bottom popup/ballon disappear & I'm unable to do anything. Thanks @AthenianAsia
A
30

I had the same problem. The problem is remote had something preventing this.

I first created a local repository. I added a LICENSE and README.md file to my local and committed.

Then I wanted a remote repository so I created one on GitHub. Here I made a mistake of checking "Initialize this repository with a README", which created a README.md in remote too.

So now when I ran

git push --set-upstream origin master

I got:

error: failed to push some refs to 'https://github.com/lokeshub/myTODs.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes
(e.g. hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Now to overcome this I did

git pull origin master

Which resulted in the below error:

From https://github.com/lokeshub/myTODs
branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories**

I tried:

git pull origin master --allow-unrelated-histories

Result:

From https://github.com/lokeshub/myTODs
 * branch            master     -> FETCH_HEAD
Auto-merging README.md
CONFLICT (add/add): Merge conflict in README.md
Automatic merge failed;
fix conflicts and then commit the result.

Solution:

I removed the remote repository and created a new (I think only removing file README could have worked) and after that the below worked:

git remote rm origin
git remote add origin https://github.com/lokeshub/myTODOs.git
git push --set-upstream origin master
Arboriculture answered 22/2, 2019 at 7:24 Comment(4)
creating a new repository is not a solutionPrater
git pull origin master --allow-unrelated-histories worked for me .. ThanksSympathetic
git push --force ... would be a proper solution on step 1 in this particular caseEupatrid
This is not a solution. If you are beginner, then you can do that, but If you are working with some real projects, you should have to deal with the proper way.Zsazsa
D
27

This usually happens when you commit first time to remote repository. As error clearly says "refusing to merge unrelated histories", we need to use --allow-unrelated-histories flag.

git pull origin master  --allow-unrelated-histories

Now there would be some conflicts which we have to solve manually. After that just commit the code and push it.

Dearborn answered 25/9, 2019 at 10:47 Comment(1)
As mentioned in the question, I'm trying to do a git-rebase and not a git-pull, git-rebase doesn't have the --allow-unrelated-histories flag.Labored
L
18

When doing a git pull, I got this message fatal: refusing to merge unrelated histories for a repo module where I hadn't updated the local copy for a while.

I ran this command just to refresh local from origin. I just wanted latest from remote and didn't need any local changes.

git reset --hard origin/master

This fixed it in my case.

Lucier answered 21/1, 2020 at 11:33 Comment(5)
WARNING: This deleted ALL my files. Be careful if you do not know what you are doing!Avuncular
This will delete all pending changes!Jaela
This works great if what you want is to pull down what's on the origin and don't need to keep any changes.Constrictor
I Agree this will delete all ur files be careful, before using this command take a side copy of your app.Kikelia
Hm, I still my error remains.Lunch
T
17

I tried git pull --allow-unrelated-histories, and it didn't work, but this solved the issue for me:

  1. I copied all the files on my desktop repository to another folder and then deleted the folder.

  2. Then I clone the repository again because it is a new project.

  3. When I copied my files again and push it worked like charm.

Tipple answered 27/6, 2020 at 17:13 Comment(0)
U
14

I struggled with this as well, but I managed to find a workaround.

When you run into the error above, just cherry-pick the merge commit and then continue the rebase:

git cherry-pick -m 1 1234deadbeef1234deadbeef
git rebase --continue
Undistinguished answered 30/8, 2016 at 15:20 Comment(0)
S
12

If you created a new repository on GitHub and accidentally initialized it with README or .gitignore files.

If you found yourself unable to merge or rebase because .git folder got corrupted.

Then:

  • Create a new folder
  • git clone
  • Paste all your files into this folder

Now the local and remote will have "related histories" and will merge or rebase happily.

Shikari answered 31/7, 2020 at 8:56 Comment(0)
K
12

The error is resolved by toggling the allow-unrelated-histories switch. After a Git pull or Git merge command, add the following tag:

git pull origin master --allow-unrelated-histories

After then, maybe you will get a conflict. So resolve the conflict, and commit it. It worked for me.

Kadi answered 20/3, 2021 at 6:44 Comment(0)
S
9

For my case I wanted to merge an unrelated history branch to my current:

git merge <-unrelated-history-branch-name> --allow-unrelated-histories
Spiny answered 6/4, 2021 at 15:47 Comment(0)
M
9

I run into the same issue:

This is what I've done:

git pull origin main --allow-unrelated-histories

I used Visual Studio Code to resolve merge conflicts, and then I did:

git commit -m "commit message"

git push origin main
Markswoman answered 10/3, 2022 at 6:5 Comment(0)
R
7

blue112's answer doesn't solve this question, which is in the context of rebasing.

The only way to synchronize the two diverged branches is to merge them back together, resulting in an extra merge commit and two sets of commits that contain the same changes (the original ones, and the ones from your rebased branch). Needless to say, this is a very confusing situation.

So, before you run git rebase, always ask yourself, “Is anyone else looking at this branch?” If the answer is yes, take your hands off the keyboard and start thinking about a non-destructive way to make your changes (e.g., the git revert command). Otherwise, you’re safe to re-write history as much as you like.

Reference: The Golden Rule of Rebasing

Rugby answered 16/11, 2020 at 6:34 Comment(0)
S
5

If someone is facing the same problem while rebase the branch after doing --allow-unrelated-histories also please use this git pull origin <branch-name> --allow-unrelated-histories --no-ff hope it may help someone

Simonesimoneau answered 26/8, 2023 at 13:23 Comment(0)
O
4

This also happens if you have a shallow Git clone of the repository.

I recently encountered this problem in my CI/CD setup, and none of the previous solutions worked for me. I was building a CI/CD pipeline to analyze code on MR creation against the MR source branch. For that, I needed to run an analysis on the main branch once and then on the MR source branch merged with main, and I got this error while trying to merge branches via the git merge command.

The reason for this happening in the CI/CD setup: Usually in a CI/CD environment, Git repositories are a shallow clone to speed up things, which doesn't include a full history of commits due to which, when merging Git, we may think that we are trying to merge unrelated branches which is in fact not true.

Solution:

Convert a shallow repository to an unshallow one using the following command:

git fetch --unshallow
Openair answered 27/3, 2022 at 15:4 Comment(1)
when using the GitHub Action actions/checkout@v3, you can specify the fetch depth: with: fetch-depth: 0Khasi
K
4

A pretty rare scenario I believe; You have removed the remote (e.g. origin):

git remote rm origin

Then, after a while, for some reason, you tried to add it back but entered the wrong URL.

git remote add origin [email protected]:example/wrong.git

The solution of course is to use the right URL:

git remote add origin [email protected]:example/correct.git
Kuban answered 21/11, 2022 at 18:20 Comment(1)
unless you're pulling from different remotes... you can check the remotes your project has with git remote -v (or: git remote --verbose) also check any branch tracking info with git config -l (or: git config --list) in eg branch.main.remote=origin -see also the values for remote.origin.url remote._name_.urlKhasi
B
3

I am using the rebase for years and I had never encountered such a problem. However, your first problem is, that you try to do it directly on the remote branch development from the remote repository, called origin. That is literally wrong because rebase is a dangerous command, that restructures the git history. Having said that, you should first try on your local repository and pushing it only, if it works for you as expected.

So, my usual rebase workflow looks like following (but please keep in mind, that you should not use rebase on branches, which you are not the only one committee. For such branches, use simply merge and resolve conflicts, if applicable):

  1. make sure you have a clean working tree (no uncommit changes)
  2. checkout to the branch you want to rebase onto (for instance, let's say it's master; as a one-line command): git checkout master && git pull origin master && git checkout development
  3. Do the actual rebase: git rebase master
  4. If it's done and everything works as expected, push it to your remote. For doing so, you need to force it, because the remote host already has the history in another order, the remote would answer with nothing to push. So, we need to say "my local version of the history is correct, overwrite everything on that remote branch using my local version of the history": git push -f origin development

As I already mentioned, keep in mind, that rebase manipulates the git history, that is usually a bad thing. However, it's possible to do that on branches, where no one else commits to. In order to keep the branch pull-able for the other developers, use another merge strategy like merge itself, squash or cherrypick. So, in other words: Rebase shouldn't be your tool on distributed development. It works fine for you if you are the only one who works on this repository.

We use the feature branch strategy. In this, I usually use rebase in order to get the "updates" from the other developers, that happened in the meantime on the master branch. Doing so, it reduces the size of commits that are visible in a pull request. Therefore, it makes it easier for the code reviewer to see my changes made in this feature branch.

Baiel answered 4/9, 2019 at 6:44 Comment(1)
In this case, I actually wanted to continue with the rebase and the answer doesn't address that. I know the risks of rebasing and when I should & shouldn't use git-rebase. This is a general (opinionated) guideline for git workflow and doesn't directly answer the question. As far as using rebase for years, this particular error was added in v2.9.0 of git and the flow used to work fine before that release. What you've posted in this answer here is answered already in much older questions like https://mcmap.net/q/12190/-git-rebase-basics and git-scm.com/book/en/v2/Git-Branching-RebasingLabored
T
3

I faced this error after force-pushing to origin/master a development branch with a few hundreds of commits, by the Administrator, on the server side.

Well, I just didn't wanted to pull (fetch+merge), but just to align my local master to the remote origin master. Moving to a separate folder and doing a Git clone is one approach, but I believe it is more elegant solution just to do a hard reset.

So my answer to this error, in this particular case, is none of the above.

I just wanted this:

git reset --hard origin/master
Trevethick answered 14/12, 2021 at 15:14 Comment(0)
T
2

for me was moving all content to another folder then i did git pull then i moved files back then git add and then git commit -m "messag" then git push

Tumpline answered 4/2, 2023 at 17:48 Comment(0)
C
1

fatal: refusing to merge unrelated histories may also be caused by a shallow clone, because the graft commit doesn't go far enough down to see the common base.

Cortex answered 14/12, 2021 at 20:19 Comment(1)
What is a "graft commit"?Gimmal
U
1

I had the same error, and this command worked for me:

git pull gitlab master --allow-unrelated-histories

Note that gitlab might be origin or heroku in your case.

Unknit answered 3/3, 2022 at 12:29 Comment(1)
you can list your remotes with git remote -vKhasi
S
0

Since you're neither able to push nor pull-and-push nor merge-pull-and-push:

  1. You can create a new branch on the GitHub repository.
  2. And then:
  3. git add .
  4. git commit -m 'commitName'
  5. And reference that branch in your current directory in Terminal.
  6. git branch -m master branchName
  7. git push -f origin branchName
  8. Your code will be pushed to a new branch.
  9. And then you can merge these two branches.

This worked for me.

Stacte answered 3/3, 2021 at 12:18 Comment(0)
T
0

I had a situation where a repo was created for me with a single commit (containing only with README.md). I had "starter code" in another repo to merge.

In the end, this is what worked for me:

# clone target repo (that we want to merge into) 
git clone <REPO1>
cd REPO1
# add remote for repo to merge
git remote add repo2 <REPO2>
git fetch repo2
git checkout repo2/branch_to_merge
# we are in "detached head" state
git switch -c branch_temp
git checkout main
git merge branch_temp --allow-unrelated-histories
# address conflicts
git add .
git commit -m "fixed conflict with initial commit"
git push

Hope this is helpful to someone.

Trunnel answered 15/2 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.