Sign In
Sign In

How to Install and Configure Git on Ubuntu 22.04

How to Install and Configure Git on Ubuntu 22.04
Adnene Mabrouk
Technical writer
Git Ubuntu
22.07.2024
Reading time: 3 min

Git is a widely-used version control system that allows developers to track changes in their code and collaborate with others. Whether you are working on a personal project or contributing to open-source repositories, Git is an essential tool in your development toolkit. This guide will walk you through the process of installing and configuring Git on Ubuntu 22.04, ensuring you are ready to manage your code efficiently.

Prerequisites

Before you begin, ensure you have the following:

  • A system, such as a cloud server, running Ubuntu 22.04.

  • A user account with sudo privileges.

  • Basic knowledge of the Linux command line.

Installing Git

Update the Package List

First, update the package list to ensure you have the latest information about available packages and their dependencies. Open your terminal and run:

sudo apt update

Install Git

To install Git, execute the following command:

sudo apt install git

This command downloads and installs Git from the Ubuntu repositories.

Verify Git Installation

After the installation is complete, verify that Git is installed correctly by checking its version with git command:

git --version

You should see output similar to this:

Image1

Basic Git Configuration

Configure Git with your personal information

Configuring Git with your personal information is crucial for keeping track of changes and collaborating with others. Set your global username and email address with these commands:

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

You can view your configuration settings with:

git config --list

Image2

Generating SSH Keys for GitHub/GitLab

To securely connect to GitHub or GitLab, generate an SSH key pair:

ssh-keygen -t ed25519 -C "[email protected]"

Follow the prompts to save the key to the default location (~/.ssh/id_ed25519) and create a passphrase for added security.

Add your SSH key to the SSH agent:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Next, copy your SSH key to the clipboard:

cat ~/.ssh/id_ed25519.pub

Add this key to your GitHub or GitLab account by navigating to the SSH keys section in your account settings.

Testing Your Git Configuration

Clone a repository to test your configuration:

git clone [email protected]:your-username/your-repository.git

Replace your-username and your-repository with your GitHub username and repository name, respectively.

Common Git Commands

Here are some basic Git commands to get you started.

  • Initialize a new Git repository:

git init
  • Add files to the staging area:

git add <file>
  • Commit changes:

git commit -m "Commit message"
  • Push changes to a remote repository:

git push origin main
  • Pull changes from a remote repository:

git pull origin main

Troubleshooting Git Installation and Configuration Issues

If you encounter issues, consider these troubleshooting tips:

  • Ensure your system is updated by running sudo apt update && sudo apt upgrade.

  • Reinstall Git if necessary: sudo apt remove git followed by sudo apt install git.

  • Check for correct SSH key configuration if you face connectivity issues with GitHub/GitLab.

Conclusion

Installing and configuring Git on Ubuntu 22.04 is a straightforward process that equips you with a powerful tool for version control. By following this guide, you have set up Git, configured it with your personal information, generated SSH keys for secure access, and tested your setup. With Git, you can efficiently manage your projects and collaborate with others seamlessly. Happy coding!

Git Ubuntu
22.07.2024
Reading time: 3 min

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start
Email us