DEV Community

Ritvik Shukla
Ritvik Shukla

Posted on • Edited on

Version Control System For Beginners-Part II

What is Git?

Git is a type of distributed version control system used for managing and tracking the changes done to the code. Git was created by Linus Torvalds in 2005 for development of the Linux Kernel. Git is a free and open source software.

Real life projects generally have multiple developers working in parallel. So a version control system like Git is needed to ensure there are no code conflicts between the developers.

Software requirements changes with time. So Git allows the developers to revert and go back to the older version of the code.

If you are the open-source enthusiast or you want to work in some big tech companies in the future you will need git somewhere definitely so lets understand How to install git on your pc!

If you are using Windows Operating System Follow these steps:-

  1. Click to download git for windows (https://git-scm.com/downloads)

1 8_bwbclOlP4bSqpWM25u0g

  1. When you will click on the git installer that you have downloaded from the above link blindly click next in every step and get git installed on your pc.

1 FnXwHJFjEerNgvbp5Zry3g

1 3dLzxTjuX3yuXTjCvdDK7w

For Linux Based Systems:-

Open the terminal using the shortcut Ctrl+Alt+T

Copy and paste these commands in the command window which you opened:-

$ sudo apt install git
$ sudo apt update
$ sudo apt upgrade

Copy and Paste these commands one by one in the terminal. You may be prompted to enter your password.

How to Create a git repository (In Windows) ?

You have to create the repository if the original code is in your pc. Create a folder in which you want to save the original code files. Right click inside the folder and click on Git Bash here. Git command terminal will open and you are good to go.

How to Create a git repository (In Linux)?

Open the terminal using the shortcut Ctrl+Alt+T

To make a new folder or directory on your pc:-

1.Type mkdir DIRECTORY NAME and then press Enter

  1. Type ls to list all the directories present on your pc

  2. Type cd Directory name to go to the wanted directory

These commands will create a new directory on your pc. After that you can initialize a new git repository in that directory.

1 Iw93UpWGeHjy-qfSAEIIiQ

To check whether git is successfully installed on your pc or not, use the command given below:-

$ git --version

If git was successfully installed on your computer then the output that you will get will be:

git version "Version of git installed" example- git version 1.9.1

1 7zB5xFesoD_4auWjrBgVFw

If you get the output like this, then git is installed successfully on your pc.

Now when we have installed git on our pc we will learn How to configure your Git username and email using the following commands. These details will be associated with any commits that you create.

To configure your Git username for every repository on your computer:-

$ git config --global user.name "Tech4everybody"Replace our name with your own inside the inverted commas!

To Confirm that you have set the Git username correctly(If you have configured username for every repository):

$ git config --global user.name

1 JuuTyUkwwP4FgaFepcnMTA

To configure your Git username for a single repository:-

$ git config user.name "Tech4everybody"Replace our name with your own inside the inverted commas!

To Confirm that you have set the Git username correctly(If you have configured username for a single repository):

$ git config user.name

1 UnV4jcqNqeCzffE8u66Hkg

To configure your Git email for every repository on your computer:-

$ git config --global user.email "Tech4everybody@t4e.tech"Replace this email with your own email!

To confirm that you have entered the right email id (If you have configured email for every repository):-

$ git config --global user.email

1 oN40lRsJrTgA47hCXMaXvg

To configure your Git email for a single repository:-

$ git config user.email "Tech4everybody@t4e.tech"Replace this email with your own email!

To confirm that you have entered the right email id(If you have configured email for a single repository):-

$ git config user.email

1 r7zeZwDW06TTgh__jz9peg

Before learning the initialization of the git repository let’s understand

What is a git repository?

A Git repository is a virtual storage of your project. It allows you to save versions of your code, which you can access when needed.

How to initialize a new Git repository?

To create a new repository, you’ll use the git init command. git init is a one-time command you use during the initial setup of a new repository. Executing this command will create a new .git subdirectory in your current working directory. It can be used to convert an existing, unversioned project to a Git repository or initialize a new git repository. Usually this will be the first git command that you will use.

1 VsEz9b2u5IZOVV7ZAbZu5g

How to add files to any git repository?

The git add command adds the untracked files to the staging area. However, git add doesn't really affect the repository in any significant way—changes are not actually recorded until you run git commit.

To add one file to staging(Index) area:

$ git add Filename

1 b4ueDLmyLPdxduzI1iQ9LQ

To add more than one file to staging(Index) area:

$ git add .

Git status command is used to show modified files of the working directory, staged for your next commit.

$ git status

1 5ESedTvao1jykffUybv6tg

How to save your changes to the local repository?

The git commit command makes sure that the changes are saved to the local repository. The command “git commit –m ” allows you to describe everyone and help them understand what has happened.

Commits all the changed files with “Commit Message”

$ git commit -m "Commit message"

1 eI3X0b_9ZDwOazBI5ezlkg

You can use — amend flag with the git commit command to commit again for changing the latest commit. Running this will overwrite not only your recent commit message but, also, the hash of the commit. It won’t change the date and time of the commit.

It will also allow you to add other changes that you forget to make using the git add command.

$ git add "Changed file name"

$ git commit --amend -m "message"

1 f4BBW6oxHUuMR1ttWR8EVw

1 l_Dx13MTVsBCVk6IrcB-6w

You can see in the above image that the commit date and time is same for both the initial and the amended commit.

To get a record of the commits in a git repository:-

The git log command shows a list of all the commits made to a repository. You can see the hash of each git commit, the message associated with each commit, and more metadata. This command is useful for displaying the history of a repository.

$ git log

How to unstage any file from the staging area?

$ git reset filename

To know about the changes which are not staged you can use git diff command:-

$ git diff

1 PJeF_XI8M9CEIxHNzy6Wfg

Here the lines in red are those which I removed and the lines in green are those which I added.

To know about the changes which are staged but not committed you can use the command given below:-

$ git diff --staged

1 w5x90FADKowO19yIEaf0gg

1 w5x90FADKowO19yIEaf0gg

So in this blog, we have learned the most important and most used git commands. In the next blog, we will learn more about Github, and how to clone a repository, how to create a branch, and how to merge them, and many more things.

After my third blog on Version Control Systems you will be ready to contribute to the open source community.

Ending this article with Steve Jobs Quote

If you found this article useful, follow me for more!

Happy Coding!

Top comments (1)

Collapse
 
codecraftjs profile image
Code Craft-Fun with Javascript

Great content ! 👍🏻
Good to include about .gitignore file as well !