How can I check write access to a remote Git repository ("can I push?")

GitGithubGit RemoteLs Remote

Git Problem Overview


I am building a (somewhat limited) Git client. To set up a repository, you enter the URL to the remote repo. I want to check whether the user has read+write access to that repository. If not, I present an authentication dialog.

I check 'read' access with git ls-remote <url>.

Is there an analogous way to check 'write' access, without cloning the repo first? (I know I could git clone <url> and then git push --dry-run)

Git Solutions


Solution 1 - Git

If the git repo is in github, open any file in the repo, then click 'edit', github will show something like this:

> You’re editing a file in a project you don’t have write access to. We’ve created a fork of this project for you to commit your proposed changes to. Submitting a change to this file will write it to a new branch in your fork, so you can send a pull request.enter code here

Solution 2 - Git

With the latest GitHub API you can check the permissions on a particular repo for a USERNAME (yours or another user) with the following command (you'll need to authenticate using your personal access token) e.g.:

curl -u your_username:your_access_token \
 -H "Accept: application/vnd.github.v3+json" \
 https://api.github.com/repos/octocat/hello-world/collaborators/USERNAME/permission

The response will show what permissions the USERNAME has for that repo. Otherwise it will report:

> "message": "Not Found"

Note that you should have push access to the repo in question or it will fail with

{
  "message": "Must have push access to view collaborator permission.",
  "documentation_url": "https://docs.github.com/rest/reference/repos#get-repository-permissions-for-a-user"
}

Solution 3 - Git

You may perform git push git+ssh://host.org/path/to/repo some_ref without cloning.

But remember that pushing a chain of commits and update remote references are two different operations and you may have a permission for the former, but no permission for the latter. Also certain references could be unmodifiable.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYang MeyerView Question on Stackoverflow
Solution 1 - GitguyskkView Answer on Stackoverflow
Solution 2 - GitPierzView Answer on Stackoverflow
Solution 3 - Gituser3159253View Answer on Stackoverflow