Is there a way to git push
, but, if the branch doesn't exist in the remote, throw an error or exit non-zero instead of creating a new branch on the server?
The use case is the following. I am creating scripts to help automate the scm workflow in my company. If someone accidentally mistypes a branch name as input into the script, I don't want a new branch created on the remote. I already can manually check remote branch existence, but I wondered if git supports this feature.
No, there is currently not a way to do this with a single call to
git-push
.Possible workarounds:
Existence of a remote branch can be checked like this:
Which could be included in a
pre-push
hook as well if desired. Something like this (place in.git/hooks/pre-push
):Which will cause the desired behavior:
If you have access to the server you can also create a
pre-recieve
hook to reject creation of new branches.