Git — Version control and brain of a codebase — Part 1

Kishor Munot
6 min readDec 11, 2023

--

Version Control

Hello Readers,

Today we are going to discuss about version control which is very important in a developer’s life because this stage creates a stressful environment at times and when many people are working on a project codebase and everyone is publishing their work after completion this stage is very important because at this stage A few mistakes can be very costly.

What is Version Control

A version control system (VCS), allows you to track changes to your code over time. It’s essential for developers who often need to work on the same codebase concurrently. It gives them a way to undo mistakes, track changes, and collaborate with others on code. However, anyone who regularly works with files that change can benefit from using version control.

There are many version control systems offered, but Git is by far the most popular. In 2018, a survey of more than 100,000 developers found that Git was the VCS used by 78.0% of respondents. This is because Git is a distributed version control system, which means that it doesn’t rely on a central server. This makes Git more flexible than other VCSs, and it’s also why Git is the VCS of choice for open-source projects.

VCS allows developers to access the project’s history in order to find answers to questions such as: What changes were made? Who made them? When did they occur? What was the purpose of the modifications?

Why Version Control is Important

Version control is important for keeping track of changes to code, files, and other digital assets. You should use version control software for all assets and development projects that multiple team members will collaborate on.

Version control software needs to do more than just manage and track files, though. It should help you develop and ship products faster. This is especially important for DevOps workflows where delivery of the latest versions can be tested or built during submission of the change. Good version control systems will flag potential conflicts before they enter the mainline of the project. This means the developer can fix the problem and not promote the problem further downstream.

Using the right version control software:

  • Improves visibility.
  • Minimizes file conflicts and wasted effort.
  • Helps teams collaborate around the world.
  • Accelerates product delivery.

GIT

Git is the most popular VCS in use today, created by Linus Torvalds in 2005. It is free, open-source, and designed to handle everything from small to very large projects with speed and efficiency. Its flexible architecture means it can be effectively integrated into your workflow. For example, you can use Git to manage all your project’s files or just a few files that are important to you.

Git supports distributed development which gives each developer a full history of their code changes locally. Git also allows for non-linear development by allowing developers to work on different branches of code simultaneously and merge them together when ready.

Basic Git Commands

A Git command is a string of text that tells Git what to do. Each Git command does a different task, but they all work together to help developers manage their code changes.

Here are some common git commands:

  • git init: Initialize a new git repository. This is the first command you should run when starting a new project.
  • git clone: Clone an existing git repository. This command allows you to create a local copy of a remote repository.
  • git add: Add files to a repository. This command allows you to add new files or changes to already existing files to a git repository.
  • git commit: Make changes to a repository. This command saves your changes to the git history.
  • git push: Push changes to a git remote repository. This command pushes your local commits to a remote git repository.
  • git pull: Pull changes from a git remote repository. This command pulls down any remote changes and incorporates them into your local git repository.
  • git status: Check the status of your git repository. This will let you know which files have been modified and which files are being tracked by git.
  • git log: View the commit history for your git repository. This is useful for finding out when certain changes were made and who made them.
  • git reset: Reset your git repository to a specific commit. This command allows you to undo changes to a file or reset your git history.

How to Use Git

Git is typically used through the command line. The Git command line interface (CLI) is a tool for running these commands. The Git command line tool is installed by default on macOS and Linux and can be easily installed on Windows. There are various Git clients with friendlier interfaces that allow users to work more efficiently with git.

Once you have Git installed, you can clone an existing repository or create a new one. To clone a repository, you will need the URL of the git repository. To create a new git repository, use the git init command.

Once you have a git repository, you can add files to it and commit changes. For instance, to add a file to a git repository, use the git add command. To commit changes, use the git commit command.

While Git is a powerful tool, it can be challenging to use if you’re not familiar with the command line. That’s where GitHub comes in. In fact, GitHub is built on Git.

Real situation in the life of a coder

Your client asks you to implement an ambitious modification to the website. It’ll take you a couple of weeks, and involve edits to many pages. You get to work.

You’re 50% done with this task when the client calls and tells you to drop what you’re doing to make an urgent but more minor change to the site. You’re not done with the larger task, so it’s not ready to go live, and the client can’t wait for the smaller change. But he also wants the minor change to be merged into your work for the larger change.

Maybe you are working on the large task in a separate folder containing a copy of the website. Now you have to figure out how to do the minor change in a way that can be deployed quickly. You work furiously and get it done. The client calls back with further refinement requests. You do this too and deploy it. All is well.

Now you have to merge it into the work in progress for the major change. What did you change for the urgent work? You were working too fast to keep notes. And you can’t just differentiate the two directories easily now that both have changed relative to the baseline you started from.

The above scenario shows that source control can be a great tool, even if you work solo. Source control can solve many problems for you, such as the following:

  • You can use branches to work on longer-term tasks and then merge the branch back into the main line when it’s done.
  • You can compare whole sets of files to other branches or to past revisions to see what’s different.
  • You can track work over time (which is great for reporting and invoicing by the way).
  • You can recover any revision of any file based on date or on a milestone that you defined.

Example

git log # Double check existing commits

# Commit changes (if any)
git status
git add .
git commit -m "My sweet new feature"
git push origin my-feature-branch
# End changes
git pull -r origin main
# Fix Conflicts (if any)
git add .
git rebase --continue
# End Conflicts
git log # Verify we did everything correctly
git push origin my-feature-branch --force

Thank you for reading my blog. Please share your thoughts on a blog. We are going to discuss some best practices for GIT in the next blog.

Keep Reading, Keep Enjoying, and Keep testing.

Thank you

--

--