Search code examples
gitgithububuntu-20.04git-pushgit-pull

git push and pull remote only work with sudo mode


When i use git pull it shows error :

fatal: Could not read from remote repository.

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

but when i use sudo git pull it show success :

Already up to date.

This problem only happens in private repo, when i change the visibility to public, i can pull with git pull well. How to make git work with git pull and not need to add sudo.

/home/username/.ssh git pull

/root/.ssh

enter image description here

git remote -v enter image description here

i copy id_rsa.pub to github


Solution

  • CASE 1

    Check the owner of the .git folder. It sounds like you first ran sudo git clone ....

    You can change the owner by doing:

    sudo chown -R $me:$me .git
    

    Where $me is your user name and group name.

    CASE 2

    In case your troubles come from SSH, make sure your local user has the correct key files populated under ~/.ssh. If it does not, you may have to take them back from /root/.ssh.

    NOTE: when managing multiple GIT repositories from multiple servers (and thus using multiple keys) you can ease this by creating a local configuration file which explicit which key is to be used for each servers.

    For instance ~/.ssh/config:

    host your.server.com
         user git
         hostname you.server.com
         identityfile ~/.ssh/id_rsa
    

    Note that it is a general format, you may have to replace the server you.server.com and the user name git to whatever is expected by your remote server.

    EDIT:

    According to the further details you provided, here is the ~/.ssh/config file's content:

    host github.com
         user git
         hostname github.com
         identityfile ~/.ssh/id_rsa
    

    Ensure the ~/.ssh/config file access is -rw-rw-r-- to your local user.

    NOTE: In any case, you must ensure that your local SSH key is known to the server. In order to avoid confusion, (re) upload the SSH key from the local user to the remote.