Home Git The Linux user’s guide to installing and setting up Git

The Linux user’s guide to installing and setting up Git

Setting up Git on Linux doesn't have to be complicated. Our comprehensive guide walks you through installation and configuration, making Git ready for your coding adventures.

by Arun Kumar
install git linux

Welcome to the world of Git on Linux! Whether you’re a seasoned developer or just starting out, mastering Git is a crucial step in your coding journey. As a fellow Linux user, I understand the unique quirks and strengths of different Linux distributions.

That’s why I have tailored this guide to help you seamlessly integrate Git into your Linux setup. Whether you’re using Ubuntu, Fedora, CentOS, or Arch Linux, this guide will assist you in installing Git and configuring it to fit your personal workflow. We will also address common challenges that you may encounter along the way.

So, let’s get started! Open up your terminal, and let’s embark on this exciting journey to unlock the full potential of Git in your Linux environment. Get ready to elevate your coding game with the power of efficient version control!

Section 1: Installing Git – The first step to coding mastery

Getting Git on your Linux machine

The installation process is simple. Open your terminal and execute the following commands:

On Ubuntu and Debian-based Linux distros, use the following commands:

sudo apt-get update
sudo apt-get install git

On Fedora:

 sudo dnf install git

On CentOS/RedHat:

sudo yum install git

On Arch Linux:

sudo pacman -S git

After installation, confirm with git --version. It’s amazing how a few lines of command can set up such a powerful tool!

Sample output on Ubuntu:

Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following additional packages will be installed:
git-man liberror-perl
Suggested packages:
git-daemon-run | git-daemon-sysvinit git-doc git-el git-email git-gui gitk gitweb git-cvs git-mediawiki git-svn
The following NEW packages will be installed:
git git-man liberror-perl
0 upgraded, 3 newly installed, 0 to remove and 19 not upgraded.
Need to get 3,877 kB of archives.
After this operation, 28.9 MB of additional disk space will be used.
...
Setting up git (1:2.25.1-1ubuntu3) ...

Section 2: Creating SSH Keys – securing your connections

Crafting your secure identity

Generate an SSH key for secure repository interactions:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Accept the default file location and passphrase. Your SSH key is now ready!Output:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/fosslinux/.ssh/id_rsa): 
Created directory '/home/fosslinux/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/fosslinux/.ssh/id_rsa.
Your public key has been saved in /home/fosslinux/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx your_email@example.com
The key's randomart image is:
+---[RSA 4096]----+
| .o. |
| . o . |
| . . o = |
| o + = + |
| .S+ o o . |
| ..o+.o . |
| .Eo+.. |
| ooo. |
| ..o. |
+----[SHA256]-----+

Section 3: Checking your connection – A quick sanity check

Verifying your setup

Ensure your SSH connection works by testing it with:

ssh -T git@github.com
A confirmation message will appear, indicating a successful setup.Output:
The authenticity of host 'github.com (IP_ADDRESS)' can't be established.
RSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,IP_ADDRESS' (RSA) to the list of known hosts.
Hi username! You've successfully authenticated, but GitHub does not provide shell access.

Section 4: Setting up your Git profile – Personalizing your experience

Making Git yours

Configure your Git identity:

git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"

This step is essential for making your contributions recognizable.

Section 5: Repository configuration – Laying the foundation

Preparing for your projects

To start your project’s version control, navigate to your project directory and run:

cd /path/to/your/project
git init

This initializes a new Git repository in your project directory.

Section 6: The easiest path to creating a local repository – Cloning made simple

Cloning: Your shortcut to repository setup

To quickly set up a local repository by cloning a remote one, use:

git clone https://github.com/fosslinux/repository.git

Replace the URL with that of the remote repository you wish to clone. This creates a local copy of the remote repository, complete with all its history and branches.

Output:

Cloning into 'repository'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 10 (delta 1), reused 10 (delta 1), pack-reused 0
Unpacking objects: 100% (10/10), 1.01 KiB | 1.01 MiB/s, done.

Why I advocate for cloning

Cloning is a time-saver. It’s perfect for jumping straight into existing projects without the hassle of setting up everything from scratch. I often use cloning to contribute to open-source projects or to work on team projects.

Section 7: Linux GUI Client – A touch of convenience

Embracing a graphical approach

While the command line is powerful, GUI clients like GitKraken and Sourcetree (for Windows and Mac only) offer a user-friendly alternative.

gitkraken user interface

Gitkraken user interface

Common troubleshooting: Overcoming Git hurdles on Linux

Encountering issues is a normal part of working with any new software. Here are some common Git problems and their solutions to help you navigate through them smoothly.

Issue 1: Git Command not found

Problem: After installing Git, you get a ‘command not found’ error when trying to use it.

Solution: This usually means Git isn’t in your PATH. Try reopening your terminal or logging out and logging back in. If that doesn’t work, manually add Git to your PATH or reinstall it.

Issue 2: Permission denied (publickey)

Problem: When trying to clone or push to a repository, you get a ‘Permission denied (publickey)’ error.

Solution: Ensure your SSH key is added to your Git account. Use ssh-add ~/.ssh/id_rsa to add your SSH key to the SSH agent. If you’re still facing issues, double-check the SSH key you uploaded to your Git account.

Issue 3: Merge conflicts

Problem: Encountering a merge conflict when trying to merge branches.

Solution: Merge conflicts need to be resolved manually. Open the conflicting files, make the necessary changes to reconcile the differences, and then commit the resolved files. Tools like Git GUI or command line text editors can be helpful.

Issue 4: Slow performance on large repositories

Problem: Git operations are slow in large repositories.

Solution: Consider enabling shallow cloning with git clone --depth 1. This clones the repository with a history truncated to the specified number of commits.

Issue 5: Problems with global configurations

Problem: Incorrect user information or editor configurations.

Solution: Use git config --global --edit to open the global configuration file and correct any mistakes. Ensure your name and email are correctly set up.

Conclusion

This guide has equipped you with the essentials to confidently integrate Git into your Linux experience, whether it’s navigating through the straightforward installation process on Ubuntu, Fedora, CentOS, or Arch Linux, setting up your Git profile, or troubleshooting common challenges. By understanding different installation methods, configuring your Git environment, and learning to overcome typical hurdles, you’re now ready to harness the full potential of Git for your development projects.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.