3

I set a new url to previously used origin in our server as

git remote set-url origin [new_url]

Now there's an error when I'm trying to pull

git pull origin dev
fatal: refusing to merge unrelated histories

I set the new url beacuse i don't want to create a new origin for doing stuff. How can I solve this?

Edit :-

When added the git pull origin dev --allow-unrelated-histories an error comes up again. and some untrack files are shown

error: The following untracked working tree files would be overwritten by merge:

I want to overwritten the files

Tharindu
  • 339
  • 6
  • 19
  • 2
    Duplicate of: https://stackoverflow.com/questions/38255655/trying-to-pull-files-from-my-github-repository-refusing-to-merge-unrelated-his/40959920 https://stackoverflow.com/questions/37937984/git-refusing-to-merge-unrelated-histories-on-rebase ? – Ripp_ Aug 20 '18 at 10:05
  • *"When added the git pull origin dev --allow-unrelated-histories an error comes up again. and some untrack files are shown `error: The following untracked working tree files would be overwritten by merge:` I want to overwritten the files"* - Just delete the listet files manually – Timothy Truckle Aug 20 '18 at 10:21
  • It may sound like a duplicate because the first error message is the same, but the scenario with the `git remote set-url` command is a little different – Jaap Jan 16 '20 at 12:48

1 Answers1

1

A git pull is just a combination of git fetch + git merge. The fetch operation should basically be conflict-less, so the problem arises from the merge part. If that merge is just a fast-forward, it's not really a merge and we can do a reset instead (i.e. git fetch + git reset instead of git pull).

I am assuming a few things first:

  1. You have no modified files when starting.
  2. Your dev branch contains no un-published commits.

Under those conditions you can replace git pull origin dev with the following:

git branch backup-of-dev-just-in-case dev
git fetch origin
git checkout dev
git reset --hard origin/dev   # This command is like "rm -rf", you should always double
                              # check conditions every time you use `--hard` so that you
                              # do not accidentally delete too much.

In case your dev branch did contain un-pushed commits, do the following additional steps:

git branch local-dev-changes backup-of-dev-just-in-case
git rebase --rebase-merges dev local-dev-changes
git checkout dev
git merge local-dev-changes
git branch -d local-dev-changes
hlovdal
  • 26,565
  • 10
  • 94
  • 165