Git for Beginners: Basics and Essential Commands

1. What is Git?
Git is a distributed version control system and is essentially a code tracker in simple words. Git works in a way that it tracks the commits made to a code base making it easier to track and understand the changes developers made over time while working on the project. Git helps keep track of any additions or deletions made to the code as it tracks the various versions of the project. Git stores the project data such as who authored the commit, the timestamp of the commit and also the previous versions(history) of the project in the repository. GitHub is a remote version control system that acts as a server storing a copy of the Git repository and allowing you to collaborate with multiple developers. GitHub is the server where the data related to the changes is stored for access by collaborators. GitHub receives commits when you push them and allows others to pull those commits, showcasing collaboration.
2. Why Git is Used?
Working on a project without Git makes it difficult to track changes, undo mistakes, and understand how the code evolved over time. Problems like creating multiple copies of the project with different changes in each version and no coordination between them were faced by developers. Not using Git creates a fear of breaking the currently working code, which can prevent one from adding new features. Only if they use Git can they have a working version of the code saved and then freely experiment with the code to try adding new features. Even solo developers need Git because they may not remember the changes they made to the code yesterday, which adds a lot of friction to the programming process. Problems like not knowing which change broke the code can also occur.
Even team collaboration needs Git, as it allows them to work on the code simultaneously. Git records which developer made each specific change to the code, along with the timestamp, and this data is stored in the repository. GitHub, being the server hosting Git, allows for collaboration and provides clarity on who made a certain change to the code. In large codebases, it can be hard to find and identify changes made by any developer. Git helps by keeping track of every change and who made it.
Git was originally created by Linus while developing his Linux operating system when he needed a version control system to track changes made to the code. Having seen the problems that arise if Git is not used, let’s understand how Git actually solves them.
3. Git Basics and Core Terminologies
Repository:
Repository is the place where every change made on the code is stored. Repository can exist locally on a developer’s machine, and it can also exist remotely on platforms like GitHub. Git works locally, while GitHub stores the remote copies of the repositories. Once a commit is made, the changes to the code, the author of the commit, and the timestamp of the commit are stored in the repository.
Working Directory:
Working directory is the folder where our project exists and it contains the source code, README, and any other file that is currently being editing. The working directory is the current state of the project where files are being edited before being saved into Git’s history. Git does not save any file or change automatically unless a commit is made.
Staging Area:
Staging Area is the location where the change is stored before a commit is made. The staging area is the intermediate state between a working directory and a repository. Once a change is made to the code, the file is added to the staging area. After being added to the staging area, a commit is made, which stores the change in the repository.
Commit:
Commit is the snapshot of the project that is stored in the repository. A commit is created from the changes added to the staging area. Once a change is added to the staging area, it is committed, and then those changes are stored in the repository. A commit stores data such as the exact changes made to the code, the author of the changes, the timestamp of the commit, and links to previous commits.
Commit History:
Commit History is the record of all the commits made so far by the multiple programmers. All of the past commits are represented in a sequential manner, and each commit points to the previous one. Commit History represents the timeline of the project.
Branch:
Branch is the concept in Git that creates a separate line of development. Branching allows working on different features without affecting the original code. Using Git, different developers can create branches of the code and work on their own specific version of the code. This allows multiple developers to work on a single project simultaneously and on different features.
HEAD:
HEAD points to the latest commit. It represents the current state of the project in Git history. When a new commit is made, HEAD moves forward and points to the new commit. HEAD is essentially a pointer to the latest commit. The HEAD branch points to the branch currently being worked on.
4. Common Git Commands
Installing Git:
Navigate to the latest Git for Windows installer and download the latest version.
Once the installer has started, follow the instructions as provided in the Git Setup wizard screen until the installation is complete.
Open the windows command prompt
Type
git versionto verify Git was installed.
Essential Commands:
git init→ Used to create a Git Repository, it runs only once per project.git add→ Used to add the current changes from working directory to the staging area, waiting to be committed.git commit→ Used to add the commit to the repository and saves any staged changes.git revert→ Used to create a new commit that undoes any unwanted changes made in an earlier commit by creating the complement of the commit.git reset→ Used to move the HEAD to a certain commit and optionally upstages or discards changes.git log→ Used to display the history of all the commits made so far.git diff→ Used to find the differences between two different commits, it is used to point what changed between those two commits.git status→ Used to show the current state of your working directory and staging area.
Basic Developer Workflow:
A developer creates a project folder.
Initialize Git using
git initEdits files
Checks state using
git statusStages changes with
git addSaves snapshot with
git commitViews history with
git log
The basic development workflow starts with creating a project folder, which is the working directory where all the files reside. This is the fundamental step in the development process. After creating the project folder, the folder is initialized using git init, which creates a repository where all the changes will be saved. The developer starts working on the project and checks the project state using git status. Once enough progress is made, the developer uses the command git add to add the changes to the staging area. Later, git commit is run to commit those changes and save them to the repository. After several commits, git log is used to see the history of all the commits made so far.
Diagrams:
Git Working Lifecycle:

Git Commit Timeline:





