Skip to content

Instantly share code, notes, and snippets.

@emha69
Last active November 8, 2021 13:55
Show Gist options
  • Save emha69/1332672a37038ffd40f719c41ea79050 to your computer and use it in GitHub Desktop.
Save emha69/1332672a37038ffd40f719c41ea79050 to your computer and use it in GitHub Desktop.
Local Git Server setup in 10 steps with Ubuntu server and Windows clients
Step 1. Setup Ubuntu 20.04 Linux server
Step 2. Install software on Windows client(s)
Step 3. Create SSH keys on the clients and copy to server
Step 4. Create remote repository
Step 5. Tweak Git to use SSH
Step 6. Create local repository
Step 7. Push it to the remote
Step 8. Clone to the clients
Step 9. Setup GitHub Desktop client
Step 10. Test it
@emha69
Copy link
Author

emha69 commented Oct 7, 2021

Step 1. Basic setup Ubuntu 20.04 server

1. First check if Git is installed
git --version

2. If not, install Git with
sudo apt-get update
sudo apt-get install git

3. Add git user
adduser git

4. Change to git user and create folders
cd /home/git
mkdir ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

Create additional temp folder, which will be used later for file upload.

Step 2. Install software on windows

  1. Download Git for windows
    https://github.com/git-for-windows/git/releases/
    For now install with default options.

Step 3. Generate SSH keys on Windows 10

  1. Press Win+R and open CMD for the current user

  2. Generate private and public SSH keys
    ssh-keygen
    Save the id_rsa in .ssh folder under current user's home directory e.g. C:\users\$user\.ssh
    Enter password to protect the private key.
    The id_rsa and id_rsa.pub keys will be created in the C:\users\$user\.ssh folder

  3. Copy the id_rsa.pub file to the server
    scp id_rsa.pub git@<host>:/home/git/temp/<user>.pub

  4. Log in as git user to the server and add the key to the authorized keys
    ssh git@<host>
    cd temp
    cat <user>.pub >> ../.ssh/authorized_keys

Logout from server and try to log in directly, using: ssh git

Step 4. Create and init remote repository

  1. Log in to server as git user
    ssh git@<host>
  2. Create git repository
    mkdir projects.git
    cd projects.git
    git init --bare
    cd ..
    chown -R git.git projects.git

Step 5. Tweak git on windows to use Windows SSH

  1. Configure Git to use the Windows OpenSSH with private key
    Open Git bash console and enter:
    git config --global core.sshcommand "C:/Windows/System32/OpenSSH/ssh.exe -i C:/users/$user/.ssh/id_rsa

Step 6. Create local repository on Windows PC

  1. On Windows computer, open Git Bash and navigate to your project e.g. c:\Works\project
    If this is an empty project create some dummy files:
    touch file.c
    touch file.h
  2. Initialize local repository with
    git init
  3. Add your files to the repository with
    git add file.*
  4. Commit files
    git commit -m "Initial commit

Step 7. Configure remote server and push to the remote repository

  1. Add remote repository
    git remote add origin ssh://git@<host>:/home/git/project.git
  2. Check the remote
    git remote -v
  3. Push to remote
    git push origin master

Step 8. Clone to other Windows clients

  1. On Windows PC repeat step 3 and step 5
  2. Pull from remote
    git clone git@<host>:projects.git .

Step 9. Setup GitHub Desktop application

Step 10. Test it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment