Getting ahead in Git and GitHub(Part 2)

Table of contents

No heading

No headings in the article.

Architecture of Git

Most of the version control systems have a two-tier architecture. However, Git has a layer more, making it a three-tier architecture.
The three layers are:

  • Working directory: This is created when a Git project is initialized onto your local machine and allows you to edit the source code copied.

  • Staging area: Post the edits, the code is staged in the staging area by applying the command, git add. This displays a preview for the next stage. In case further modifications are made in the working directory, the snapshots for these two layers will be different. However, these can be synced by using the same ‘git add’ command.

  • Local repository: If no further edits are required to be done, then you can go ahead and apply the git commit command. This replicates the latest snapshots in all three stages, making them in sync with each other.

  • Hands on with some git commands

    Inside our terminal , let's make a directory (Project) in which we will practice some commands.
    -$ mkdir Project : helps us in making a directory named Project
    -$ ls -a Project: list all the files and hidden files inside Project directory Screenshot from 2022-10-04 18-28-22.png -$ git init: The git init command creates a new Git repository. Executing git init creates a .git subdirectory in the current working directory, which contains all of the necessary Git metadata for the new repository.
    Screenshot from 2022-10-04 18-35-32.png Now if we do the $ls -a command in the project directory we will be able to see .git directory

    Screenshot from 2022-10-04 18-43-05.png Lets make some changes into our project.
    -$ touch names.txt : touch command serves various purposes such as generating files or sets of files, altering the modification or access times of files, etc. Screenshot from 2022-10-04 18-44-06.png

    - $ git status : The git status command displays the state of the working directory and the staging area. Screenshot from 2022-10-04 18-46-24.png As we can see in the output our branch is master (discussed further) , we haven't committed anything and there is an untracked file which we just created( names.txt) but its empty .
    Now lets add this untracked file to the staging area with:
    -$ git add : The git add command adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit.
    But lets suppose if there are (20-30 or even more files , we will not write name of each file to add them to the staging area). Therefore $git add . command comes to play
    ( here . means add all the files in the current directory to the staging area)

    Screenshot from 2022-10-04 18-54-57.png We see that the names.txt file which earlier was untracked is now in the staging area (use "$git rm --cached ..." to unstage)
    Files added to the staging area needs to be committed (a commit records changes to one or more files in your branch. Git assigns each commit a unique ID, called a SHA or hash, that identifies: The specific changes. When the changes were made.)
    - $ git commit -m "...": captures the snapshot of the project's currently staged changes and there need to be precise message telling what that commit is all about.

    Screenshot from 2022-10-04 19-01-53.png Lets shift to VS code and add some content into the names.txt file.

    Screenshot from 2022-10-05 11-25-57.png This is modified names.txt hence we need to bring it to the staging area with $git add . and then commit the change with some specific message.

    Screenshot from 2022-10-05 11-27-59.png


    What is Github???

    GitHub is a Git repository hosting platform. It provides the distributed version control of Git plus access control, bug tracking, software feature requests, task management, continuous integration, and wikis for every project.


    Lets make a public repository and transfer the project that we have just created using terminal commands.
    Make a github account , for that you can visit their official website (here)
    After setting up your profile and all , lets create our repository. Click on 'New' button

    Screenshot from 2022-10-05 11-46-55.png And write the name of the repository (here Project) , keep it Public and click on create repository. Screenshot from 2022-10-05 11-43-43.png Here we will now push existing code from the command line.

    Screenshot from 2022-10-05 11-53-08.png -$git remote add origin https://github.com/peeyushguleria/Project.git
    -$git branch -M master
    -$git push -u origin master

    image.png These commands will help us to export our code to the github.
    Refresh your github page and you will see the following interface.

    Screenshot from 2022-10-05 11-57-52.png Inside names.txt we can see the names we entered.

    Screenshot from 2022-10-05 11-58-27.png We had done 2 commits , they can also be seen on the GUI as well as the CLI. Screenshot from 2022-10-05 12-00-04.png

    Screenshot from 2022-10-05 12-01-09.png Also if we want to pull some changes to our local repository , the command for that is $git pull and same for if we want to push some changes $git push.

    That's pretty much about the git and github , further more blogs would be coming which will explain these push and pull commands and the origin as well as branche concepts. Thanks for reading this blog , like it and share it with your friends .