Version control is an essential tool for software development that enables developers to keep track of changes to their codebase and collaborate effectively with other team members. Git is one of the most popular version control systems, widely used by developers worldwide.

Git works by creating a history of changes made to a project, allowing developers to revert back to previous versions of their code if needed. In this blog post, we will introduce you to some of the basic Git commands that you can use to get started with version control.

Initializing a Git Repository

The first step in using Git is to initialize a repository. This creates the necessary structure to keep track of your changes. To do this, navigate to the directory where you want to create your Git repository and run the following command:

Fish
git init

This command will create a new repository in your current directory, and you can then start tracking changes to all files in that directory.

Adding new changes

After making changes to your code, you need to add them for commit. This is done using the git add command. For example, if you want to stage all changes made to files in the current directory, you can run the following command:

Fish
git add .

Alternatively, if you want to stage changes made to a specific file, you can run:

Fish
git add <path to filename>

Committing Changes

Once you have added your changes, you can commit them to your Git repository using the git commit command. Think about a commit as a snapshot of your code, you will always be able to return to this point in time. This command requires a commit message to be provided to describe the changes made. For example:

Fish
git commit -m "Added new feature"

Now your changes are safely saved, you can do any modification to your code and you will always be able to go back to this commit.

Viewing Commit History

To view the commit history of your repository, you can use the git log command. This will show you a list of all the commits (snapshots) that have been made, along with their commit messages and other details. For example:

Fish
git log

For example, this is the history of out K-Means repository:

commit 709c755aed814f5fba44509de62d6bfc3cf00d08 (HEAD -> main, origin/main)
Author: Pablo Jimenez Mateo <gef3233@gmail.com>
Date:   Thu Jan 26 11:33:04 2023 +0100

    feat: Add image

commit c176a237357b70c485c19ceac9c4a6ca4bce0e9e
Author: Pablo Jimenez Mateo <gef3233@gmail.com>
Date:   Thu Jan 26 08:31:09 2023 +0100

    feat: Updated README

commit 48cd6c73014a8cca0c5cdd0937887ebaabf8f9e3
Author: Pablo Jimenez Mateo <gef3233@gmail.com>
Date:   Wed Jan 25 13:05:41 2023 +0100

    chore: Some small changes

commit 12c3927dd1374a0cdba34b1bd770d68e32078eac
Author: Pablo Jimenez Mateo <gef3233@gmail.com>
Date:   Tue Jan 24 14:06:07 2023 +0100

    chore: Remove unused imports

commit 8c56e58c4eaf58ef51770849a764a67b8a90efa4
Author: Pablo Jimenez Mateo <gef3233@gmail.com>
Date:   Sun Jan 22 23:00:53 2023 +0100

    feat: Now KMeans will stop if centroids converged

commit c9c81013293f289776f3e4a536aa1cf20803ece2
Author: Pablo Jimenez Mateo <gef3233@gmail.com>
Date:   Sun Jan 22 22:40:59 2023 +0100

    feat: The KMeans code is complete

commit 18e636fea2aec280a207d65f2caa3ec03425abd9
Author: Pablo Jimenez Mateo <gef3233@gmail.com>
Date:   Fri Jan 20 18:22:48 2023 +0100

    feat: Some tests

commit e70a760d6b07471e6c916ee145886cccc1bfc9d4
Author: Pablo Jimenez Mateo <gef3233@gmail.com>
Date:   Fri Jan 20 17:46:34 2023 +0100

    first commit

Branching

One of the key features of Git is branching, which allows you to work on different versions of your code in parallel. To create a new branch, you can use the git branch command. For example:

Fish
git branch new-feature

This will create a new branch called new-feature. To switch to this branch, you can use the git checkout command:

Fish
git checkout new-feature

You can then commit changes and they will only be saved into this branch.

Merging Changes

Once you have made changes to a branch and want to merge them back into the main branch, you can use the git merge command. For example:

Fish
git checkout main
git merge new-feature

This will merge the changes made in the new-feature branch back into the main branch.

Integrating with GitHub

Integrating your local Git repository with GitHub can provide a centralized location for your code and facilitate collaboration with other developers. Basically, your Git repository will be available from everywhere. In this section, we will cover the basic steps required to integrate your local Git repository with GitHub.

Creating a Repository on GitHub

The first step in integrating Git with GitHub is to create a new repository on the GitHub website. To do this, log in to your GitHub account and click on the “New” button in the top left corner of the page. Give your repository a name and a brief description, and choose any other settings as needed. Once you have created your repository, you will see its URL in the address bar of your browser.

Pushing your local changes to the remote

Fish
git remote add origin git@github.com:<username>/<repository>.git
git branch -M main
git push -u origin main

This will submit all your changes into the `main` branch.

Pushing Changes to GitHub

When you commit your changes, those changes will only be on your local computer, you then need to push them for them to be available on GitHub:

Fish
# After you have done git add, commit
git push

Now if something happens to your local repository you will always be able to recover a copy from GitHub.

Conclusion

In this blog post, we have covered some of the basic Git commands that you can use to get started with version control. We also show how to save your local changes to GitHub, the most popular version control website.

Categories: Practical

2 Comments

Shreyash Somvanshi · April 24, 2023 at 6:38 PM

Thank you for such precise and informative tutorial for getting started with Git.

    David Andrés · April 27, 2023 at 9:15 AM

    Many thanks for your feedback Shreyash! 🙂

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *