views:

94

answers:

3

I started a new project and created a local git repo with "git init" and now I have a few branches and everything works great.

However, since my webhosting company offers git hosting (details if you're curious), I'd like to push my entire repo to their servers to have a backup in the cloud in case something bad happens to my local repo.

How can I make the remote repo the "origin" since the repo was started locally?

A: 

Use git remote:

git remote add origin http://your.push.url

The first time you push, you will have to name the branch:

git push origin master

Subsequently, you can just:

git push

since origin is the default. To push other branches, see the git push documentation.

Greg Hewgill
Are you saying that `git push <remote> <branch>` on a branch with no configured remote will automatically configure that remote/branch as default?
Jefromi
@Jefromi: Not quite, `git push` with no named remote defaults to `origin`, so you have to manually push your branch to `origin` first. If you used another remote name like `github`, then you would have to use `git push github` instead.
Greg Hewgill
@Greg Hewgill: Oh, the push will work, right. But a pull won't without the configuration.
Jefromi
@Jefromi: Quite right, thanks for the clarification.
Greg Hewgill
+3  A: 

Do this:

git remote add origin <url of remote>

The url is the same you'd use to push, perhaps something like "ssh://user@host/path/to/repo.git"

You can go look at .git/config to see what it's set up there, if you like.

You may also want to add under [branch "master"] in .git/config (add the section if it's not there):

[branch "master"]
    remote = origin
    merge = refs/heads/master

to tell git to associate your master and the remote master branch, so that when you just type "git pull" or "git push" with master checked out, it'll assume you meant origin's master.

This could also be done with the pair of commands:

git config branch.master.remote origin
git config branch.master.merge refs/heads/master
Jefromi
A: 

If you started you repo locally, then you have no origin defined and git remote should produce no output. So you just need to define the new origin with, for example:

git remote add origin url
gdelfino