How to Install Git and Configure Personal Access Tokens on an Ubuntu Server

Git is a powerful version control system widely used for source code management in software development. Setting up Git on an Ubuntu server is a straightforward process, and configuring it with Personal Access Tokens (PAT) enhances security, especially when working with remote repositories. In this article, we will guide you through the steps to install Git and configure it with Personal Access Tokens on your Ubuntu server.

Update Your System

Before installing any software, it’s essential to ensure that your package list is up to date. Open your terminal and run the following command:

sudo apt update




Install Git

Once your system is updated, you can install Git using the following command:

sudo apt install git

To verify that Git was installed correctly, you can check the version:

git --version




Configure Git

After installing Git, you need to configure it with your name and email address. This information will be associated with your commits. Run the following commands, replacing Your Name and [email protected] with your actual name and email:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

You can check your configuration settings using:

git config --list




Generate a Personal Access Token on GitHub

To securely connect to GitHub, you need to generate a Personal Access Token. Follow these steps:

  1. Log in to your GitHub account.
  2. Go to Settings (click on your profile picture in the upper right corner).
  3. In the left sidebar, click on Developer settings.
  4. Select Personal access tokens > Tokens (classic).
  5. Click on Generate new token.
  6. Give your token a descriptive name and set an expiration date.
  7. Select the scopes or permissions you want to grant this token (e.g., repo for access to your repositories).
  8. Click Generate token. Important: Copy the token now, as you won’t be able to see it again.

Configure Git to Use the Personal Access Token

When you clone a repository or push changes to GitHub, Git will ask for your username and password. Here’s how to configure Git to use the Personal Access Token instead of your password:

Clone a repository using HTTPS:

git clone https://github.com/username/repo.git

When prompted for your username, enter your GitHub username.

When prompted for your password, enter the Personal Access Token you generated earlier.

Alternatively, you can cache your credentials so you don’t have to enter the token every time. Use the following command to enable credential caching:

git config --global credential.helper cache

You can set a timeout (in seconds) for how long Git will remember your credentials:

git config --global credential.helper 'cache --timeout=3600'




Verify Your Configuration

To ensure that everything is set up correctly, try pushing a change to your repository or cloning another repository. If everything is configured correctly, Git should authenticate using your Personal Access Token without prompting for your password.

You’ve successfully installed Git and configured it to use Personal Access Tokens on your Ubuntu server. This setup enhances your security by avoiding the use of your GitHub password. With Git installed, you can now efficiently manage your source code and collaborate with others.