1

My company uses Gerrit Code Review for Git. I've been given a custom incantation to use every time I want to push code changes:

git push origin HEAD:refs/for/my-branch-name

Other than "my-branch-name", that's it verbatim. The refs/for/ syntax seems crazy to me, and is not something I'd seen before in Git.

How can I configure my repo so that git push alone does the same as the above?

Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • 1
    See also the related question, ["Why do git push gerrit HEAD:refs/for/master instead of git push origin master"](http://stackoverflow.com/q/10461214/405017). – Phrogz Dec 20 '12 at 00:18
  • Duplicate of [git alias for HEAD:refs/for/master](http://stackoverflow.com/questions/7423893/git-alias-for-headrefs-for-master) – Phrogz Dec 20 '12 at 00:26

3 Answers3

2

That syntax isn't crazy, it's just the full git push syntax. When you do git push by itself, it really works out to be:

git push origin current-branch:refs/heads/current-branch

You're probably using gerrit for code review, so instead of just pushing directly onto a particular branch, you want to let gerrit know about the change - gerrit uses the refs/for area for that purpose.

Newer versions of gerrit use refs/publish and refs/drafts, so you might see those, too.

If you want something more convenient, you can easily make aliases and add them to your git configuration. Check out this question: git alias for HEAD:refs/for/master

Community
  • 1
  • 1
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • _"you can easily make aliases and add them to your git configuration"_ You presume too much, sir. :p – Phrogz Dec 20 '12 at 00:24
  • Thank you for finding [the duplicate question](http://stackoverflow.com/questions/7423893/git-alias-for-headrefs-for-master) that has the answer I need. I'll +1 you for that, and vote to close my own question. :) – Phrogz Dec 20 '12 at 00:26
2

You can update the push ref-spec for your remote by typing

git config remote.origin.push HEAD:refs/for/my-branch-name

I've created a git post-checkout hook that makes sure all branches (and master) are configured for use with gerrit. I think it works but your work flow might differ from mine so read the script and understand it (it's not hard) before using it.

Andreas Wederbrand
  • 38,065
  • 11
  • 68
  • 78
0

refs/for/.... is a "magical" ref that Gerrit uses to trigger and keep track of code reviews for a particular branch. This is documented at http://gerrit-documentation.googlecode.com/svn/Documentation/2.5.1/user-upload.html

You need to push to this ref so that Gerrit can pick up your changes and start the code review process.

If you just did git push origin master then it would bypass Gerrit since your commits would already be in the master branch. The whole point of Gerrit is make sure that your code is reviewed before it makes it into master (or whatever).

regulatethis
  • 2,322
  • 14
  • 17
  • Yes, but this doesn't answer the question of how to configure the repo so that `git push` does the magical incantation for me. – Phrogz Dec 20 '12 at 00:23
  • You can create a git alias to do that. – regulatethis Dec 20 '12 at 00:24
  • Given that this is the entire question, perhaps you should amend your "answer" to show how to do this. Otherwise, this is a helpful comment, but not an answer. – Phrogz Dec 20 '12 at 00:25