49

I have created a Git repository on my Desktop machine (Windows 7) with:

git init
git add <all my files>
git commit -m "added my files"

Now I have installed a new Ubuntu Server 10.10 on a machine on my LAN and installed OpenSSH. My home directory is /home/jonas and I created a directory ~/code/ to contain my projects. I can log in to the Ubuntu Server from Windows 7 with Putty.

I installed Git on the server with sudo apt-get install git

Adding a remote repository

Now I want to add my Git repository on my Desktop to the Server. I tried to follow the instructions from Pragmatic Version Control Using Git.

From my Desktop I run these commands:

git remote add origin jonas@192.168.1.10/home/jonas/code/myproject.git
git push origin master

But I got this error message:

fatal: 'jonas@192.168.1.180/home/jonas/code/myproject.git' does not appear to be
 a git repository
fatal: The remote end hung up unexpectedly

What is the problem? How do I create the remote repository?


As PerfectlyNormal suggested, I added a : in the address. Now it worked better, and I had to type my password to the server, but then I got a similar error message:

fatal: '/home/jonas/code/myproject.git' does not appear to be a git repository
fatal: The remote end hung up unexpectedly

Do I have to initialize a Git repository on the server before I can git push to it?

Jonas
  • 121,568
  • 97
  • 310
  • 388

7 Answers7

48
git remote add origin jonas@192.168.1.10/home/jonas/code/myproject.git

When using SSH, remote repository addresses can be expressed in two ways. One using absolute paths and one using relative paths from the users home directory. You've mixed them up.

The corrected command would be one of the following.

git remote add origin jonas@192.168.1.10:code/myproject.git
git remote add origin ssh://jonas@192.168.1.10/home/jonas/code/myproject.git
Arrowmaster
  • 9,143
  • 2
  • 28
  • 25
32

Did you setup the repository on the remote server? You need to run

mkdir -p /home/jonas/code/myproject.git
cd /home/jonas/code/myproject.git
git init --bare

on the server in order to set it up. I recommend taking a look at how to setup a git server in the free ProGit book.

Daniel Vérité
  • 58,074
  • 15
  • 129
  • 156
Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
  • Thanks, this seem to work. But how can I checkout the project on the server? If I do `git status` then I get a message that I must be on a "work tree" – Jonas Feb 27 '11 at 22:28
  • 3
    You can't. A remote repository must be a bare repository, which cannot be used for working on. See [this answer](http://stackoverflow.com/questions/738154/what-does-git-updating-currently-checked-out-branch-warning-mean/738256#738256) for more information. If you want to be able to work on the repo on the server as well, you need to checkout the repository in another directory on the server as well and use it just as if you were on a different computer (i.e. you must pull and push as usual). – Andrew Marshall Feb 27 '11 at 22:34
  • 2
    @andrew-marshall Is that true? Couldn't he use a non-bare repo on the server and then push to `working-dir/.git`? Clearly, this is not a typical setup but isn't it possible? – Tilman Vogel Feb 28 '11 at 00:05
  • 2
    It's _possible_ but will lead to unexpected results if you push to a branch on a remote repository that is checked-out on the remote machine. In fact, I don't think recent versions of git (1.7+ perhaps?) will even allow you to do so. – Andrew Marshall Feb 28 '11 at 00:10
  • Pushing to the checked out branch in a non-bare repo is disabled by default. You can change the setting in every non-bare repo you create but its the default for a very good reason and unless you know exactly what you are doing you likely shouldn't even be attempting it. – Arrowmaster Feb 28 '11 at 00:11
8

First thing I notice is that you're missing a ':'. Should be git remote add origin jonas@192.168.1.10:/home/jonas/code/myproject.git

PerfectlyNormal
  • 4,182
  • 2
  • 35
  • 47
3

I normally create a bare repository locally and then scp that repository to the server when I'm setting up a remote repository.

For example,

cd c:\gits
git clone --bare c:\path\to\local\repository\some_project

which creates some_project.git.

Then,

scp -r some_project.git login@some.server:/path/to/remote/gits/.

enter your password or maybe you already have public/private key access working.

sshine
  • 15,635
  • 1
  • 41
  • 66
Keith John Hutchison
  • 4,955
  • 11
  • 46
  • 64
  • This creates a new repository on a remote server, without being 'there'. Works fine for me. – fcm Jun 01 '16 at 13:41
2

you need a colon:

git remote add origin jonas@192.168.1.10/home/jonas/code/myproject.git

should be:

git remote add origin jonas@192.168.1.10:/home/jonas/code/myproject.git
Dan D.
  • 73,243
  • 15
  • 104
  • 123
0

Have a look at the handy script git-create.bash by Eike Kettner . Give it your preferred remote address (jonas@192.168.1.10:code/myproject.git), and it will automatically SSH in to create the directory and initialize an empty --bare repository for you. All you need to do is add the git remote and git push.

git-create.bash: Create new empty remote git repository via ssh

Usage:

git-create.bash 'jonas@192.168.1.10:code/myproject.git'
git remote add origin 'jonas@192.168.1.10:code/myproject.git'
git push -u origin master
Joel Purra
  • 24,294
  • 8
  • 60
  • 60
0

Connecting your Local Repo. to Git Remote Server Ubuntu

Create a User in remote server and assign permission for ssh access using ssh-keygen in your local server and paste, its .pub file to ssh of the remote server.

Things to do on remote Server

Server : XX.XXX.1XX.XX

Inside Putty command line

  1. Go to location : cd ~/srv/git srv in your root directory

  2. Make a folder : mkdir your_file_name followed by cd inside your file inside your git folder

  3. initialize git there : git init --bare

       Done
    

Things to do On Local Machine

  1. create your repository ..

  2. initializing Git: git init

  3. Create the remote connection : git remote add origin git@xx.xxx.xxx.xxx:/srv/git/your_file_name

  4. git push.

        Done
    

If you got stuck any point refer :https://git-scm.com/book/en/v2/Git-on-the-Server-Setting-Up-the-Server.