blog-image

Jun 06, 2024

9 min read

How To Install and Use Git on Debian 11

Written by

Abdelhadi Dyouri

Introduction

Version control is a system that allows you to track changes to your files and revert to previous versions if needed. Git is a type of version control system that is popular for software development. It allows you to keep track of changes to your files and code, as well as collaborate with other developers. When you make changes to a file, Git stores a copy of the old version and the new version, allowing you to revert back to the old version if needed. Git also allows you to create branches of your code, which can be used for experimentation or to work on different features separately. When you are done with a branch, you can merge it back into the main codebase.

In this tutorial, you'll learn how to install Git on your Debian server, how to use it, and how to connect to Github.

Updating the Package Cache

Start by updating the packages in the package manager cache to the latest available versions using the following command:

sudo apt update

Installing Git Using the Default Repository

To quickly install a working stable version of Git, and are not particularly interested in the new functionality in the latest release, it is best to install Git through the default Debian repositories.

To install Git on your Debian server, use the following command:

sudo apt install git

If Git is already installed on your system, you may receive the following output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
git is already the newest version (1:2.30.2-1+deb11u1).

To confirm that Git is available on your system, use the following command to check its version:

git --version

You should receive an output similar to the following:

git version 2.30.2

With this you can now configure your Git installation by setting up a default username and a default email so that your commit messages include the correct information by default.

You can set a default username for your Git commits using the following command. Replacing Your Name with your full name or your preferred username:

git config --global user.name "Your Name"

To set a default email, use the following command. Replacing [email protected] with your email:

git config --global user.email "[email protected]"

To list your Git configuration, use the following command:

git config --list

To edit your configuration, you can modify the file in which your Git settings are stored. This file is called .gitconfig, and is located inside your home folder.

To edit your Git configuration file, run the following command:

nano ~/.gitconfig

The contents of this file should look like so:

[user]
        name = Your Name
        email = [email protected]

Once you modify the information in this file, press CTRL+X, then Y and ENTER to save and close it.

Using Git

Now that you have Git installed on your system, you can now use it to track and manage modifications to your files and folders.

Creating a Git Repository

The first step in using Git, is to initiate a directory and make it a Git repository, to do this, create an empty folder in your home directory to be used as a demo.

Create a folder named demo_repo or any other name you'd like:

mkdir demo_repo

Navigate to this new folder:

cd demo_repo

To initiate an empty git repository, use the following command:

git init

This marks demo_repo as a Git repository, and you can now track the changes of files you add to it.

Adding Files to Your Git Repository

To track the changes of a file, you need to add it to your repository. To demonstrate this, create a file called README.md inside your demo_repo like so:

nano README.md

Add some contents to it:

Hello World!
This is my Git repo.

After you create this new file, Git still won't track it, because you need to add the files in the demo_repo to the Git repository in order to track their changes. To do this, use the following command:

git add .

This adds all the files in the demo_repo folder to the Git repository.

If you want to add only a specific file, use its name like so:

git add README.md

Listing the staged files

To see a list of files that were staged, you can use the following command:

git status

This lists all new or modified files to be committed:

On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
    new file:   README.md

Commiting Changes

Now that you've added files to your Git repository with git add. You can now commit the changes you made with a message attached using the following command:

git commit -m "First commit"

The commit message should briefly describe the changes you've made since the last commit.

Connecting to Github

Your demo_repo is a "local" repository, which means that it exists only on your machine. To collaborate with others, you need to publish your repository on Github.

First, create a Github account if you haven't already.

Go to the Create a New Repository page on Github, and create a repository called demo_repo.

This Github repository is referred to as a "remote" repository, in contrast to your "local" repository.

Now, connect your local repository to your remote Github repository using the following command, make sure you're inside your local demo_repo directory before you run this command:

git remote add origin remote_repository_URL

Change remote_repository_URL to the URL that corresponds with your remote Github repository.

Once connected, you can now push the files in your local repository to your remote repository with the following commands:

git branch -M main

git push -u origin main

You'll be asked for your Github username, enter it then hit ENTER. You will then get a prompt for a password, this does not accept your Github password, instead, enter your personal access token, which you can get by following the instructions on this page.

Once you push your files, you should see the contents of your README.md file on your remote repository's Github page.

With your local repository connected to your remote Github repository, you can now modify files in your local repository, commit the changes when you're ready, and then push them to the main branch using the git push -u origin main command.

Congrats!

With this, you now have Git on your Debian server. You've learned how to install Git using both the default repository version and using the source code to compile it from scratch. You've also learned about Git basics, and how to connect your local repositories to Github. To learn more about Git, check out the Git Documentation.

A note about tutorials: We encourage our users to try out tutorials, but they aren't fully supported by our team—we can't always provide support when things go wrong. Be sure to check which OS and version it was tested with before you proceed.

If you want a fully managed experience, with dedicated support for any application you might want to run, contact us for more information.

Leave a Reply