What is Git? How to use Git commands in Terminal?

What is Git? How to use Git commands in Terminal?

Hello there, there are many distributed version control systems are available like git, Mercurial SCM, Fossil, Pijul, BitKeeper, Veracity, and monotone. This article will talk about Git version control system because Git is the most popular and widely-used distributed version control system.

Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It helps developers to collaborate on projects of any scale.

Note: I used Ubuntu operating system to execute the git commands.

Now, let's get started:

How to Install Git using the terminal in Ubuntu:

Open your Terminal using Ctrl + Alt +T and use the below commands to install GIt. The newest version of git is installed.

      sudo apt-get update 
      sudo apt-get install git

How to check Git is installed or not:

The below command will return the version of git installed on your machine.

      git --version

How to check Git Configuration:

The below command returns git configuration on your machine including user name and user mail.

     git config -l

How to Change or Update your user name:

Use the below command to update or change your user name. Replace #Your_name with the name you want to configure.

     git config --global user.name "#Your_name"

How to Change or Update your user mail:

Use the below command to update or change your user mail. Replace #Your_mail with the mail you want to configure.

     git config --global user.email "#Your_mail"

How to cache your login Information:

We can store our login details using the below command.

     git config --global credential.helper cache

Initialize a git Repository:

To initialize a git repository locally for your project, use the below command. It is the first step, to start your project as a git repository.

     git init

Add a file to the Stagging area:

You can add a single file to the stagging area. The stagging area confirms that file or files are ready to commit. Change #File_name to the file name you want to add.

      git add #File_name

Add multiple files to the Stagging area:

If you want to add multiple files to the stagging area, then use period (.). It will add all the files which are changed in the repository.

      git add .

Check status of Git repository:

Use the below command to check the status of your git repository. It shows the status of the working repository.

      git status

Unstage the changes:

This command is used to unstage the changes done by the git add command. Replace the #file_name with the files you added. If you want to unstage all the files use the period(.) sign.

      git restore --staged #file_name

Commit changes in Git:

To commit the changes, use the below command. This command will open a text editor in the terminal to write a commit message. Commit message is nothing but a description of changes or updates you made

      git commit

Commit Changes by passing message in command only:

In the below command, we pass First Commit as a commit message

      git commit -m "First Commit"

Commit history:

This command shows the history of the current repository.

      git log

Commit history including changes:

To see our commit history including all files and their changes, use the command as follows:

     git log -p

Specific commit information:

The below command will retrieve information about the specific commit. Replace commit-id with the id which you want. You can get commit-id using the git log command

      git show commit-id

Log stats:

It displays statistics about including author, date of commit, time, commits, and changes in the files. It shows how many files are inserted or deleted at each commit.

      git log --stat

Changes made before committing them using "diff":

You can pass a file as a parameter to only see changes on a specific file. git diff shows only unstaged changes by default. We can call diff with the --staged flag to see any staged changes.

     git diff
     git diff all_checks.py

     git diff --staged

Review the Changes before committing:

To review the difference before adding modified contents to the index.

     git add -p

Remove tracked files from the current working tree:

This command expects a commit message to explain why the file was deleted. Replace filename with the file you want to remove

git rm filename

Rename files:

To rename a file in git, use the below command.

      git mv old_filename new_filename

GitIgnore Command:

A gitignore file specifies intentionally untracked files that Git should ignore. Create a .gitignore file and commit it.

Rollback to the last commit:

git revert will create a new commit that is the opposite of everything in the given commit. We can revert the latest commit by using the head alias like this:

      git revert HEAD

List the branches in the current repository:

The below command lists all the branches which are present in the current working repository.

     git branch

Create a New branch:

The default branch in git is the main branch. This command creates a new branch in the current working repository. Here, replace branch_name with a branch name that you like.

     git branch branch_name

Switch between branches:

This command switches you from your current working branch to the branch you specified in the command i.e. branch_name.

     git checkout branch_name

Shorthand to create and switch to new branch:

     git checkout -b branch_name

Delete the branch:

     git branch -d branch_name

Merge two branches:

To merge the history of the branch you are currently in with the branch_name, you will need to use the command below:

      git merge branch_name

Add a remote repository:

This command adds a remote repository to our local repository. Replace #your_url with your remote URL like https://github.com/user_name/example.git

     git remote add origin #your_url

Review remote URLs:

      git remote -v

Push changes to the remote repository:

The git push command is used to upload local repository content to a remote repository. Pushing is how you transfer commits from your local repository to a remote repo.

     git push <remote> <branch>

OR

Use this command if you are working on the main branch.

     git push -u origin main

Pull changes from a remote repo:

If other team members are working on your repository, you can retrieve the latest changes made to the remote repository with the command below:

      git pull

Force a push request

This command will force a push request.

     git push -f

These are the basic commands which every git user should know. There are many more commands in git. You can check them at Git Documentation. Thank you for reading this article.