Introduction to Git

Introduction to Git

·

4 min read

What are Git and GitHub

  • Git is an open-source software tool popularly known for its version control system.
    So, what does version control mean?
    Version control is like a superpower that is used to manage all the changes made to a file. It tracks and saves the changes we made so that we can simply go back to a particular change we made at a point in time.

Git has another cool feature that allows multiple users to work on the same project. It gives access to those users and the version control in it tracks and manages the changes made by each other.

  • GitHub is a web-based hosting platform where one can store their git projects and collaborate on projects
    GitHub is the most popular open-source community. It helps developers to contribute and share their projects and knowledge. Anyone can access and learn from all over the world.

Github gets all the learners, developers, and tech enthusiasts in one place. It is used by most organizations in the world.
So, the combination of git and GitHub becomes a powerful tool for developers.

Installing Git

We can download Git for free from the following website here
After installing, by opening the command prompt we can check whether it is properly installed or not by using the git --version command in windows.

 git --version
 git version 2.37.3.windows.1

Set up Identity

By entering your name and mail-id git will be able to know who is using and this will be used to login into GitHub too.

git config --global user.name "name"
git config --global user.email "name@mail.com"

Initializing Git

To initialize git on a folder, by selecting the folder use the git init command

selected folder> git init

Phases of a Git File

-> 1. Modified:
When we are working on our project and made any kind of changes to our files then it is called modified.
Let's say I have added a text file to my project folder, now if we check the status by using the command git status

Project > git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
newfile.txt
nothing added to commit but untracked files present (use "git add" to track)

It says that there are files that are "untracked" and also suggests us to use the git add command to track. Which will be done in the staged phase.

-> 2. Staged:
As we made some changes in the previous step and now we have to bring those changes to the staging area and make those untracked files into tracked.
By using commands like,

  • git add used to add mentioned file to the staging area

  • git add . used to add multiple files to the staging area
    Now, if we check the status:

    Project>git status
    On branch master
    Changes to be committed:
    (use "git restore --staged <file>..." to unstage)
         new file:   newfile.txt
    

    So,we got our newfile into staging and its ready to get committed.
    But, incase you want to unstage and discard those recent changes use the below command:

    Project>git restore --staged newfile.txt
    

-> 3. Commit:
Here, the changes will be applied to our project i.e, the new changes waiting in the staging state will be added to the project.
So, finally when you make a commit, and it gets committed, then this simply means that you have successfully applied a certain modification to the code.

Project>git commit -m "Added newfile.txt"
        [master a3cd09c] Added newfile.txt
         1 file changed, 1 insertion(+)
         create mode 100644 newfile.txt

To view all the history of changes we made, use the command git log:

 Project>git log
        commit 66e5a23bdb1f831fbc5153ec07f58cfc950ed221 (origin/master, origi/master, 
        features)
        Author: UserName<you@example.com>
        Date:   Sat Sep 30 18:04:40 2022 +0530

         newfile.txt modified
         commit a3cd09cc7a2be651b9a32f0baa89cce378d759bc (HEAD -> master)
         Author: UserName <you@example.com>
         Date:   Fri Sep 30 18:02:40 2022 +0530

              Added newfile.txt

From the above we can see there is a hashid for every commit we make.
So, we can use this hashid when we want to remove a particular commit from the history of the project. Let's see how it works by using the below command:

 Project>git reset 66e5a23bdb1f831fbc5153ec07f58cfc950ed221

Now, if we check the log:

Project>git log
commit a3cd09cc7a2be651b9a32f0baa89cce378d759bc (HEAD -> master)
Author: UserName <you@example.com>
Date:   Fri Sep 30 18:02:40 2022 +0530

    Added newfile.txt

There is only one commit here that means, we removed the other commit by using the hashid.

Stash

If there is a case, where you made some changes to your project but you are not sure of committing them and want to store them somewhere else, then by using the command git stash it saves your changes for later use.

  • git stash pop used to bring the changes that you stashed recently.

  • git stash clear used to clear the changes that you stashed and we can't bring them back.