Version Control with Git

Let's start with Version Controlling

Imagine a Project on which multiple Developers are working at the same time. Now somebody changed a piece of code and the whole software crashed, or some old feature was working properly and people used to love that, now that is changed and you want need the previous one, in these type of scenarios if you can go back to the previous code base how convenient this would be. With this solution Version control comes in. It is just the snapshot of the state of a code base in a particular time. Whenever you want to go to previous state, it is ready there just have fetch it again. Version Control is of Three types Local, Centralized and Distributed.

Local Version Control System (LVCS)

Local Version Control system allows to host the whole code base locally, it will take the snapshots of the code at a given time and whenever you want to go back to that state you can. It is fast because you have the code on your system, no need to fetch from another code server (Source Code Repository). But it has a problem of Single Point of Failure. This technique is used by Revision Control System.

Centralized Version Control System (CVCS)

In Centralized Version Control the whole code is hosted in a single server. Every Project Contributor makes change in the code base from their system and merge with the main code base in the server. Now the problem arises with the reliability of this server. What if this server fails, everything is just gone unless you are keeping another copy of this code base. What if people want to work offline from their own system and when they are connected, they want to merge the code then. There is no choice for that. Some Centralized Systems like Concurrent Versions System, Perforce, and Subversion used to do this type of work. Some benefits with this system is you are having centralized code base which is easy to maintain from top level, For very secure Projects you need to keep the code centralized, in this cases Centralized Version Control works best.

Distributed Version Control System (DVCS)

Unlike Centralized type, in Distributed Version Control every Project Contributor have the copy of whole code base in their system. In centralized the project contributors don’t have the copy of the whole code base, in Distributed everyone have the copy, that’s make the whole system very convenient to work in offline mode or remotely, No problem in losing of whole code base, Working flexibility in own system. These days Most used Distributed Version Control System is Git. Mercurial, Bitbucket are also there but Git is the most used one.

Git

Git was Developed by Linus Torvald and Linux Team in 2005. BitKeeper was also a DVCS which was used in Linux those days, but due to an internal problem with BitKeeper team, Linux decided to build its own DVCS. That bring Git into existence. The goal was to build a tool which will be fast, can be used for large projects, simple design and can used to work with multiple number of branches.

Working Process of Git

Git is different than other CVCS in way of data management. Other CVCS tracks all the files individually in a linear way and stores the change occurred to the file at a given time. This is called as Delta Based Version Control. Instead of this Git takes a snapshot of all the files at given time. So, in every commit it parallelly stores the state of each file. Every Commit is built over the previous Commit like a Stack. A Head Pointer points the latest commit, which is the current state of the files. A Commit Hash is Generated for each commit, and we can move the Head to the previous states with that Commit Hash.

Working Steps of Git

Three steps are involved to take the snapshot of a project.

  • Modified
  • Stagged
  • Committed

When you modify a file, like adding a line of code or deleting a particular file Git tracks them as a modified file. Then you need to stage that file, in that stagged area only Git will Commit those changes and after committing Git will save the snapshot.

Installing Git

Git can be installed from this link: https://git-scm.com/downloads After installing execute

git --version

to check the current version of your git and if this shows any error then git is not installed.

Git Commands

Git can be used as GUI and CMD both ways. Commands are the most used one, Now some Commands to start with git...

  • Initializing Git
git init

This will initialize a. git named hidden directory in your system. It contains the Files for Head, Branches, Commits, Configurations and the essentials files for working of Git.

  • Staging a file Now after you created a file on the Directory where . git is initialized that file will be treated as modified file in Git. Now to Stage the file
git add .

for all the files or,

git add < THE FILE NAME >

this command is used.

git status

Command will show the state of files, are modified or are they stagged. This can be executed before and after git add command. After that this file can be Committed.

  • Committing a file
git commit -m"YOUR COMMIT MESSAGE"

This command is used to commit the changes on that staging area and these files will be saved.

  • History Now to view the history of the commits
git log

or

git reflog

can be used. This will display all the commits that are made.

  • Uncommitting Change If something is committed by mistake now you want undo that, for that
git revert < COMMIT ID >

this will revert that commit or undone that commit.

git reset < COMMIT ID >

Revert was undoing that commit only, Reset will undo all the changes after that commit or take you the previous state of that given commit.

  • Branches Branches are the linear snapshots of a current version. If want to add any feature to that code, you can create a Branch from the Main Branch (Default Branch) and do your own stuff. After completing you will merge your work with the main one this way the main piece of code will be intact from your changes during any unsuccessful piece of work.
git branch < YOUR BRANCH NAME >

Will create a branch which will contain the files of the Main Branch, or the Copy of the files. Here you can do your own things.

git switch < YOUR BRANCH NAME >

or

git checkout < YOUR BRANCH NAME >

will take you to that Branch.

  • Merging your work After a successful code change you can Merge your branch with the Main Branch of that Project. To Do that first go to the main Branch and
git merge < YOUR BRANCH NAME >

execute this to merge with the Main Branch.

git rebase < YOUR BRANCH NAME >

This will do similar to merge but the difference is in merge you are merging your commits to the main branch and then Head Points to that commits on main branch but your-created-branch commits is also stored in the database, in rebase you will shift the your-created-branch in front of main not the commits, the whole branch is shifted. This solves the issue of storing the others set of commits.

  • Working with Remote Repository Most of the time Projects Works are done on Remote Code Repository. For that the Git need to be configured to connect with Remote Repository. For that we generally start with,
git git config --global user.name < YOUR USERNAME >
git config --global user.email < YOUR EMAIL >

these commands are used to set your profile on Git with which people will get know that you have done or committed a set of work. Now after that Git need the URL of that repository. which can be added with

git remote add origin https://REPOSITORY-HOST/YOUR-USERNAME/NAME-OF-THE-REPOSITORY.git

origin is the standard alias name of the remote repository on your system, but it can be anything.

git remote -v

This will show your current remote repository.

git remote set-url origin https://YOUR-USERNAME:access-token@REPOSITORY-HOST/YOUR-USERRNAME/REPOSITORY-NAME.git

If the repository is private then Git need access that with a access token provided by your Repository Host.

git pull origin < BRANCH-NAME >

This will fetch all the work on the remote repository and merge that on your repository. To do this individually for some cases

git fetch origin < BRANCH-NAME >

This will fetch the changes form remote but not merge it on your repository. After that git merge command can be used. But Git Pull does the same. After doing your work locally Now you want to Push those changes on Remote Repository. For your own Repo you can just do that easily. But some other`s Repo you have Create a Pull Request after this.

git push origin < BRANCH_NAME >

with this command your changes will be pushed to the Remote Repo.

git clone < REPOSITORY-URL >

This command is used clone any repo in your local system.

To work on Other People`s Repository you have to fork that on your remote repo, then you will made the changes needed after that you will create a pull request to merge your changes on Main Project.

Conclusion

Now these are the Most used Git commands. There almost 150+ Git commands which are used as needed, but these are for the everyday use. This is the Version Control System Evolution which started from Revision Control System in locally to Working the Biggest Source Code Projects using Git.