How to link Git repos?

How do you link together two Git repos?

I’ve been managing a project in a local Git repository, and now I want to “publish” it on GitHub.

I’ve created a corresponding GitHub repo and I know I could easily clone that locally, copy over the files from the old repo, and then commit, but I’d like to preserve my local history. Is it possible to link my local Git repo to this new one on GitHub and push all my history up to it, and if so, how would I do this?

2 Answers

While Mtak’s answer is correct for older versions of Git, newer versions of Git would work as follows.

The first step would still be:

git remote add origin git@

Then—just in case you have been working on other branches—I would recommend switching to the local master branch like this:

git checkout master

With that done, simply running this “push” command would work:

git push

In some cases you might need to force that “push” with the -f flag like this:

git push -f

But for the first push you might need to run a command like this to set the upstream “origin” as well as “push”:

git push --set-upstream origin master

And again, in case you need to force that “push” just run the same command but with the -f flag like this:

git push -f --set-upstream origin master

And if you have other branches in your local repository you would like to push to “origin”, you might need to just run that same command with the new branch name. Like let’s say you have a branch named “develop” you would just need to checkout that branch like this:

git checkout develop

And then set the upstream “origin” for “develop” like this:

git push --set-upstream origin develop

You can add the Github repo as a remote repository using the command:

git remote add origin git@

And then push the repository using:

git push origin master

This will preserve all the history of your current local repository and push it to Github.

origin is the de facto standard name for a remote repository, if you only have one. You could name it something else.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like