A Beginner's Guide to Git And GitHub

What is Version Control

Version control, also known as source control, is defined as the process of tracking and managing changes made to software code. Version control systems are software tools that help software teams manage changes to source code over time. As development environments have accelerated, version control systems help software teams work faster and smarter.

When projects grow very large and multiple people are working on the same, the project maintainers need to know who made changes to the project, when they made them, and what were the changes. Among many utilities of the version control system, the major is, if an application in production that runs into any error, the application can be rolled back to the stable version very easily.

There are two types of version control systems - distributed and centralized

Distributed Version Control System

The distributed version control model is similar to the centralized. The only major difference is, instead of one single repository which is the server, here every single developer or client has their server and they will have a copy of the entire history or version of the code and all of its branches in their local server or machine. Every client or user can work locally and disconnected which is more convenient than centralized source control and that’s why it is called distributed. After making the changes they can commit to the project upstream, as by then the project might have expanded more.

Centralized Version Control System

In centralized source control, there is a server and a client. The server is the master repository that contains all of the versions of the code. The code from the master repo must be fetched to work with the same. To do so, the client communicates with the server and pulls the current version of the code from the server to their local machine. Then, changes are made to the code and after that, we commit those changes directly to the master repository. So whatever work is done involves the central or master repository. As there will be just one repository that will contain all the versions of the code and different branches of the code, it is referred to as the Central Version Control System

What Is Git

Git - Logo Downloads

Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It is the most popular and easy-to-use tool used for version control. Git was originally developed in 2005 by Linus Torvalds, the famous creator of the Linux operating system kernel.

In Git, every developer's working copy of the code is also a repository that can contain the full history of all changes, which is unique for distributed source control systems.

Setting Up Git on Personal Computer

It might be very clear by now, the purpose of using the version control system. So now, it is time to try it ourselves.

Head to the Git SCM official page, download the preferred installer and install it on your computer.

Then right-click anywhere on your computer and you should see options like this:

Click on the Git Bash Here option, and it will open a terminal in front of you. We need to set up some initial information in the newly installed git, like username and user email. Execute the following commands with your personal information.

git config --global user.name "Your full name"
git config --global user.email "Your Email ID"

As it is well understood, the first line sets your name and the second line will set your email id.

That's it! Now you are ready to execute commands and explore.

Essential Git Command

git status

This gives the current git status of the present working directory, like if it is a git repo or not, or the untracked or modified files that are needed to be committed.

git init

This command initializes an empty repository in the present working directory.

Note that, if there is already a git repository in the folder, executing this command will reinitialize the repository, and hence all the previous history will be deleted. So, it is recommendable to use the git status command to ensure the current status of the working directory.

git add file.txt

This command stages the file named file.txt whose history or version we want to track.

git add . 
git add --a

Both of these commands stage all the files and folders in the present directory.

git commit

This command saves the changes of the files to the repository. This code triggers a VIM editor, where you can describe the changes made and write any message.

git commit -m "Your commit message"

This code serves as a one-liner for the above code.

rm -rf .git

This command deletes the repository. That means all the history and versions of the code, files, and folders are removed.

.gitignore

This is a special file, that contains information about the files and folders whose history or version we are not willing to track.

git log

It shows the previous commits descriptively made to the code. Press q to exit the log.

git log --stat

It shows the commits shortly and descriptively.

git restore --file.txt
git reset --file.txt

This command reverses the git add function, i.e, it unstages the staged file.

git commit -a -m "Commit Message"

You can skip the staging part of the trackable files and directly save the changes to the repository. But this cannot commit an untracked file, i.e., no new files can be

git checkout -b develop

This command creates a separate branch named "develop" from the master branch so that any changes brought to the new branch do not affect the main code base.

You can come back to the master branch by git checkout master

git branch

Lists all the branches that are associated with a particular repository.

git branch -d develop

This command deletes the specified branch.

git merge develop

This command merges a branch with the master branch.

git config --global alias.st status

This code helps to set the alias to a command in git. The next time you type git st, the command will correspond to git status.

GitHub and Working with remote repositories

GitHub Guide for Beginners | Analytics Vidhya

GitHub is a cloud-based service that provides to host your git repositories online. Not only that, but this service also allows individuals to collaborate on other's repositories and projects. The repositories of GitHub are called remote repositories. You can access and manage files of these remote repositories from your personal computer, through something called a remote URL. We need to add this remote URL to the repository and you can push your changes to them.

git remote add origin <link>

This code does the same for you. You just need to get the link from the repo. You can easily get it from the initial screen when you create a remote repository.

git push origin master

This code helps to push the changes and the files that occurred on the master branch to the remote repository. To upload another branch to the remote repository, just change the master to the corresponding branch name.

git remote remove origin
git remote rm origin

There may be some cases when you want to delete an old remote URL to the remote repo and use a new one instead. So you can delete the remote origin and freshly add it again.

However, you can update the existing remote URL by

git remote set-url origin <new url>
git clone <repo ssh link>

This is essential when you want to clone another project with a record of all its previous changes for contributing to the project or exploring the project or anything like that. The command creates a clone of the repository on your personal computer.

In the future, I will bring another blog, where I will discuss the Git workflow and process of contributing to some projects. There I will be discussing remote repositories in further detail.

Reference

  1. GeeksforGeeks

  2. Atlassian Blog

  3. Git Official Page

Did you find this article valuable?

Support ARITRA BHATTACHARJEE by becoming a sponsor. Any amount is appreciated!

Â