Mark Harrison
Mark Harrison

Reputation: 304484

git: how can I programmatically tell if a repo can be pushed?

I see this output from a cloned repo for which I have write permission:

$~/g/cad --> git push 
Everything up-to-date

and this output for one for which I don't:

$~/g/opentx --> git push
Username for 'https://github.com': 

How can I tell programatically that a particular repository has write permission?

Upvotes: 0

Views: 117

Answers (1)

larsks
larsks

Reputation: 311635

The only way to tell if you have write permission to a remote repository is to try writing something there by pushing a commit or a tag or something.

I guess you try to push a tag, then delete the tag:

git tag a-test-tag
git push origin a-test-tag
git push --delete origin a-test-tag

If the push is successful you probably have write permissions on the remote repository (I say "probably" because you may only be able to update tags but not push commits, or you may only be able to push commits to specific branches, etc).

You can cause git to fail if authentication is required, rather than prompting for credentials, by setting the GIT_ASKPASS environment variable to true:

GIT_ASKPASS=true git push origin a-test-tag

Upvotes: 1

Related Questions