Header Ads

Version Control : All you need to know



Hey coders :), hoping that you are doing good in your coding journey. We are again here with an another informative article, Or you can say informative tutorial.
In this tutorial, we will be learning about version control systems, Git and GitHub etc. and discovering the various abilities of these tools.

Before starting, let's take an example for your understanding :-

Imagine you and your friends are working on a project, let say a weather app. All the features have been added and now the backend work is going on for proper functioning of the app. Now, you strike with an idea to add a new feature, so you took the code and did some changes in it. Now during testing of the app, the app crashes due to that feature. Now, You want to go back to that point where the feature was not added, How will you do that ? Will you delete that changes that have been done to the code, Or you will write a new code for the app .
It would be very difficult not only for you but your whole group to undo the changes. And after that, your friends will not take you in any group work because of that app crash.  

A few Challenges arise considering the above example :-

1) making it challenging to track changes and revert to a stable state when a new feature causes issues.
2) reverting to a previous stable state becomes a manual and error-prone process. It's challenging to pinpoint the exact changes that introduced the crash.
3) Accidental deletions or overwrites may occur, leading to valuable code being lost.
4) The app crash incident may strain team dynamics and erode trust, as team members might lose confidence in the ability to manage and collaborate on the project effectively.

To overcome these, Version Control system was introduced, first time in 1972 by Bells Labs, called SCCS (Source Code Control System)
In this tutorial, we will learn about most popular version control systems : Git and GitHub

Let's dive into it and uncover it completely :-

- INTRODUCTION
- INSTALLATION & CONFIGURATION
- BASIC COMMANDS :-
            1. REPOSITORY
            2. FILES
            3. BRANCHES
            4. MERGE
            5. FORK
            6. MISC
- CONCLUSION


Introduction

While doing collaborative work, tracking changes and maintaining a historical record of modifications to code is essential. Version control systems (VCS) are like a life saver in achieving these goals. One of the most popular and widely used VCS is Git, a distributed version control system known for its speed, flexibility, and powerful branching capabilities.
It allows developers to manage their source code efficiently, track changes, and collaborate seamlessly with teams. Whether you're a beginner or an experienced developer, understanding Git and its integration with platforms like GitHub is fundamental to modern software development.

Installation

Here are the basic steps for Installation of git common operating systems:

Windows:
- Download the Git installer from the official Git website.
- Run the downloaded executable file and follow the installation wizard.

macOS:
- Git comes pre-installed on macOS. You can verify the installation by opening a terminal and typing
git --version.

Linux:
- Use your package manager to install Git. For example, on Ubuntu, run sudo apt-get install git.

Configuring Git

Once Git is installed, you need to configure it with your name and email. Open command prompt (terminal in case of macOS) and run the following commands, replacing the placeholders with your information:



Basic Commands

Let's go step by step :-
you created a folder for your project in you local machine "demo".

1. Repository :

- Create new Repository : To start using Git in your project, navigate to the project's root directory in the terminal and initialize a new Git repository using git init command. This command creates a hidden .git folder in your project directory, which contains all the necessary Git data to manage version control.

- Clone and existing repository : If you want to work on an existing Git repository from a remote location (like GitHub), you can clone(copy it) it to your local machine using
git clone <repository name> OR git clone <repository url>

2. Files : Now suppose you have made some files containing your work, they can be of any type (html, css, java, etc.). But you have saved them in your local machine, then you have to add them on some remote platform so that all the collaborative members can see it.
there is a protocol that we have to follow to make this happen, let's understand it step by step by an example :

- adding a file to stage : assume you are in a hotel, checking the menu to order something. after seeing the menu, you have shortlisted some items in your brain. these items will be called stage files. stage is (as the name suggests) a virtual space where all the files that have been changed or are not been uploaded on the remote platform resides. before uploading these files, we have to add them to stage area, by using git add <file.ext> command.
you can add all the files on to stage at one go using git add . command.

- commit a stage file : To give final order, you will call the waiter and tell him the names of items you wanna order. now these ordered items will be called as committed items. Now you have added your files onto stage, here comes the part where you have to commit these stage files. Committing the files will save the unsaved files. it will also store the changes that have been made to the stage files. you can commit the stage files using git commit -m "message" command. you can give any text in the "message", this will be stored as the change that have been made.

-push committed files : Now you know what you want to order, so you will call the waiter and tell him the items you want. Similarly, after committing the staged files, we will finally add them to github platform using git push origin main command. this will upload all the files from your local machine to the remote platform i.e. GitHub. 

- status of files : Now you have ordered, you have to see what is the status of your items. like how many of them are ready, etc. you can see the status of files that are being changed, but not added to the stage by using git status command. 

I made a text file "file.txt" and made changes to it, it is giving me a text in red " modified ". the files that are ready to be committed will be shown in green color.

- git log : Now, you have eaten your meal. you have bill on your table. the bill will contain all the items you ordered, total money you have to pay, etc. similarly, git log command shows all the changes you made to the existing files. 

the changes we made are shown as " CodinGates " that we gave in "message" text.

3. Branches : A branch as the name suggests is a part of the workflow of your project. 
the dots in purple and green are originated from a branch of the main workflow of the project in blue dots. taking the previous example, when you want to add a new feature or do some changes for testing, then basically you create your own branch containing the copy of the project files. Any changes you made, will not reflect in the original project until and unless you commit them in the original files.
let's take a look at some branch related commands :

- create a branch : to create a new branch, we use git checkout -b <branch name> command. In this branch, you can add and test your own changes made to the project files.

- delete a branch : to delete a branch, we use git branch -d <branch name> command.
Note : we cannot delete a branch when we are currently in it. this will give an error : 

- navigate between branches : to navigate between branches, we use git checkout <branch name> command, this will change our branch.

we were in " branch1 ", we navigated from " branch1 " to main branch "master". git branch command is used to see all branches in the repository.

- rename a branch : to rename the branch we are currently in, we use git branch -M <newname> command.
 
we were at branch "master", we renamed it as "main".

3. Merge : We made a branch to add some features, the features are working fine and now we want to add them to the original project files. this is called merging out code with the original project. let's see how we can do it :-

- merge using CommandLine : we can merge one branch to another branch using git merge <branchname> command. the "branchname" is the name of the branch to which we want to merge the branch we are currently in.
we merged the "main" branch (in which we are currently in) with other branch "branch1". it will overwrite all the changed files of "main" over original files in "branch1".

- merge using github : we can also merge two branches using github. to do this, we have to create a Pull request. Pull request basically means we are making a request to merge our code with the original files. to make a pull request, there is an option on github website to create it. first it will check if the branch can be merged automatically. otherwise the contributors (admins) of the project will review the request and allow it to merge or not.

5. Fork : using fork, we can copy any other repository and save it in our account for some purpose. we can fork any repository using a fork button on github website. 

6. Misc :

- pull : We have successfully made our project, but it is currently stored on our github account, we want to store it in our local machine. to do this, we use git pull origin main command, this command downloads all the files from github and copies it to our local directory.
- revert changes : when we commit the stage files, the changes are also stored in the log file of git. all the changes are saved with a token key
using this token key, we can revert back that state before the change was made. to do this we use
git reset <token key> command. 

Conclusion

Git and GitHub makes it easier to work in collaboration based projects. It makes it simpler to communicate between all the members. You've navigated the vast sea of Git and GitHub, gaining invaluable skills that will propel your coding journey forward. Git, with its version control magic, has now become your faithful companion, allowing you to travel through time in your projects. GitHub, the bustling hub of collaboration, is where your code finds its community.



HAVE ANY DOUBTS ? Feel free to ask in Comment section
HAPPY CODING :)

No comments

Feel free to ask your doubts :)

Powered by Blogger.