anatoly techtonik
anatoly techtonik

Reputation: 20531

How to set default push repository in Git?

How can I make git pull from [email protected]:... repository and git push to [email protected]:... repository by default?

In Mercurial I create .hg/hgrc with the following content:

[paths]
default = ssh://[email protected]/...
default-push = ssh://[email protected]/...

What is the way to set the same default behaviour in Git?

Upvotes: 7

Views: 6762

Answers (3)

anatoly techtonik
anatoly techtonik

Reputation: 20531

I combined hints from @torek, @klaustopher (thanks for the answers) and duplicate link from @Mali to make it easier for people from Mercurial background to find the answer.

To add default-push in Git, use:

$ git config remote.origin.pushurl [email protected]:yourname/project.git

To check current setup:

$ git remote -v
origin  [email protected]:user/project.git (fetch)
origin  [email protected]:yourname/project.git (push)

Upvotes: 5

torek
torek

Reputation: 488453

In git the remotes are in the [remote "<name>"] sections. When you clone something, the initial remote is origin, so that's usually the one to use. The fetch URL is the url and the push URL is the pushurl. You also need a fetch line (or several lines) to bring over branch names.

Typically, then, you will see, in .git/config, something like:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = ssh://...

Change the url to git://source and add pushurl = git://target (you can do this with git config commands, or git remote, or just run git config -e to bring up your regular editor on the file).

Branches need to have an "upstream" set (in two parts, remote and merge) to cause them to "track" a "remote branch". Typically since the remote is named origin you set branch.master.remote to origin and branch.master.merge to master, for instance. (When you create a local branch based on a remote branch, in any even slightly modern version of git, it will set it up as "tracking" for you.)

Generally you should also configure push.default to something other than the git 1.x default value; simple is probably best for avoiding accidents.

Note that if you're used to Mercurial, hg pull is most similar to git fetch, not git pull; hg pull -u is somewhat closer to git pull, but I recommend training yourself to use git fetch instead of git pull (even if you're not used to Mercurial, actually :-) ). It also takes some adjusting to the different ways hg and git handle branches (hg has one single global name space for branches, and another single global one for bookmarks; git has per-remote "remote branch" name spaces, and local branches are more like hg local bookmarks.)

Upvotes: 5

klaustopher
klaustopher

Reputation: 6941

It's almost the same in git. Each repository has a .git/config file and there should be some thing like this in it:

[remote "origin"]
url = git://path/to/your/repo
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master

You don't have to add those manually, there's quite a few commands that help you. A good start is the Git SCM Book.

To set the default fore one single branch you can simply type: git branch --set-upstream-to YOUR_REMOTE_NAME/YOUR_BRANCH_NAME(when using git >= 1.8.0)

Upvotes: 6

Related Questions