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

261.5k views Asked by At

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)

8

There are 8 answers

0
dk4software On

Go the branch that you want to edit and hover over the edit button and it might say "Fork this repository and edit this file", if you have no access to the repository. But suppose you have some access to the repository, then the hover message will show "Edit this file", but you might not have access to commit to the branch. To check access to a particular branch, you can click on the edit button and on the submit changes form,you will see "Commit changes", if you have access. Or if you don't have access, you will see "Propose Changes" as it will create a new branch.

0
Alberto Salvia Novella On
GIT_TERMINAL_PROMPT=0 git push --dry-run &>/dev/null
  • GIT_TERMINAL_PROMPT=0: fail if you are required to enter credentials manually.

  • git push: try to push changes to origin.

  • --dry-run: simulate the operation, but don't perform it.

  • &>/dev/null: silence any output.

4
user3159253 On

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.

0
Teddy C On

You can use gh (GitHub CLI)'s api subcommand to check your access to a repo (you need to use gh auth login to login to your GitHub account first).

The command is

gh api /repos{/owner}{/repo}/collaborators/USERNAME/permission

This will give you the lots of info.

If you want a command to output either just read or write, run this:

gh api /repos{/owner}{/repo}/collaborators/USERNAME/permission | jq '.permission' -r

2
Jack M On

Literally just push (with no changes). Git needs credentials even just to attempt a push and report that it's unnecessary.

With correct credentials set up:

$ git push
Everything up-to-date

after messing up my ssh config:

$ git push
ERROR: Permission to [some repo] denied to [my user name].
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
1
Pierz On

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_username and your_personal_access_token) e.g.:

curl -u your_username:your_personal_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 can report:

{
"message": "Not Found"
}

Or if you don't 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"
}
5
guyskk On

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

0
awvalenti On

Push a harmless branch to the server:

git clone (...)
git branch -a
# Make sure no branch named "test" exists. If it does, use another name.

git branch test
git push -u origin test
# If the above push worked, you have write access

# Cleaning up
git branch -d test
git push -d origin test # only run this if you do have write access