Push current branch to Gerrit
Asked Answered
C

3

8

I like to use simply git push to push (only) the current branch to the configured upstream. For that I set git config push.default upstream which does exactly the right thing.

Now I tried to set up a a suitable remote for Gerrit, which automatically pushes to refs/for/*.

The idea was doing it this way:

[remote "gerrit"]
        url = ssh://host:port/repo
        push = refs/heads/*:refs/for/*

I would like to type git push gerrit and expect git to push only the current branch to the correct ref.

Unfortunately now git tries to push all local branches to Gerrit. - Is there any way to tell git to push only the current branch like with the push.default above?

Update:

  • Git push can only use a single refspec.
  • If an explicit refspec is given on the command line this one is used.
  • If none is given on the command line, the one defined with the remote is used.
  • If none is given with the remote, too, only then push.default is considered.

For Gerrit I need a refspec like 'branch:refs/for/branch' with branch being the current branch.

Question:

  • Is there an explicit refspec equivalent to what push.default=upstream is doing?
  • Is there a way to configure git to make a plain git push automatically push to the correct refs/for ref?

Currently the best way I found is wrapping everything in a separate script like suggested here. But I am still not convinced that it is not possible with just a suitable refspec.

Carven answered 14/1, 2013 at 14:30 Comment(1)
Ok. Just checking the different options here. I have learned that push.default is only considered if no refspec is given. And that if you supply a refspec (like HEAD) via command line, this overrides the refspec given with the remote. Hence it would just push to ref/heads, which is not what you want. Interesting.Trumpeter
C
1

According to the source, Git currently implements push.default = upstream by generating an explicit refspec containing explicit names.

Seems like there is currently no way to specify that behavior by a single generic refspec, and some script (like suggested here) which builds an explicit refspec is the only way for now.

Carven answered 19/1, 2013 at 21:36 Comment(1)
Interesting feedback, certainly more accurate than my (now deleted) answer. +1Trumpeter
S
1

As a workaround, I intercepted git function and get it to do the default push:

.bashrc

git ()
{
    if [ $1 = "push" -a -z $2 ]
    then
        /usr/bin/git push origin HEAD:refs/for/master
    else
        /usr/bin/git $@
    fi
}

I guess its a hack, but it works for me.

Souterrain answered 20/7, 2020 at 14:26 Comment(0)
A
-1

You can configure git to send only the current branch to the remote repository with the command git config --global push.default current.

The other options that you can pass to push.default are:

nothing - do not push anything.
matching - push all matching branches. All branches having the same name in both ends are considered to be matching. This is the default.
upstream - push the current branch to its upstream branch.
tracking - deprecated synonym for upstream.
current - push the current branch to a branch of the same name.

These configurations can be seen in git help config command.

UPDATE: However, reading more carefully the manual page, push.default "defines the action git push should take if no refspec is given on the command line, no refspec is configured in the remote, and no refspec is implied by any of the options given on the command line". So, I think you will need to use the script to send only the current branch when specifying a refspec.

Agrobiology answered 15/1, 2013 at 20:27 Comment(2)
Did you read my comment above before posting? I answered the same below (deleted, you cannot see it), and got a -1 ;)Trumpeter
Did you read the question completely? No, push.default is only considered if no refspec is given, i.e. it only pushes to heads.Carven

© 2022 - 2024 — McMap. All rights reserved.