How to configure a Git repository using Linux CLI?

Overview

This tutorial provides a general overview of how to set up a Git repository using the Linux command line. It introduces the essential Git operations and guides you through the process of creating a new or ongoing Git repository. This tutorial assumes that you have some familiarity with the command line

Prerequisites

  • Already created GitHub account.

  • Ubuntu 20.04

  • SSH connected text editor

  • Internet connection

Get Started

Step 1: Git Installation

Git requires several dependencies to build on Linux, which can be fulfilled via apt, so we’ll use the same to install the latest version of git.

Enter into root user.

 $ sudo -i

Update your system with the following command:

 $ sudo apt-get update 

Download and install git, using the following command:

$ sudo apt-get install git 

Verify the installation by running the following command:

$ sudo --version 

Step 2: Git Proxy Configuration

Use the following commands to set up your Git login and email. Be sure to use your own name in place of DEMO. These credentials will be connected to any commits you make.

$ git config --global user.name "DEMO Username" 
$ git config --global user.name "DEMO Email" 

Step 3: Creating a new Repo

Login to the official GitHub page and make a new repository.

Step 4: Basic Git Workflow

Note: Kindly insert your own values in between <>, wherever given.

Make a folder / directory on the command line of your Linux machine, using the following command:

$ mkdir <folder-name> 

Create a file in the recently made folder:

$ cd < folder-name> 
$ nano <file-name>  

Initialize the git repository, by using the following command:

$ git init

Let’s see the current position of our working directory and staging area. It will show the files and folders, ready to be added:

$ git status

Use the add command to add the files to the staging area. It also prepares the staged items to be committed next.

$ git add .

Note: The above command will add all the files and folders of that particular folder to the staging area. If you want any particular file to be staged, use the following command instead:

$ git add <file name> 

You may check again the status of your files, by using git status command. Save all the changes made to the working directory by using the following command takes everything in the staging area and makes a permanent snapshot of your repo’s current state.

$ git commit –m “<your-commit-message>” 

In the directory, where your repo is stored, add a new remote using the following command on the terminal:

$ git remote add origin <remote-repo's URL>

Two arguments are required for the git remote add command:

To push the latest changes to the central working directory, the following command is used:

$ git push –u origin main

To clone or copy an existing repo in your new directory, the following command is used:

$ git clone <path of repo, you want to clone>

Last updated