0

I am working with Gerrit and when I push a commit, I use the syntax git push origin master:/refs/for/master

I would like to configure Git to automatically push my current branch to refs/for/master if my current branch is master. My claim is to be able to keep using git push to push to Gerrit review server instead of Git server.

My config file

[remote "origin"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = git@GitServer:/myrepo
[branch "master"]
        remote = origin
        merge = refs/head/master
[url "ssh://user@GerritServer:29418/myrepo.git"]
  pushInsteadOf = git@GitServer:/myrepo

Note: The git repository is not handled by Gerrit, this is why I used the option pushInsteadOf to push to Gerrit server.

Flows
  • 3,675
  • 3
  • 28
  • 52
  • I don't quite follow. If you simply want to be able to write `git push` instead of `git push origin master:refs/for/master` then you just need to add `push = +refs/heads/*:refs/for/*` to your git config under `[remote "origin"]` – Dunno Aug 02 '16 at 14:19
  • Could this perhaps be done with `git branch --set-upstream my_branch origin/my_branch`, where `my_branch` is your local branch and `origin/my_branch` is your remote branch – e.doroskevic Aug 02 '16 at 14:21
  • @Dunno , I don't want to push to `refs/for/*` for any branch of my project (because only master branch is set up on Gerrit). I want only `master` branch to be impacted. I was looking on add a key word on `[branch "master"]` section. If I don't have other solution, I'll live with it. – Flows Aug 02 '16 at 14:26
  • @e.doroskevic, does this will impact pull to ? This should not for my case – Flows Aug 02 '16 at 14:27
  • So if you're on master, you want to push to `refs/for/master` and in other cases to upstream branches? – Dunno Aug 02 '16 at 14:27

1 Answers1

2

First set up the upstream branch for master:

git push -u origin master:refs/for/master

Then set the default push strategy to upstream:

git config push.default upstream

With this setup, whenever you write git push, git will check what is the upstream branch for your current branch and push to it. This also means that you'll have to specify upstream branches for all branches you want to push in the future

Setting upstream branch doesn't affect pulling. Actually, that is the reason it's been called upstream, in the old days it used to be called tracking just like in pulls, and it caused a lot of confusion. Read on in the related commit message

duplicate-ish of Git push: set target for branch

Community
  • 1
  • 1
Dunno
  • 3,632
  • 3
  • 28
  • 43
  • I had to change manually in config file under section `[branch "master"]` the line `merge = refs/for/master` to make it functional. – Flows Aug 02 '16 at 14:50