3

I created local git repo. (git init, git add, git commit). Then I created git repo on Azure DevOps. I also executed locally git remote add origin <azure_repo_url>

Now when I try git push origin master I get back:

To azure_repo_url
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'azure_repo_url'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

So I tried pull first: git pull origin master but got back:

$ git pull origin master
warning: no common commits
remote: Azure Repos
remote: We noticed you're using an older version of Git. For the best experience, upgrade to a newer version.
remote: Found 3 objects to send. (27 ms)
Unpacking objects: 100% (3/3), done.
From azure_repo_url
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
fatal: refusing to merge unrelated histories

I can solve it by git pull origin master --allow-unrelated-histories from here so, but I wonder if there is cleaner way to do it?

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
user1700890
  • 7,144
  • 18
  • 87
  • 183
  • There are a lot of options. You could rebase. You could force push. Do you want to retain the history in the repo you created? – Daniel Mann Jan 02 '20 at 18:16
  • @DanielMann, In github there is option `...or push an existing repository from the command line` I would like to do the same on Azure, i.e. populate Azure repo from my local repo. – user1700890 Jan 02 '20 at 18:18
  • 1
    Then just force push. – Daniel Mann Jan 02 '20 at 18:19
  • Does this answer your question? [git: updates were rejected because the remote contains work that you do not have locally](https://stackoverflow.com/questions/24357108/git-updates-were-rejected-because-the-remote-contains-work-that-you-do-not-have) – phd Jan 02 '20 at 18:26
  • https://stackoverflow.com/search?q=%5Bgit%5D+hint%3A+Updates+were+rejected+because+the+remote+contains+work+that+you+do – phd Jan 02 '20 at 18:26
  • @phd, thank you for the link. My question is slightly different. In githut it is possible to create repo and populate it from local repo without need to force anything or to allow unrelated histories. I would like to do the same with Azure DevOps. – user1700890 Jan 02 '20 at 18:38
  • 1
    From your point you have now only 2 ways forward — it's either `git pull` or `git push --force`. Both are discussed at the linked question. – phd Jan 02 '20 at 18:42
  • @phd, I also came across the following: https://learn.microsoft.com/en-us/azure/devops/repos/git/share-your-code-in-git-cmdline?view=azure-devops I wonder if I can do it without Azure CLI? – user1700890 Jan 02 '20 at 18:45
  • 1
    If you don't have history in the Azure repo yet, then just force push. If you do, my suggestion would be to clone the Azure repo and move the new local files into that repo as a new commit, then push. – Josh Gust Jan 02 '20 at 18:54
  • The _force push_ option in global settings didn't help me. I didn't have console access. So if your local and remote are misaligned like this, click the branch at the bottom right, click manage branches, then you get a lot more options. You can use Rebase to get them aligned. – Nick.Mc Aug 24 '20 at 23:26

1 Answers1

2

When you create a new repo on Azure DevOps you have the option to initialize it, or leave it bare. When you initialize the repo, Azure DevOps adds a .gitIgnore and a readme.md.

enter image description here

enter image description here

This is generally the cause of the push/pull issues you see.

The easiest way to solve it is to do:

git remote add origin <azure_repo_url>
git fetch origin
git rebase master --onto origin/master
... fix any merge conflicts ...
git push origin master

or:

git remote add origin <azure_repo_url>
git pull --rebase
... fix any merge conflicts ...
git push origin master

The end result is that you end up with the local changes and the generated ignore and readme file. If there are more changes on the remote, you may need to do a more advanced version of the merge, but in the case of a otherwise clean remote, this should do.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • 2
    Awesome, unclicking `add readme` does the trick, I gives me option to `Push an existing repository from command line`. Thank you! – user1700890 Jan 03 '20 at 15:42