Why Gerrit is unable to create branch by itself?
Asked Answered
F

4

11

Following this answer and my own question, I have a simple (hope so) question.

If I'm pushing a particular branch, with all required refs properly set:

git checkout 82-blah-blah
git push origin HEAD:refs/for/82-blah-blah

Why do I always get:

! [remote rejected] HEAD -> refs/for/82-blah-blah (branch 82-blah-blah not found)

and I always have to go to Gerrit's UI and create that branch manually?

Isn't that an obvious step, that Gerrit could simply automate? Or am I missing something?

Fick answered 16/12, 2013 at 8:28 Comment(4)
as a workaround first make an initial direct push to create the branch itselfPretense
If I do that, branch will be created, but the change itself (in Gerrit) won't and I'll be lost with a mess, unable to fix this situation. See these two my questions: one and two.Fick
first create the local branch without any new changes - git checkout -b <branch> origin/master then the remote: git push origin <branch> or via the Gerrit UIPretense
@HiB: for this to work, you need Gerrit permission to direct push, which is typically not desirable to have for average user - after all, whole point of Gerrit is that all commits should go through code review. OP wants to create branch after commit passes review (and not have this annoying error message).Mammal
M
3

This feature was implemented very recently and will be available in Gerrit v2.9.

Mammal answered 16/12, 2013 at 8:42 Comment(4)
OMG! Up until version 2.9 with such basic, fundamental feature not implemented! :]Fick
AFAICT the new branch creation REST and SSH commands that'll be available in Gerrit 2.9 won't address the original problem. It's just two new ways of manually creating branches, but since day one branch creation has been available through git push. I suspect the opinion of @Fick that this is a "basic, fundamental feature" stems from a misunderstanding that branch creation is a common task that's more or less always performed when uploading a change, when it's actually a rare operation that users typically aren't allowed to do.Eirene
@MagnusBäck: you are wrong. New feauture will create target branch after this commit passes review, which is that OP wantsMammal
@mvp: Where is this behavior implemented or described? Change 52500 which is the only change referred in issue 1156 only adds the SSH command. The original feature request of creating the branch upon change submission is not addressed by that change.Eirene
M
25

The accepted answer is referring to a feature that allows users to create branching using SSH, all it adds is the CreateBranchCommand. The original issue request might actually refer to what @trejder wants but the implementation is just a create branch through an SSH command.

I was under the impression that if you have the create-reference right you can push to ref/for/new-branch but I was wrong, just tested it and it doesn't work. It only allows you to create new branches but directly pushing to them.

Guess the fastest way to get it done is:

git checkout master
git push origin HEAD:new-branch
git checkout new-branch
git push origin HEAD:/refs/for/new-branch
Mancini answered 17/12, 2013 at 1:28 Comment(3)
My answer is not wrong, new Gerrit feature is exactly what OP is asking. Read this carefully: What I really want to be able to do with Gerrit is: git push origin HEAD:refs/for/some-new-branch and have the new branch created once the pushed code passes review.Mammal
I must agree with mvp. I'm actually asking, why do I have to use UI to do the same, what Gerrit can automate? Assuming, that I'm using Ctrl+C, Ctrl+V to use exactly the same branch name in both Git Bash and Gerrit's UI, I have exactly the same chance to misspell branch name or create to many of them, as I would have, if Gerrit would automate that process for me.Fick
I think it's clear what trejder wants and I agree with @Mammal that the original feature request in issue 1156 would match what is being asked for, but there's no evidence that the fix for the issue was implemented as originally requested. As indicated in another comment, Change 52500 only implements the backup plan described in the final paragraph of the issue.Eirene
M
3

This feature was implemented very recently and will be available in Gerrit v2.9.

Mammal answered 16/12, 2013 at 8:42 Comment(4)
OMG! Up until version 2.9 with such basic, fundamental feature not implemented! :]Fick
AFAICT the new branch creation REST and SSH commands that'll be available in Gerrit 2.9 won't address the original problem. It's just two new ways of manually creating branches, but since day one branch creation has been available through git push. I suspect the opinion of @Fick that this is a "basic, fundamental feature" stems from a misunderstanding that branch creation is a common task that's more or less always performed when uploading a change, when it's actually a rare operation that users typically aren't allowed to do.Eirene
@MagnusBäck: you are wrong. New feauture will create target branch after this commit passes review, which is that OP wantsMammal
@mvp: Where is this behavior implemented or described? Change 52500 which is the only change referred in issue 1156 only adds the SSH command. The original feature request of creating the branch upon change submission is not addressed by that change.Eirene
H
1

Use this simple bash script to create and push a new branch to gerrit. Usage: Branch_name and CommitId are 2 inputs needed

#!/bin/bash 
read -p "Enter New Branch Name: " Branch_Name 

read -p "Enter CommitID:" CommitID 

git fetch --all 

git branch $Branch_Name $CommitID 

git push origin $Branch_Name:$Branch_Name 

git checkout $Branch_Name 

git branch --set-upstream-to=origin/$Branch_Name $Branch_Name
Herbal answered 27/5, 2019 at 6:59 Comment(0)
B
0

Let say new branch is "myNewBranch"

git checkout master
git push origin HEAD:myNewBranch

After commit some changes to push the new commit:

git push origin myNewBranch

Works for me. But..

Do not know why can not push the same commit to master after that and the other way around - if I have opened PR for review in master, can not push the same commit to the "myNewBranch". To do it had to abandon the PR to master and then cherry-pick the commit to the new local branch and then push to the remote "myNewBranch"

Good luck!

Biochemistry answered 5/7, 2017 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.