How to Install and Configure Git with GitHub on Ubuntu 22.04

How to install Git on Ubuntu 22.04

Table of Contents

  1. Introduction
  2. Verifying Git Installation
  3. Installing Git on Ubuntu
  4. Configuring Git
  5. Creating a GitHub Repository
  6. Cloning a Repository
  7. Pushing Changes to GitHub
  8. Using Personal Access Tokens
  9. Installing Git from Source

Introduction

Git is a powerful version control system that allows you to track changes in your code and collaborate with others. In this guide, we will show you how to install Git and configure it with GitHub on your Ubuntu operating system. Follow these steps to set up Git and GitHub for efficient version control and collaboration.

Verifying Git Installation

First, let’s check if Git is already installed on your Ubuntu system. Open your terminal by pressing Ctrl + Alt + T or by searching for the terminal in your applications menu. Once the terminal is open, type the following command:

git --version

If Git is not installed, you will see an output indicating that the command is not found. In that case, proceed to the next section to install Git.

Installing Git on Ubuntu

To install Git, follow these steps:

Update package index:

sudo apt update
  1. This command updates the local package index to ensure you have the latest information about available packages.

Install Git:

sudo apt install git

If you encounter unmet dependencies, you can fix them with:

sudo apt --fix-broken install
  1. After the installation is complete, verify the installation by checking the Git version again:

git --version

Configuring Git

Once Git is installed, you need to configure your username and email address. These configurations are important for identifying the author of commits. Run the following commands:

  1. Set your username:
    git config --global user.name "Your Name"
  2. Set your email:
    git config --global user.email "your.email@example.com"
  3. To verify your configuration, use:
    git config --global --list

Creating a GitHub Repository

To create a repository on GitHub:

  1. Navigate to GitHub and log in to your account.
  2. Click the + icon in the top-right corner and select New repository.
  3. Enter a repository name and description.
  4. Choose to make the repository public or private.
  5. Optionally, add a README file.
  6. Click Create repository.

Cloning a Repository

To clone the repository you just created:

  1. Copy the repository URL from the GitHub page.

Open your terminal and navigate to the directory where you want to clone the repository:

cd /path/to/your/directory
  1. Clone the repository using:

git clone https://github.com/your-username/your-repository.git

Pushing Changes to GitHub

To push changes to GitHub:

  1. Create or modify files in your local repository.

Stage the changes:

git add .
  1. Commit the changes:

git commit -m "Your commit message"
  1. Push the changes:

git push origin main

Using Personal Access Tokens

Since GitHub no longer supports password authentication, you need to use a Personal Access Token (PAT) for authentication:

  1. Go to GitHub settings and select Developer settings.
  2. Click on Personal access tokens and then Generate new token.
  3. Set a note for the token and select the required scopes.
  4. Generate the token and copy it.

When you push changes, use the PAT instead of your password.

git push https://your-username:your-token@github.com/your-username/your-repository.git

Installing Git from Source

If you’re looking for a more flexible method of installing Git, you may want to compile the software from source. This takes longer and will not be maintained through your package manager, but it will allow you to download the latest release and give you greater control over the options you include if you wish to make customizations.

Install dependencies:

sudo apt update

sudo apt install libz-dev libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext cmake gcc
  1. Create a temporary directory and move into it:

mkdir /tmp/git && cd /tmp/git
  1. Download the latest Git source tarball:

curl - o git.tar.gz https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.38.1.tar.gz
  1. Unpack the tarball:

tar -zxf git.tar.gz && cd git-*
  1. Compile and install Git:

make prefix=/usr/local all

sudo make prefix=/usr/local install
  1. Restart the shell:

exec bash
  1. Verify the installation:

git --version

By following these steps, you have successfully installed and configured Git on your Ubuntu system and connected it with GitHub. Whether you used the default package manager or installed from source, you are now ready to efficiently manage your projects and collaborate with others using Git and GitHub.