How many times have you started a project locally on your machine, initialized it as a git repository, then after doing some work, realized that you have a repository sitting on GitHub or Azure DevOps with a readme markdown file and a .gitignore file? If you’re like me, that probably happens more than you’d like to admit. There’s an easy solution for this.

First, we need to add a remote. We can do this by using the following command:

1
git remote add origin https://{path-to-repository}

Next, we need to take our existing branch, presumably it’s the master branch, and create the link between the origin\master and the local master branch:

1
git branch --set-upstream-to=origin/master master

Finally, when we pull our changes, we may run into the following:

1
2
>git pull  
 fatal: refusing to merge unrelated histories

This can be solved by merging in the unrelated histories. We can do this by using:

1
git pull --allow-unrelated-histories

Just note, we may now have merge conflicts as there can be files with the same name in both locations. However, we can follow our normal diff processes to determine which file or portion of each we need to keep.