Skip to main content

Command Palette

Search for a command to run...

Git and Its Basic Commands

Published
7 min read
Git and Its Basic Commands
O

I am a Technical Writer with a knack for creating concise and easy-to-understand contents for the end user.

Introduction

In this article, we will look at Git in detail. Git, its basic commands, installing Git, troubleshooting Git etc. will be discussed.

What is Git?

Git can be described as a distributed version control and a code management system used to track different file versions. Git enables collaboration between developers, engineers, product managers and likes, on the same project as it makes it possible to make contributions without over-riding each other's changes. It was developed by Linus Torvalds in 2005.

Difference between Git and GitHub

More often than not, people tend to misunderstand Git and GitHub. The confusion probably emanated from the fact that there's a "Git" in GitHub but they are not the same thing.

Below is a simple table that showcases the differences between Git and GitHub:

S/N.GitGitHub
1Git is a softwareGitHub is a service
2Git is a command-line toolGitHub is a graphical user interface tool (GUI)
3Git must be installed locally on your computerGitHub is hosted on the web
4Linux maintains GitMicrosoft maintains GitHub
5Git was first released in 2005GitHub was first released in 2008
6Git is solely for version control to manage file historyGitHub is a hosting service for Git repositories

How to install Git on your local machine

Installing Git on Windows devices

The best way to get Git installed is to do so from the official website.

Steps to download and install Git:

Step 1: Visit the official website: https://git-scm.com

Step 2: Click on the "Download for Windows" Tab

An image of on how to Install Git

Step 3: Next click on the 64-bit Git for Windows Setup and allow to download

64-bit Git Windows Setup

Step 4: Go to your download location, double-click the file to install the app

Step 5: Permit the app to modify your device by selecting Yes in the control window that appears

Control window panel

Step 6: Click Next after checking the GNU public license

GNU public license

Step 7: Choose the install location. Leave at default, then click Next

Location selection for Git Installation

Step 8: Leave the settings as it is when the screen for component selection pops up and click Next

Component selection pop-up

Step 9: You are asked to create a start menu folder, click Next

Start Menu folder selection

Step 10: Select the text editor of your choice to use with Git from the dropdown menu, then click Next

Select editor

Step 11: The next step is to give your branch a name**.** Git's default branch is "Master". Leave the default selection and click Next

Selecting Branch name

Step 12: Select the recommended path which allows you to use Git from Git Bash, the command prompt, the Windows PowerShell as well as other third-party software, then click Next

Selecting Path environment

Step 13: Select the Secure Shell client program you want Git to use, keep the default and click Next

Select SSH program

Step 14: The next step is about server certificates, stick with the default and click Next

Select server certificate

Step 15: This step is about how git structures data**.** Keep the default selection and click Next

Data structure settings

Step 16: A default terminal emulator has been selected, click Next

Select terminal emulator

Step 17: Now choose what the git pull command will perform. Keep the default selected option and click Next

Default git pull behaviour

Step 18: Select the Git credential Manager, then click Next

Select  Git credential Manager

Step 19: Configuring extra options. Keep the selected default option, then click Next

Configuring extra options

Step 20: You are asked to try some experimental features. Leave options unchecked and click Install

Click install option

Step 21: Launch Git Bash once installation is complete

Git bash command line

Configuring Git on your computer

To configure Git on your computer, it is essential to have Git installed locally. Configuring Git is important because it will enable you to push your changes to GitHub; a hosting service for Git repositories.

To do this, you must set up Git with your user credentials via the command line. Your user credentials are your username and email you registered with on GitHub.

Type the following command to configure Git globally on your computer:

git config --global user.name "Username"
git config --global user.email "email"

The global flag is important as it helps set up git for the user and not just a single repository.

Those commands in general create a .gitconfig file and add a user section with the name and email in your home directory.

Common Git commands

Now we will take a look at some of the basic Git commands that are needed to get the most out of the version control system.

Git Init

git init

The purpose of the command above is to initialize a new git repository or a new environment for a new project. Without the "git init" command, git will not track changes made to a file. Tracking only commences when you initialize git and then commit and push the files to a remote repository.

Git Clone

The git clone command is used to copy a particular repository. Cloning a repository means that you'll have the entire repo, its files and history on your local machine.

Use the command below to clone a repository:

git clone <repository link>

Git Add

The git add command prepares changes from the working directory to the staging area. This is important as it stages files for the next commit. Staging in git prepares changes before committing them to a repository.

To add a specific file for the next commit, use the command below :

git add <filename>

To add multiple files, use the following command:

git add .
git add -all

Dot or single period in Unix means "current folder" hence the command git add . tells git to stage all files in the current folder.

Git Commit

The purpose of the git commit command is to transfer files from the staging area to your repository.

The command can be used with the following flags:

git commit -m "commit message"
git commit -a "commit message"
git commit -am "commit message"

The -m flag creates a commit written with a commit message under quotation.

The -a flag only commits tracked files i.e. files that have been added with the git add command.

The -am flag is a combination of the -m and -a flags.

Git Status

git status

The git status command displays the current state of the working area. With the command, you can see which files are being tracked, the changes that have been staged and those that are yet to be staged.

Branching in Git

Creating a Branch in Git

This is an important feature in Git as it allows for collaboration between software engineers working on a project. Creating a branch apart from the Main branch makes it possible for developers to work on different aspects of the software.

To create a branch, use the command below:

git branch <branch name>

This command will create a different branch with the customized name chosen. From this branch, changes can be pushed and pulled from the repository.

Git Checkout

The git checkout command makes it possible to switch between different versions of files on your local machine.

git checkout new_branch
git checkout -b <new_branch>

The command works closely with the git branch command. We know the git branch creates a new branch, and the "git branch new_branch" creates a new branch off the main. The "git checkout new_branch" can be used to switch to that branch.

The git checkout command also accepts a -b argument which makes it possible to create a branch and switch to it immediately.

Troubleshooting in Git

Undoing changes in Git

To move back to an old commit, use the command below:

git checkout commit-id

To roll back to the last commit, use the command below:

git reset --soft HEAD~

To permanently delete changes that may have been done after a specific commit, use the command below:

git reset --hard commit-id

To make delete changes made after the last commit, use the command below:

git reset --hard HEAD~

To undo changes in a file or a directory, use the command below:

git checkout -- name_of_the_file

To undo changes all changes present in a file on the current directory, use the command below:

git checkout --.

Conclusion

Git is an important version control system tool that makes the software development lifecycle easy for software developers, testers, project managers etc. through its ability to track changes and improve collaboration. The commands highlighted in this guide and many others are used to interact with Git.

Visit the official Git Documentation for more commands, tips and how to troubleshoot various issues that may arise while using Git.