Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take two weeks Trial! Try Now

Version Controlling with GIT using GitBash

Agenda

Version controlling is the most required feature when we work in an enterprise environment. This is because the code we write is not our code. If you are working for a client, it is the property of the client, so it becomes the liability of the developers. To stay on the safer side, you require to track modifications to the code repository.

To track changes, a version control system is needed. The most efficient, lightweight, and freeware version controlling system is GIT.

GIT is a version controlling system, created on the top of Unix shell scripting and works on the command line with some of the pre-defined commands. One should learn these commands, which are easy to learn.

It comes with GIT Bash, a shell scripting-based command-line tool where we can write commands and have full version control over the repository. A repository can be a full project or a blank folder. It is up to the requirement of the Offshore software development firm.

Step 1: Navigate to https://git-scm.com and download GIT for your operating system.

version-controlling

Once you have downloaded it, it will download the executable file and follow the on-screen instructions to install GIT on your system.

Step 2: Create a repository. You can use an existing project folder to be a repository. I am using a blank folder as a repository.

version-controlling
version-controlling

I have created a new folder named ‘DemoApp’ in C:/DemoApp.

Once we create this folder, it is a blank folder. Now must create it as a GIT repository so that GIT can track the changes inside this folder.

Step 3: Find GIT Bash in your start menu and click on it.

version-controlling

Step 4: Once you click on it, it will open a colourful command line tool, where you can write commands.

Now, move to the directory where you created the new folder.

version-controlling

Step 5: Now, we will write ‘git init’ command, and it will make this folder a GIT repository.

version-controlling

As you can see, once we run the init command, it starts it in the repository and the blue color '(master)' is displayed. This indicates that we are in the Master branch of our GIT repository.

Open the repository folder, and you will see that it is blank. But, go to View Menu, and select the Hidden Items checkbox and you will be able to view it, there is now a hidden .git folder.

This folder maintains the complete history and tracks all the changes in that repository.

version-controlling
version-controlling

Step 6: Now, we need to set global configurations like setting up username, and email id.

Follow the commands below:

version-controlling

Step 7: Lets add some new files to our repository. Once you add, you can see then as untracked files in git bash. For this, you need to enter ‘git status’ command.

version-controlling

As you see in the above screenshot, I added three files to my repository, and when I hit the ‘git status’ command, we can see those three files in the ‘red’ color under the category of untracked files.

This means that even if you try to commit them to the repository, they will not be added to your commit. So, you need to add them to the tracked files.

Step 8: Now add the untracked files to the tracked files using ‘git add .’. This period(.) at the last of the command means all untracked files to be taken care.

version-controlling

You can see above that now, after adding the files to the tracked files, and if we hit the ‘git status’ command, then those files are now in ‘green’ color and are shown under the category ‘Changes to be committed.

Step 9: Now the time has come to ‘Commit’ the changes to our repository. Commit states that we are done with the changes and now we want to finalize them in the repo.

Use command ‘git commit -a -m “Your message”’.

In the above command (-a) stands for all tracked files, and (-m) stands for any user message that you want to save with that commit.

version-controlling

Now our changes are committed to our repository.

Step 10: For example, I want to know the commits that I made in the past. So, the command ‘git log’ can help us.

version-controlling

You see in the above screenshot, the commit that we made in Step 9 is shown as history.

Step 11: Now, we want to create a Branch based on the data in the ‘Master’ branch. So, follow the below screenshot.

version-controlling

In above,

  • In first command, we create a branch with name ‘testbranch’.
  • Since you see in blue colour, we are currently in master branch. Now, I want to move to our new branch, we use checkout command as shown.
  • When you get switched to other branch, then your new branch will be visible in blue colour.
  • In third command, I intentionally wrote incorrect command, so git throws error and it also shows some suggestion. So, it is sometimes a helpful feature.
  • In the fourth command, ‘ls’ is used to list all the items in the current directory. We created our new branch from master, so the master had 3 files, so the new branch also shows a list of three files.

Step 12: Below are a few self-explanatory screenshots. I just added some files, add them to the tracked files, committed them, and moved to another branch and a few more examples.

Have a look at them, I am sure, you will understand easily.

version-controlling
version-controlling
version-controlling

Recent Blogs

Categories

NSS Note
Some of our clients
team