Git is an open-source and popular version control system, that provides the facility to create, share, and control the repositories remotely. It has a distributed version architecture, which helps to manage small and large projects efficiently. It has the support of over 100 million developers. Git is used to keep track of changes made in any project’s directory. The team of more than one developer can easily see the changes made by other developers. Ubuntu 22 is the latest Linux distribution edition, which is being used widely. Git helps in many projects where you want to keep track of changes. Git is a cross-platform software that can be installed on Ubuntu 22.

Quick Answer

Install Git on Ubuntu 22 with sudo apt install git, configure with your username and email using git config --global, then clone repositories and push files using token-based authentication.

How to Install and Use Git on Ubuntu 22?

Step 1: Install Git

Open the terminal by pressing “Ctrl + Alt + t“. Type the following command for installing Git:

$ sudo apt install git
Terminal output of sudo apt install git on Ubuntu 22

Press “y” to grant disk space permissions, and hit “Enter“.

Terminal prompt asking to confirm disk space for git installation on Ubuntu 22

A Git version control system will be installed, to confirm, execute the following command:

$ git --version

The command will display the git version installed on the system.

Terminal output of git --version showing installed Git version on Ubuntu 22

Open your web browser and search for GitHub. Click on the GitHub login page:

GitHub login page in web browser

Sign in, if you’ve already a GitHub account, by providing your username and password. But if you do not have an account, proceed with “Create an account

GitHub sign in form with username and password fields

Enter your email, and click on continue:

GitHub account creation entering email address

Create your password, and click on continue:

GitHub account creation entering password

Enter your username, and click on continue:

GitHub account creation entering username

Press “y“, if you want to receive product updates and announcements via email. In my case I don’t want to receive any emails from GitHub, so I press “n“:

GitHub account creation opting out of product update emails

Enter the verification code, that is sent by GitHub to your email:

GitHub account creation entering email verification code

Enter the number of team members you have and click continue:

GitHub account creation selecting number of team members

Step 2: Configure Git

Configure the git with your git username, and email, with global permissions. Type the following command and press “Enter“:

$ git config --global user.name "your git username"

Now provide your git account email address by using the following command:

$ git config --global user.email "your git email"

You can see your configurations by typing the following command:

$ git config --list

You can see your username and email after the execution of the command:

Terminal output of git config --list showing username and email on Ubuntu 22

Step 3: Using Git

Login to your Git account, and you’ll be on the homepage of the GitHub. Click on the “+” button, select the “New Repository” from the dropdown menu:

GitHub dashboard showing plus button with New Repository dropdown

Provide the name of your repository, and enable the “Add a README file“, option:

GitHub new repository form with name and Add README file option

Click on “Create Repository“, a repository is created as per your given name. In my case, the repository name is “test123“:

GitHub showing newly created test123 repository

Now go to the terminal of your Ubuntu Desktop and create a repository, in which we clone the “Github repository”, then we can manage our changes remotely, using our terminal. Use the “mkdir” command to make a repository:

$ mkdir gitRepo

The command will make a directory gitRepo:

Terminal output of mkdir gitRepo creating directory on Ubuntu 22

Use the “cd” command to change the directory:

$ cd gitRepo
Terminal output of cd gitRepo changing to directory on Ubuntu 22

Now go to the GitHub page, and click on your repository. Then click on the code button. From the local option copy the git clone link of your repository:

GitHub repository page showing HTTPS clone URL

Now execute the following command for cloning the repository:

$ git clone "your repository link"
Terminal output of git clone cloning GitHub repository on Ubuntu 22

Execute the “ls” command, if you want to verify the cloning process:

$ ls

You can see that test123 has been added to the gitRepo repository:

Terminal output of ls showing cloned test123 directory on Ubuntu 22

Now change the directory, by executing the following command:

$ cd test123

The command will change the directory to test123:

Terminal output of cd test123 changing into cloned repository on Ubuntu 22

You can see the contents of test123 by executing the command ls:

Terminal output of ls showing README.md in test123 repository on Ubuntu 22

Now, create a file by executing the command:

$ touch test.py

The command will create a Python file, execute the ls command and you’ll see that a new file has been included in the repository:

Terminal output of touch test.py and ls showing new file in repository on Ubuntu 22

Now execute the following command to edit the test.py file in the gedit text editor:

$ gedit test.py
Terminal showing gedit test.py command opening the file on Ubuntu 22

Add the desired Python code in the file and save the changes:

gedit editor showing Python code added to test.py on Ubuntu 22

Execute the command git status to see the files that can be added to the Git repository:

$ git status

You can see that test.py appears to be red, which means it has not yet been added to the repository:

Terminal output of git status showing test.py in red as untracked on Ubuntu 22

Execute the following command to add the file to the repository:

$ git add "file name"

The command will add the file to the repository:

Terminal output of git add adding test.py to staging area on Ubuntu 22

Now execute the command to check the status of the file, whether it is added or not:

$ git status

The name of the file appears to be green, which means that our file has been added to the repository:

Terminal output of git status showing test.py in green as staged on Ubuntu 22

The file is ready to push, but the old authentication method is deprecated, so we have to follow token authentication to push the file to the repository. The token-based authentication is used due to security reasons. In this method, you can generate a token from your GitHub account and use it for authentication in the terminal of your desktop.

Step 4: Token Based Authentication

Open your GitHub account and click on your profile, then select “Settings” from the dropdown menu:

GitHub profile dropdown menu showing Settings option

Then select “Developer Settings“:

GitHub Settings sidebar showing Developer Settings option

Click on “Personal access tokens“, and select “Tokens classic“:

GitHub Developer Settings showing Personal access tokens menu

Click the button Generate new token, and select the Generate the new token (classic) option:

GitHub Personal access tokens showing Generate new token classic option

Enter the purpose of your token, expiry date, and other options, required to generate the token:

GitHub new token form showing note and expiration fields

Enable other options accordingly:

GitHub new token form showing repository permissions checkboxes

After enabling the options, click on the “Generate token” button:

GitHub new token form with Generate token button at bottom

The authentication token will be generated, copy the token, and then you can use it for authenticating your push repositories:

GitHub showing newly generated personal access token

Now go to your terminal, and type the following command for authentication:

$ git remote set-url origin https://"your_token"@github.com/<your_username><your_git_repository>

The command will authenticate your credentials, and now you can push the files to the origin:

Terminal output of git remote set-url with token authentication on Ubuntu 22

Step 5: Push the File

Now type the following command to push the file to the main branch:

$ git push origin main

The command will push the file to the main branch of the directory:

Terminal output of git push origin main pushing file to GitHub on Ubuntu 22

Now go to the GitHub repository page to verify whether the file has been added or not. You can see that the file has been added:

GitHub repository page showing test.py file successfully pushed

You can add many files by following the same method, without any error. That’s how you can install and use Git on Ubuntu 22 with the new “token” based authentication.

Related Linux guides