Search code examples
gitgit-push

Failed to pushing files into newly created git repo


Maybe a duplicate question, though I have tried to found out answers from existing questions but failed.

I created a git repo on the server with command:

mkdir gitrepo
cd gitrepo
git init

Then from another machine I tried to push files to this repo but failed.

git init
git clone user@server:~/gitrepo/.git
cd gitrepo
touch test
git add test
git commit -a

Util now, no error occurs. When I try to push the changes to the server, the following error occurs:

>git push
No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to 'user@server:~/gitrepo/.git'

Anyone encountered this problem before?

I found an blog which explains well the difference between non-bare and bare repo. http://www.bitflop.com/document/111 Those who gets the same problem may refer to this.


Solution

  • On the first machine, you created a non-bare repository, or a repository that has a working copy. Pushing to a non-bare repo can get a little exciting, so make sure that's actually what you want.

    On the second machine, you created a new repository in the current directory (git init), then cloned gitrepo into a sub-directory as a second repo. Again, that's fine, just make sure it's what you wanted.

    Here's how it would work with a bare repo on the first machine and one repo on the second:

    First machine:

    git init --bare gitrepo.git
    

    Second machine:

    git clone user@server:~/gitrepo.git
    cd gitrepo
    touch test
    git add test
    git commit -a
    
    # '-u' tells git to track the remote master branch with your local master branch
    git push -u origin master