Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 837
Yang Meyer Avatar asked Apr 02 '14 12:04

Yang Meyer


People also ask

How do I know if I have write access to my GitHub repository?

how can i check write access to a git repository, if i do have a clone of it? A very easy way to check is whether you see an edit 'pencil' icon in the top right of the README.MD on the main Code page of the repo (scroll down to it if there's a long list of top level files/folders).

How do I push to a remote git repository?

In order to push a Git branch to remote, you need to execute the “git push” command and specify the remote as well as the branch name to be pushed. If you are not already on the branch that you want to push, you can execute the “git checkout” command to switch to your branch.

How do I see git repository permissions?

Open Security for a repository You set Git repository permissions from Project Settings>Repositories. Open the web portal and choose the project where you want to add users or groups. To choose another project, see Switch project, repository, team. Open Project settings>Repositories.

How does git know which repository to push to?

git directory that contains metadata about the repository. That's what Git uses to determine where to push your changes.


Video Answer


2 Answers

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

like image 75
guyskk Avatar answered Sep 20 '22 18:09

guyskk


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" } 
like image 36
Pierz Avatar answered Sep 22 '22 18:09

Pierz