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.
In this particular article, we’ll see how you can install and use Git on your Ubuntu 22.
How to Install and Use Git on Ubuntu 22?
Git is a version control system used as a distributed database management system for managing small and large projects. Sometimes a single line of code destroys the whole project, to tackle these kinds of problems Git comes into play. You can undo changes by the use of branches feature in Git architecture.
Step 1: Install Git
Open the the terminal by pressing “Ctrl + Alt + t”. Type the following command for installing Git:
$ sudo apt install git
Press “y” to grant disk space permissions, and hit “Enter”.
A Git version control system will be installed, to confirm, and execute the following command
$ git –version
The command will display the git version installed on the system.
Open your web browser and search for GitHub. Click on the GitHub login page:
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”
Enter your email, and click on continue:
Create your password, and click on continue:
Enter your username, and click on continue:
Enter your username, and click on continue:
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”:
Enter the verification code, that is sent by GitHub to your email:
Enter the number of team members you have and click continue:
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:
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:
Provide the name of your repository, and enable the “Add a README file”, option:
Click on “Create Repository”, a repository is created as per your given name. In my case, the repository name is “test123”:
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:
Use the “cd” command to change the directory:
$ cd gitRepo
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:
Now execute the following command for cloning the repository:
$ git clone "your repository link"
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:
Now change the directory, by executing the following command:
$ cd test123
The command will change the directory to test123:
You can see the contents of test123 by executing the command ls:
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:
Now execute the following command to edit the test.py file in the gedit text editor:
$ gedit test.py
Add the desired Python code in the file and save the changes:
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:
Execute the following command to add the file to the repository:
$ git add “file name”
The command will add the file to the repository:
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:
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:
Then select “Developer Settings”:
Click on “Personal access tokens”, and select “Tokens classic”:
Click the button Generate new token, and select the Generate the new token (classic) option:
Enter the purpose of your token, expiry date, and other options, required to generate the token:
Enable other options accordingly:
After enabling the options, click on the “Generate token” button:
The authentication token will be generated, copy the token, and then you can use it for authenticating your push repositories:
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:
Step 5: Push 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:
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:
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.
Conclusion
Git is a version control system, You can install Git by executing the command “sudo apt install git” in the Ubuntu terminal. After installation and making repositories, you can push them to your Git account with the “token” based authentication system. This article has explained, how you can install Git and use Git token to push files to the remote repository.