How to set up private git server on linux

前端 未结 4 988
轮回少年
轮回少年 2021-01-31 12:30

I have tried following how-set-up-your-own-private-git-server-linux and private-remote-git-repositories-ubuntu-linode but I am still having problems.

My local environmen

4条回答
  •  梦毁少年i
    2021-01-31 13:17

    I'm not sure if this should be here or if it would be best migrated over to a different site, but since I might be able to help I'll go ahead and answer.

    I just skimmed the articles you linked. It looks like they both deal with accessing a git server over ssh, which you mention, so that's what I'll focus on.

    First, on your server:

    You need to set up an account on the server so that you can log in. This can be either a generic git account, or your own personal account. For the moment we'll assume that you are setting it up to work with a personal account. What you want to do is create your account, then somewhere accessible to that account (say, in your home directory), create the git repository.

    mkdir myrepo.git
    cd myrepo.git
    git --bare init --shared=all
    

    So now you have a git repository up on the server. Depending on the git client you are using, you might not need to mess with keys right now. If SSH is configured on your server to allow password login then you can probably just connect and enter your password when you need to interact with the server. If you want to set up keys, what you need to do is to generate an ssh public key. I don't know off hand how to do this in windows, but in linux you'd do something like:

    ssh-keygen -t rsa -b 1024
    

    That command will generate two files, "id_rsa" and "id_rsa.pub"; whatever tool you use should also generate two files, a public key and a private key. They might have different names, but assume for now that "id_rsa.pub" is the name of your public key file.

    You want to copy the public key to the server, you can use scp, ftp, or just move it over using a thumbdrive. Either way, once you get it onto the server, and it's accessible by your user, log in as your user on the server. You want to add the public key to your "authorized_hosts" file, so after logging in to your account on the server, do this:

    cd
    mkdir .ssh
    cat id_rsa.pub >> .ssh/authorized_hosts
    rm id_rsa.pub
    

    Now, from your workstation you need to configure your ssh client to use the private key your generated. Again, I don't know how to do this on Windows and it will probably vary depending on what ssh client you are using, so you'll need to get that information somewhere else.

    Next, you need to create your local repository, add some files, and make a commit. Note that you can't clone the remote repository you made yet because there's nothing in there. Once you have some commits made locally, you need to set the remote server in your repository.

    If you are using the git command line tools, you can run:

    git remote add origin user@yourserver:myrepo.git
    

    If you put the repository somewhere other than your home directory, use the full path:

    git remote add origin user@yourserver:/path/to/repo.git
    

    Note that you need the ".git" in there, since your directory name has ".git" as part of the name.

    If you are using a GUI tool, then you instead just edit the configuration file for the repository. This will be in the top level of your repository under ".git/config". You'll want to add something like

    [remote "origin"]
        url = user@yourserver:/path/to/repo.git
        fetch = +refs/heads/*:refs/remotes/origin/*
    

    Now that your remote is configured, and you have some commits locally, you can push your master branch up to the server. If you're using the command line use:

    git push origin master
    

    Or, if you're working on a different branch:

    git push origin mybranch
    

    If you are using a GUI frontend for get then you'll need to look up the documentation for that tool on how to push.

提交回复
热议问题