Git Your Way to Better Code: Exploring the World of Git

Ankit Khandey
5 min readMar 28, 2023

--

Greeting to my fellow readers. In this blog let us try to cover the world of git and how can we use it with examples. This blog is for both a beginner to understand and a professional to revise. So, let’s get started.

What is git and why we need it?

Git is an essential tool for developers and software engineers as it enables effective version control and collaboration on code. It allows users to track changes made to code over time, revert to previous versions if necessary, and merge changes made by multiple team members seamlessly. This makes it easier to manage and maintain code, reducing errors and ensuring consistency. Git also provides a safe and secure way to store code, with built-in mechanisms to protect against data loss and corruption. Overall, Git simplifies the development process, improves productivity, and ensures that code is organized and accessible for both current and future team members.

What is a version control system?

Version control system (VCS) is a software tool used by developers to manage changes made to code over time. It enables users to track and record modifications made to a file or set of files, and revert to previous versions if necessary. VCS also facilitates collaboration among team members working on the same codebase by allowing them to work on different versions simultaneously and merge their changes seamlessly. This makes it easier to manage and maintain code, reducing errors and ensuring consistency. VCS also provides a safe and secure way to store code, with built-in mechanisms to protect against data loss and corruption. Overall, version control system is an essential tool for developers to streamline their workflows and ensure that code is organized and accessible for both current and future team members.

Terminologies to get familiar with

  1. Repository (Repo): A repository is a storage space where all the files and history of changes made to the files are stored.
  2. Commit: A commit is a record of changes made to a file or a set of files in a repository.
  3. Branch: A branch is a separate line of development that allows you to work on changes without affecting the main codebase.
  4. Merge: A merge is the process of combining changes from one branch into another branch.
  5. Pull: A pull is the process of getting changes from a remote repository and merging them into your local repository.
  6. Push: A push is the process of sending changes from your local repository to a remote repository.
  7. Clone: A clone is the process of creating a copy of a repository in a new location.
  8. Fork: A fork is the process of creating a copy of a repository under your own account, allowing you to make changes independently.
  9. Remote: A remote is a connection to a remote repository that enables you to push and pull changes between your local and remote repositories.
  10. HEAD: HEAD is a reference to the most recent commit on the current branch.

Everyday Used cases with example

I. How to clone a repo

  1. Open your terminal or command prompt and navigate to the directory where you want to clone the repository.
  2. Copy the URL of the repository you want to clone from the remote location.
  3. In the terminal, enter the following command: git clone <repository URL>
  4. Press Enter to execute the command. Git will start cloning the repository to your local machine.

Now you have successfully cloned the repository from the remote location to your local machine. You can start working on the code and make changes as needed. Remember to push your changes to the remote repository when you are done.

II. Make a new branch.

  1. Ensure that you are currently on the master branch by running the following command in the terminal:
git branch

This command will show you the current branch and highlight it with an asterisk.

  1. If you are not on the master branch, switch to it using the following command:
git checkout master
  1. Create a new branch from the master branch using the following command:
git branch <new-branch-name>

Replace <new-branch-name> with the name you want to give to your new branch.

  1. Switch to the new branch using the following command:
git checkout <new-branch-name>

For example, let’s say you want to create a new branch called feature-branch from the master branch. Here's how you would do it:

git checkout master
git branch feature-branch
git checkout feature-branch

Now you are on the feature-branch and you can start working on your new feature without affecting the master branch.

III. How to commit and push the code

  1. Make changes to your code and save the files.
  2. Stage the changes to be committed using the following command:
git add .

This command will stage all the changes made to the files.

  1. Commit the changes with a commit message using the following command
git commit -m "Your commit message here"

Replace Your commit message here with a brief description of the changes made.

  1. Push the changes to the remote repository using the following command:
git push origin <new-branch-name>

Replace <new-branch-name> with the name of the branch you created earlier.

For example, if you made some changes to a file named example.html and you want to commit and push those changes to the feature-branch, here's how you would do it:

git add example.html
git commit -m "Updated example.html with new content"
git push origin feature-branch

This will commit and push the changes to the feature-branch on the remote repository. Now other team members can access the changes and review your code.

In conclusion, Git is a powerful and essential tool for version control that enables developers to work collaboratively and efficiently on projects. It provides a reliable way to track changes, revert to previous versions, and manage conflicts that arise when multiple team members are working on the same codebase. With Git, you can create branches, experiment with new features, and merge changes back into the main codebase with ease. Whether you are a beginner or an experienced developer, learning Git and mastering its basic concepts is a valuable investment in your career. So start using Git today, and take your development workflow to the next level!

--

--