5

How can I make git pull from git@source.com:... repository and git push to git@target.com:... repository by default?

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

[paths]
default = ssh://hg@source.com/...
default-push = ssh://hg@target.com/...

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

anatoly techtonik
  • 19,847
  • 9
  • 124
  • 140
  • duplicate of : http://stackoverflow.com/questions/2916845/different-default-remote-tracking-branch-for-git-pull-and-git-push – Mali Dec 06 '13 at 13:50
  • Incidentally, it's unusual (though not impossible) to push via `git://`. As the man page notes, "disabled by default, as there is no authentication in the protocol (in other words, anybody can push anything into the repository, including removal of refs). This is solely meant for a closed LAN setting where everybody is friendly." – torek Dec 06 '13 at 14:27
  • @torek, fixed links to be more real. – anatoly techtonik Dec 06 '13 at 18:56
  • Does this answer your question? [Changing the Git remote 'push to' default](https://stackoverflow.com/questions/18801147/changing-the-git-remote-push-to-default) – BuZZ-dEE Jul 08 '20 at 20:16

3 Answers3

5

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)

klaustopher
  • 6,702
  • 1
  • 22
  • 25
  • Thanks for the hint - Git SCM definitely helps - I can now see effective settings with `git remote -v`. Regarding this answer, I can't see how to set `default-push` here. I am not sure I am proficient with branch stuff in Git. – anatoly techtonik Dec 06 '13 at 14:08
5

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.)

torek
  • 448,244
  • 59
  • 642
  • 775
  • Thanks for the explanation. This `remotes` jargon is unusual, but ok. I also don't use branches much, so this is kind of extra info for me right now. Forgot to say - `pushurl = git://target` helps! – anatoly techtonik Dec 06 '13 at 14:15
  • In git, you're always forced to use the remote and local branches and named "remotes", as that's just how it works. Hg's single branch name-space means that if you just use branch `default` (and maybe even a few more), things are a lot easier, but they turn out to be much less flexible. The good stuff is bad and vice versa, depending on your needs; that's life! :-) – torek Dec 06 '13 at 14:19
4

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 git@github.com:yourname/project.git

To check current setup:

$ git remote -v
origin  git@github.com:user/project.git (fetch)
origin  git@github.com:yourname/project.git (push)
anatoly techtonik
  • 19,847
  • 9
  • 124
  • 140