Search code examples
gitgitlabgit-remote

Cannot push files to remote repository


I cloned an empty repo for a project I'm invited to, but then cannot push files to the remote (first time push).

git remote -v
origin  https://gitlab.com/project-path (fetch)
origin  https://gitlab.com/project-path (push)

I can confirm I successfully added my ssh public key:

ssh -T [email protected]
Welcome to GilLab, @username!

But unable to push file:

git push README.md
fatal: invalid gitfile format: README.md
fatal: Could not read from remote repository.

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

What am I missing here?


Solution

  • You can't push a file to a remote branch. git push operates on branches.

    Here's an simple workflow.

    # modify the README.md file, or any other file
    
    # add it and make a commit
    git add REAME.md
    git commit -m "update README"
    
    # push the branch to remote, replace "<remote-repo>" with real repo name
    git push <remote-repo> <branch>
    # or
    git push <remote-repo> <local-branch>:<remote-branch>
    # eg
    git push origin master
    

    BTW, you ssh public key won't be used in git push in this situation. Cause the "remote" branch origin is using https protocol, but not git protocol.