Version Control

What is Version Control?

Imagine you’re working on a project, like writing an essay or painting a picture. As you make changes, you might want to keep track of different versions to see how your work evolves over time or even to have access to previous versions because maybe you liked a specific part more, but decided to erase, and after a while you wanted to use it again. Version control is like having a super organized system that helps you keep track of all these different versions.

In simple terms, version control allows you to:

  1. Save different versions of your files.
  2. Keep track of who made changes and when.
  3. Compare different versions to see what changed.
  4. Go back to an earlier version if something goes wrong.
Version Control in Software Engineering:

In software engineering, version control is super important because you’re often working with lots of different files and collaborating with other people. It helps keep everything organized and makes it easier to work together.

There are several different tools you can use to keep your code organized.

Git is one of the most popular version control systems used in software development. It’s like the magic behind the scenes that helps developers collaborate smoothly. With Git, you can create a “repository” (or repo for short), which is like a special folder that tracks all the changes to your files.

Here’s how it works:

Step 1: Open Terminal

First, open Terminal on your MacBook. You can find it in the Applications folder under Utilities, or you can use Spotlight Search by pressing Command + Space and typing “Terminal”.

Step 2: Navigate to Your Project Directory

Use the cd command to navigate to the directory where you want to create your project. For example, if you want to create it in your Documents folder, you would type:

cd Documents

Step 3: Create a New Directory

Now, let’s create a new directory for your project. You can name it whatever you like. For example:

mkdir my_project

This command creates a new directory named my_project.

Step 4: Navigate into Your New Directory

Use cd to navigate into your new directory:

cd my_project

Step 5: Initialize Git

To initialize Git in this directory, simply type:

git init

This command initializes a new Git repository in your project folder.

Step 6: Create Some Files

Now, let’s create some files for your project. You can use any text editor you prefer. For example, you can create a new file called index.html:

touch index.html

Step 7: Add Files to the Staging Area

Before you commit your changes, you need to add the files to the staging area. This tells Git which files you want to include in the next commit. You can add all files using:

git add .

Or you can add specific files by replacing . with the file name. For example:

git add index.html

Step 8: Commit Your Changes

Once your files are staged, you’re ready to commit them. Committing means saving the changes to your local repository along with a message describing the changes. Use the following command:

git commit -m "Initial commit. Added index.html"

Replace the message inside the quotes with a brief description of the changes you made.

Step 9: Push to GitHub

Now, if you want to push your local repository to GitHub, you’ll need to create a repository on GitHub first. Once you have created the repository, you can push your local repository to GitHub using the following command:

git remote add origin <repository_url>

Replace <repository_url> with the URL of your GitHub repository.

Then, push your changes to GitHub:

git push -u origin master

This command pushes your changes to the remote repository (GitHub) and sets the upstream branch.

That’s it! You’ve successfully created a Git repository from scratch on your MacBook, added files, committed changes, and pushed everything to GitHub.

But wait. What is GitHub?

GitHub is like a fancy online hub for Git repositories. It’s like a social network for developers, but instead of sharing photos or videos, they share code.

Here’s how it fits in:

GitHub hosts Git repositories online, so you can access your projects from anywhere with an internet connection. It makes it easy for multiple people to work on the same project by allowing them to clone repositories, make changes, and then submit those changes for review.

When you want to add your changes to a project hosted on GitHub, you create a “pull request” to ask the project maintainer to review and merge your changes.

In a nutshell, Git helps you manage changes to your code, while GitHub provides a platform for sharing and collaborating with others. Together, they make the world of software development a whole lot easier to navigate!

Hummmm 🤔 This is not clear.

Ok, so imagine you went through all the steps above to create the directory folder for your project, you initialized the Git Repository. So you can write code, and create several versions of it. That’s awesome. But it still sits on your machine only. So how other people can access your code? Or, what if your house is invaded by a bear and it wracks your office and destroy your computer looking for food?

Ok, this is probably not gonna happen. But computers die, right? What if you loose everything?

So, what you do? You push your code to Github. Git hub is where all the magic repos live.

So let’s say you are working on your code with a friend, you push the project to Github, your friend can clone the project to their machine, create a new branch and work on whatever changes they need to, then they push their branch with the changes back to the remote repository living in Github via a Pull Request. Then, ideally, someone else would review the Pull Request (PR) and then merge that new branch into the main (master) branch and the project code is updated on a new version. You, on ther other end, can pull the code which will update the version of the project you have on your computer to match the most updated version, and then you can work on your changes and repeat the process.

So it “looks” a bit like this:

In the context of a repository, branches represent different paths of development, enabling the isolation of new features or fixes without impacting the main version of the project. Creating a branch essentially produces a copy of the main project, providing a secure environment for implementing changes.

Pull Requests (PRs):

A fundamental feature of GitHub, pull requests serve as a means to suggest modifications to a repository. Upon readiness to integrate changes into the main project, a pull request is created, signaling to project maintainers the desire to merge changes and allowing them to review the code before incorporation.

PR Reviews:

Throughout the pull request review process, fellow developers offer feedback on the code, recommending improvements, identifying potential issues, and posing queries about the changes. PR reviews significantly contribute to the collaboration process, ensuring the code’s quality prior to its addition to the project.

Merge:

Following approval of the pull request, changes can be merged into the main project. This action combines the alterations from the branch with the main repository’s branch, effectively integrating the work into the project’s history.

In conclusion, version control and platforms like GitHub play a pivotal role in modern software development, offering seamless collaboration, meticulous organization, and robust disaster recovery. By embracing these tools and understanding the workflow, developers can efficiently manage code changes, work harmoniously with peers, and safeguard their projects against unforeseen events. The combination of version control and platforms like GitHub ensures that software development remains agile, responsive, and conducive to continuous improvement. Cheers to streamlined collaboration and code management!

Discover more from { Code Journey; }

Subscribe now to keep reading and get access to the full archive.

Continue reading