Using Git on Ubuntu Server

How to use git on Ubuntu Server?

How to use git on Ubuntu Server

You can use git to clone repositores e.g. from Github to the server.

  1. Generate ed25519 key on the server
    ssh-keygen -t ed25519 -C "user / device info"
    Rename generated ssh keys to "githubkey"
    mv ~/.ssh/id_ed25519 ~/.ssh/githubkey
    mv ~/.ssh/id_ed25519.pub ~/.ssh/githubkey.pub
    Start SSH agent on background
    eval "$(ssh-agent -s)"
    Add githubkey to SSH agent
    ssh-add ~/.ssh/githubkey
  2. Go to your Github.com account settings. Within section "Access", select "SSH and GPT keys". Add new SSH key of type "authentication" with a content get from following command processed on teh ubuntu server:
    cat ~/.ssh/githubkey.pub
  3. Create a custom configuration file for SSH on the Ubuntu Server
    nano ~/.ssh/config
    Fill the file with following configuration for github.com (or alternative services)
    Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/githubkey
  4. Check authentication to Git from Ubuntu Server
    ssh -T git@github.com

    You should get following response

    Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.
  5. Download a git repository through its SSH address
    git clone git@github.com:user/repository.git /path/to/clone

Connect to certain Git origin and push first commit

  1. Initialize git repository in PC
    git init
  2. Create README.md file and add it to teh repository
    echo "# Repository" >> README.md
    git add README.md
    git commit -m "first commit"
  3. Set branch to main
    git branch -M main
  4. Set origin
    git remote add origin <sshurl>.git
  5. Push first commit
    git push -u origin main