GIT - Version Control

GIT - Version Control

What is Git? 
Git is a free, open-source version control software. It was created by Linus Torvalds in 2005. This tool is a version control system that was initially developed to work with several developers on the Linux kernel. 
We could say Git is a cloud repository which is is mostly use to store codebase. 
There are a number of software development organizations across the globe so as the developers. In live scenarios on a Project multiple developers working in parallel from different locations worldwide. so they need a version control system like Git to make sure that there is no code conflicts between them and everybody should get the updated code due to their working timezone conflicts. 
Also on the same line, it is often that requirements might get changed in these projects. Hence, a version control system allows developers to revert and go back to an older version of their code. 
The branch system in Git is the good utility that allows developers to work individually on a task (for e.g. One branch -> One task OR One branch -> One developer). Basically think of Git as a small software application that controls your code base, if you’re a developer. 

Git Repositories 
A Repository or Repo is the area where we put our files. It is a project that contains multiple files. In our case a repository will contain code-based files. 
If we want to start using Git, we need to know where to host our Repositories. 
There are two ways to host repositories - one is online i.e. on cloud and other is offline i.e. self installed on the server. 
There are three popular Git hosting services: GitHub (owned by Microsoft), GitLab (owned by GitLab) and BitBucket. We’ll use BitBucket as our hosting service. 

Track changes in your code across versions 
When multiple people collaborate on a project, it’s hard to keep track of revisions — who changed what, when, and where those files are stored. 
GitHub takes care of this problem by keeping track of all the changes that have been pushed to the repository. 
We can have a version history of your code so that previous versions are not lost with every iteration. It’s easy to come back to the previous version and contribute your work. 

How to use Git? 
Bitbucket account creation 

We need to create account on BitBucket. 

Git installation 

We need to install Git's tools on our computer. We will use CLI to communicate with Git. 

For Ubuntu: 

1. Update your packages. 

sudo apt-get update 

2. Next, install Git with apt-get 

sudo apt-get install git 

3. Verify that Git is installed correctly, do 

git --version 

4. Run the below commands to set a default username and email 

git config --global user.name "Git User" 
git config --global user.email "[email protected]

Working with Git Projects 
Create the repository, clone it to your PC, and work on it. 
This involves creating a new repository on Git, cloning it to your computer by creating a directory with project name, work on your project - do coding, and commit & push it back to your Git repo. 
Create a new repository by clicking the "Repository" button on the Git (Bitbucket) Repositories web page. 

Put the project name,  
put the name for your repository,  
choose the access level,  
choose to include readme or not,  
select the branch* name (Master), 
under advanced tab add a small description. 
select programming language for your project. 
click on create repository. 

*branch name - There can be a number of branches in your project/repository. Master branch is the parent branch of all the other branches. Build from Master branch will get deployed in Production for Release. 

Now clone this newly created repository on your local system. Click on “clone” buttone against the repository, copy the link pop-out. Now go to terminal, navigate to your project folder that you have created earlier and paste the link there on the terminal. It will ask for the authentication, give your password*. 

Bitbucket password* - you can create your bitbucket password by navigating to your profile, peronal settings, app passwords, create app password. 

To clone a repository means that you're taking a repository that’s on the server and cloning it to your computer – just like downloading it. 

Use the below command on your terminal.  

cd / 
git clone  

Authenticate 

This command will make a local copy of the repository hosted at the given address. 
Now, your repository is on your local system.  

cd  

Now, in this local repo or folder you can create files that means you can actually code there by creating a project in tool (e.g. Eclipse IDE) and save your work locally. To save them in a remote place — like Git (Bitbucket) – you have to “commit” your work/changes to the cloud repo. To do this follow below 

cd // 

There are 4 steps to do commit: 'status', 'add' , 'commit' and 'push'. All the following steps must be performed within your project/ repo. Let's understand them. On the terminal in your repository run the below commands one-by-one and check the output. 

"git status": This command will check the files you have modified. It will show the list of the files that you have worked upon/ modified with their complete path. Just the commnd on terminal like below 

git status 

This command will show the list of the files in red colour suggesting that the files are present on the local system and not yet pushed to master branch. 

"git add": With this command you can add all files you want to upload/ want to add to the cloud repo. Run below command  

git add [FILENAME] #run this command to add specific files 

e.g. git add sample.html 

or 

git add . #run this command to add all the files in staging area that you seen as in the output of git status command 

once add completed, again run the git status command to ensure that the files you have added to staging area have been added successfully. The output of the git status command now will be the list of files in green colour that suggest local files have been added to staging area. 

"commit": Once you have added the files to the repo, we need to commit them into the staging area while writing a suitable message to explain what we have done. This message will be useful later if we want to check the change history. 

git commit -m "Added sample HTML file that contain basic syntax" 

"git push": Now you can put your work to actual cloud Git Repository. To do this you need to push the changes to the branch in the repository. 

git push origin master 

Now, if you go to our repository on the Git (Bitbucket) web page, you can see the sample.html file that you have pushed master branch. 

NOTE: Sometimes when you’re using Git commands in the terminal, it can lead you to the VIM text editor (a CLI based text-editor). So to get rid of it, you have to type 

:q! 
and ENTER. 

Other Git Commands 

git pull: This command is use to pull all the latest changes that have been commited by another developer. As we discussed earlier, at a time on a project a number of developers work together, to not to get in some conflicting situtation it is always the good idea to commit your changes timely and pull the changes onto your local system commited by other developers as well in timely manner. 

git branch: to check in which branch you are currently working. It will show the list of all the branch you have on your local system - the clone you have taken earlier (e.g. master, integration, testing) & your local branches as well (story you are working upon).  

git checkout : This command is use to switch branch on you local system. For e.g if you were working on integration branch and now you wanted to switch to testing branch, this command will do it for you. 

git checkout -b : This command is use to create new branch on your local system. 

git merge origin/: This command is use to merge your branch into another Master/Integration/Testing branch. 

git branch -d : This command is use to delete any specific branch. 

Conclusion
In this blog we understood about the Git (Bitbucket), repositories, branches and how to use the git commands.