DEV Community

Ankith Deegoju
Ankith Deegoju

Posted on

Basics of Git -

What is Git.

Git is a version control system. Version Control System is a software that manages changes of your code/files in a folder/directory.

What is the problem its solving -

  • Provides us tracking of changes in codebase.

  • Provides us simultaneous access to codebase enabling collaboration
    [ via GitHub -> Simply say git in a server || Even I need to deep dive into it ]

  • Provides us a single source of truth i.e. provides us exact state of code base that is constantly being updated with changes done by members accessing the code base.

To understand it's importance and need of git.

Scenario - Imagine how you would collaborate with other developers and build together a project.

Suppose you have multiple people working with the same codebase but need simultaneous access and push changes independent of others in different parts of codebase.

Era before git - By sharing code via pendrive.
Think of use cases like - How can we provide simultaneous access and consistent updates of code across all developers of the code base.

I would like to explore other ideas which you can think of in the above context.

How was git built - [Just a short story]

Linus Torvald was working on Linux. As it was open source project many developers pushed code to improvise it, eventually the code base was too big to track and update changes. To solve this issue git was developed as his side project.


How do we use git -

[Start doing it in a system with me]

  1. Create a folder - LearningGit

  2. Create two files - ex - demo1.txt , demo2.txt

  3. Open your terminal and let's work in it now on.

Few basic commands -

ls - to list files in a folder.

              Linux/WSL    PowerShell (ls)    Command Prompt (cmd)
List Files   ls    ls or dir           dir

Detailed List   ls -l      ls (default)        dir

Show Hidden ls -a      ls -Force           dir /a

All Details ls -la     ls -Force           dir /a
Enter fullscreen mode Exit fullscreen mode

nano - Linux -> To create and edit files

edit - Windows -> Installation Command - winget install Microsoft.Edit

Usage:- > edit <filename>

Key Bindings { Shortcut Keys } ->

Ctrl+S to save.

Ctrl+F for find and replace.

Ctrl+Q to exit. 

Enter fullscreen mode Exit fullscreen mode

Add some info into your files .

cat - Linux -> To view file data

type - Windows

Usage - type file.txt

Concatenate/Merge files: type file1.txt file2.txt > combined.txt

Let's now start using our git commands .

git init - Initializes git inside your folder with by creating a .git file.

O/P => Initialized empty Git repository in C:/WebDev/LearningGit/.git/

git status- TO check the status

Red - Untracked
Green - Files that are being tracked / staged to be tracked.

Staging -

We need to stage files that we wish to track. A way to tell git to look into these files.

git add <filename> => For individual file to be tracked

git add . -> For all files within the directory to be tracked.
Enter fullscreen mode Exit fullscreen mode

Committing -
Once we are okay with changes we can commit the changes so that that we can store them as checkpoints for reference of changes or revert back incase of any fallback.

Command-

git commit -m "Initial files" 
Enter fullscreen mode Exit fullscreen mode

Try to change the code a few times and try to commit.

Once committed -> changes are permanently stored in .git file.

How does it do it.
Under the hood we create a linked list of commits to file. Each new node stores a hash of prev node as a reference. As we have a head pointer in linked list we have a head here which points to the most updated node.

We can perform git add and commit both in a single command.
git commit -am "msg"

git log - gives us history of commits performed.

You get the list of commits with a unique hash value associated with each commit.

We can compare changes form one commit to another using commands-

git diff <hashval of commit> <hashval of commit to be compared with>

git revert hash_id=> Creates a complelement of the iven hash_id and attaches head to it.

The attached screenshot would be helpful.

Under the hood working of git and basic commands

We will explore .git folder,git branching, GitHub concepts in the upcoming blogs.

Git Cheat Sheet

Top comments (0)