Flows Flows - 4 months ago 13
Git Question

Configure git push destination branch name different from the local name

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.

Answer

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

Comments