How to Set Up GitHub in a Local Environment on Ubuntu

Step-by-step instructions for configuring Git, adding SSH keys, and linking GitHub on Ubuntu.

How to Set Up GitHub in a Local Environment on Ubuntu

Step 1: Install Git

Git is an essential tool for version control, allowing you to track changes to your code, collaborate with others, and maintain a history of your projects. On Ubuntu, installing Git is straightforward:

  1. Update Your System:

    • Before installing new software, it’s always a good idea to update your package list to get the latest version of the repository listings.
    sudo apt update
  1. Install Git:

    • The following command will download and install Git.
    sudo apt install git -y
  1. Verify Installation:

    • Confirm that Git was installed correctly by checking the version:
    git --version
  • This command should return the current Git version.

Step 2: Configure Git

After installing Git, set your user information. This information is tied to each commit, letting collaborators know who authored specific changes.

  1. Set Your Git Username:

     git config --global user.name "Your Name"
    
  2. Set Your Git Email:

     git config --global user.email "your.email@example.com"
    
    • GitHub uses this email to associate your commits with your GitHub account, so make sure it matches the email on your GitHub profile if you want to link them.
  3. View Your Git Configuration:

     git config --list
    
    • This will display your Git settings and confirm they’re saved.

Step 3: Generate SSH Keys

SSH (Secure Shell) keys allow you to securely connect to GitHub without needing to enter your password every time. GitHub uses SSH keys to authenticate your identity.

  1. Generate a New SSH Key:

    • Run the following command, replacing the email with the one you use for GitHub:
    ssh-keygen -t ed25519 -C "your.email@example.com"
  • When prompted, press Enter to save the key in the default location and add a passphrase for extra security if desired.

What is SSH? SSH is a protocol that provides a secure way of accessing remote systems over a network. Using an SSH key pair (a private and a public key), you can securely connect your local machine with GitHub.


Step 4: Add SSH Key to the SSH Agent

The SSH agent manages your keys and helps you avoid re-entering your passphrase every time you use Git.

  1. Start the SSH Agent:

     eval "$(ssh-agent -s)"
    
  2. Add Your Key to the Agent:

     ssh-add ~/.ssh/id_ed25519
    

This command ensures your SSH key is loaded and ready for GitHub to use.


Step 5: Add Your SSH Key to GitHub

Now, you need to register your SSH key with GitHub to grant your machine access.

  1. Copy Your Public SSH Key:

     cat ~/.ssh/id_ed25519.pub
    
    • Copy the output (your public key) to your clipboard.
  2. Add the Key on GitHub:

    • Go to GitHub and sign in.

    • In the upper-right corner, click on your profile picture, and select Settings.

    • Go to SSH and GPG keys on the left sidebar and click New SSH key.

    • Paste your SSH key into the "Key" field, give it a recognizable name (e.g., “Ubuntu-24-laptop”), and click Add SSH key.


Step 6: Test Your SSH Connection

Once your key is added, test the SSH connection between your machine and GitHub.

  1. Run the following command to test:

     ssh -T git@github.com
    

    If successful, you should see a message like:

    Hi yourusername! You've successfully authenticated, but GitHub does not provide shell access.

This message confirms your SSH key is working with GitHub.

Conclusion

Setting up GitHub on your local Ubuntu environment allows for easy code management, secure connections, and efficient collaboration. By installing Git, setting up SSH keys, and connecting your machine to GitHub, you can clone repositories, contribute to projects, and manage version control directly from your terminal. This setup lets you focus on coding while Git and GitHub take care of your project history and collaboration needs. Happy coding!