Basics: Getting Started with Git and Version Control

Basics: Getting Started with Git and Version Control

ยท

7 min read

  • This article will provide a comprehensive introduction to Git and version control, offering a step-by-step guide to help you get started with managing your code and collaborating with others effectively. Whether you are new to version control or looking to enhance your existing skills, this guide will equip you with the knowledge and tools needed to streamline your development process and ensure the integrity of your codebase.

Grasping the Concept of Version Control

Version control is a system that enables the management and tracking of changes in a project or file. Commonly employed in software development, it helps monitor code modifications. This system maintains a change log and facilitates collaboration among team members. Version control allows for creating, comparing, and merging different project versions, ensuring access to the most current and accurate version. This process is vital for maintaining project integrity and development efficiency.

Git is a version management tool that records changes to files or projects. Linus Torvalds, the founder of the Linux Operating System, created it. GitHub is a cloud-based, open-source platform that connects developers worldwide, enabling collaboration, sharing, and software improvement across public and private teams and personal projects. Users can upload their code and track project changes for potential future use.

Getting started with Git

You can easily download Git for free by visiting the official website.: https://www.git-scm.com/

To start using Git, we first need to open our command shell.

To check if Git is properly installed, you can use Git Bash, which comes with Git for Windows. If you're using Mac or Linux, you can utilize the built-in terminal for the same purpose.

$ git --version
  git version 2.30.2.windows.1

If Git is installed, Running the command git --version should display something like git version X.Y.

Time to configure our GIT

Inform Git about your identity. This is crucial for version control systems, as each Git commit relies on this information.

$ git config --global user.name "Your name"
$ git config --global user.email "Your email"

Update the username and email address to your personal information. You may find it useful to use this information when signing up for GitHub in the future.

Initializing Your Repository

Before initializing Git, we'll create a folder and change the current working directory to that folder. Once that's done, we can initialize Git in our new directory.

Let's create a new folder for our project using this command:

$ mkdir my-project

Next, change the directory to the current folder named "my-project" using the following command:

$ cd my-project

Now that we are in our current directory, which is "my-project," let's initialize our Git repository with the following command:

$ git init
  Initialized empty Git repository in /Users/user/myproject/.git/

We have managed to establish a repository that is functioning effectively and efficiently.

Adding New Files to Git Repository

After setting up your initial local Git repository, you may notice it is empty. To populate it with content, consider adding existing files or creating new ones using your preferred text editor. Once you have created or modified the files, save them and move them to the recently established directory.

Let's create an HTML file with some added styling to make it look more visually appealing:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello World!</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f8ff;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            text-align: center;
        }
        .container {
            background: #fff;
            padding: 20px 40px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #4CAF50;
            font-size: 2.5em;
            margin-bottom: 10px;
        }
        p {
            font-size: 1.2em;
            color: #666;
        }
    </style>
</head>
<body>

<div class="container">
    <h1>Hello World!</h1>
    <p>This is the first file in my new Git Repo.</p>
</div>

</body>
</html>

Adding a file

Now that Git is aware of the file but has not added it to our repository, let's understand the concept of file states within a Git repository.

In your Git repository folder, files can exist in one of two states:

  • Tracked: Files that Git recognizes and are included in the repository.

  • Untracked: Files that reside in your working directory but have not been added to the repository.

When you initially add files to an empty repository, they are considered untracked. To transition them to the tracked state, you must stage them by adding them to the staging environment following this command:

$ git add index.html

With the above command, I have just added a single file, so if you are willing to add more than one file, here are the commands. Improve

$ git add -A

This command adds all changes, including new, modified, and deleted files, to the staging area.

$ git add --all

This command is similar to git add -A and adds all changes to the staging area.

$ git add .

This command adds all new and modified files to the staging area but does not include deleted files.

The file has been added to the staging environment. To confirm this, let's check its status using the following command.

$ git status

The file has now been successfully added to the Staging Environment, representing a significant advancement in the development process.

Git Commit

Now that our work is complete, we are prepared to transition from the staging phase to committing changes to our repository. Commits are checkpoints that track our progress and modifications throughout the development process. Each commit represents a specific point in the project that can be revisited in case of bugs or the need for alterations. When committing, it is essential to provide a descriptive message. Clear messages accompanying each commit facilitate easy tracking of changes for yourself and others like this.

$ git commit -m "First commit!"

Creating a New Branch

While working on a project, it's often beneficial to isolate your changes on a separate branch. This allows you to develop new features or fix bugs without affecting the main project code. To create a new branch named "feature_branch" for implementing a new feature, use the following command:

$ git branch feature_branch

Switching Branches

To switch to an existing branch and start working on it, use the git checkout command followed by the branch name. For instance, to switch to the "feature_branch," we just created:

$ git checkout feature_branch

Merging Branches

Once you've completed your work on the (feature_branch) and then completed adding and committing, you can merge your changes back into the main branch (usually named "master" or "main"). This integrates the modifications you made on the feature branch into the main codebase.

Here's the command to merge the "feature_branch" into the "master" branch:

$ git checkout master
$ git merge feature_branch

Pulling Changes (Remote Repositories)

If you're collaborating with others on a project hosted on a remote platform like GitHub, you'll need to pull the latest changes from the remote repository. This ensures you're working with the most up-to-date codebase.

The git pull command fetches changes from the remote repository and attempts to merge them with your local branch automatically. Here's the syntax:

$ git pull origin master

In this example, "origin" is the default remote name for the repository you cloned from, and "master" specifies the branch from which you want to pull changes.

Pushing Changes (Remote Repositories)

Once you've made and committed changes locally, you can push your changes to the remote repository. This allows other collaborators to access your contributions. Use the git push command followed by the remote name and branch name:

$ git push origin master

Conclusion

This script provides a basic understanding of essential Git commands for version control. With practice, you'll become comfortable with these commands and be able to manage your code effectively, collaborate seamlessly with others, and maintain a clear history of your project's development. Remember, there are many other Git functionalities, and you can explore them further as you delve deeper into version control best practices.


Thanks for being one of my blog readers! Right before you go:

ย