What is Git? How to use it? Why to use it? Explained in Depth!!

Explained in Depth!!Jatin VarlyaniBlockedUnblockFollowFollowingFeb 5In this post I’m going to explain you what is Git and how you can quickly get started with git.

Since many of you might have heard of git very often but never understood what it exactly means and how to use it.

You have stumbled upon the right place.

Git is a Distributed Version Control System (VCS) which is originally developed in 2005 by Linus Torvalds (Creator of Linux) & Open Source in nature i.

e freely available to use.

It’s the most popular and most used tool right now.

Let’s understand what does Version Control means here:Version control systems : Are a category of software tools that help a software team manage changes to source code over time.

Version control software keeps track of every modification to the code in a special kind of database.

If a mistake is made, developers can turn back the clock using rollback or revert and compare earlier versions of the code to help fix the mistake while minimizing disruption to all team members.

Distributed Version Control System.

Let’s quickly get started with using GitFirst lets quickly grab the git from the official site which is Git SCM https://git-scm.

com/After installing from here, Open your Terminal and typegit –versionIt will show you the version of git installed in your local machine.

Now go ahead and create a folder and Initialize a git Repository.

mkdir git-sample-repogit initThe git init command adds a Local Git Repository to the project folder.

Now create a simple Hello-World.

txt in your folder and add some content in it.

touch Hello-World.

txtStaging & Committing the ChangesUnlike the other systems, Git has something called the “staging area” or “index”.

This is an intermediate area where commits can be formatted and reviewed before completing the commit.

One thing that sets Git apart from other tools is that it’s possible to quickly stage some of your files and commit them without committing all of the other modified files in your working directory or having to list them on the command line during the commit.

git add * ORgit add Hello-World.

txtThe command git add * adds all the files in the staging area.

Now let’s commit the changes made by us.

git commit -m "Added the First Commit"“Added the First Commit” is the commit message here.

Enter a relevant commit message while the -m are the options.

A shorthand version of Staging & Committing isgit commit -a -m "Initial Commit"Git Status and Git Log and DiffNow modify the Hello-World.

txt file by adding few more lines.

StatusUse git status to find out information regarding what files are modified and what files are there in the staging area.

git statusThe status shows that Hello-World.

txt is modified and is not yet in the staging area.

git add Hello-World.

txtgit commit -m "Added Some Content in File"LogUse git log to print out all the commits which have been done up until now.

git logThe log shows the author of each commit, the date of the commit, and the commit message.

Diff ToolUse git diff to show unstaged changes between your index and working directory.

It is a multi-use Git command that when executed runs a diff function on Git data sources.

These data sources can be commits, branches, files and more.

The git diff command is often used along with git status and git log to analyze the current state of a Git repo.

git diffBranchesThe Git feature that really makes it stand apart from nearly every other SCM out there is its branching model.

Up until now we have not created any branch in Git.

By default, Git commits go into the master branch.

What is a branch?Git branches are effectively a pointer to a snapshot of your changes.

When you want to add a new feature or fix a bug — no matter how big or how small — you spawn a new branch to encapsulate your changes.

So currently the master branch is a pointer to the second commit “Added Some Content in File”.

Diagram shows the master branch.

We always create branches for Features in our Project.

Create a New BranchCreate a new branch called sample using the following command:git branch sampleThis command creates the sample branch.

We are still in the context of the master branch.

In order to switch to the sample branch.

use the following command:git checkout sampleNow we are in the sample branch.

You can list out all the branches in local using the following command:git branchLet’s do some Commits in the New BranchModify Hello-World.

txt by adding the following snippet:Hello World Adding some Content Adding some Content from sample Branch.

Seems Nice!!Now stage and commit using the following commands:git commit -a -m "Sample Branch Commit"This commit was done in the Sample Branch, and now Sample Branch is ahead of Master Branch by 1 commit — as the Sample branch also includes the 2 commits from the master branch.

You can verify the commit history in Sample Branch using:git logMergingThe git merge command lets you take the independent lines of development created by git branch and integrate them into a single branch.

Currently, Sample Branch is ahead of the Master by 1 commit.

Let’s say that now we want all the code in the Sample Branch to be brought back to the Master Branch.

This is where git merge is very useful.

It will combine multiple sequences of commits into one unified history.

First go back to the master branch:git checkout masterThen run the merge command:git merge sampleAfter running these 2 commands, the merge should be successful.

In this example, there are no conflicts.

But in real projects, there will be conflicts when a merge is being done.

Resolving the conflict is something which comes with experience, so as you work more with Git you will be able to get the hang of resolving conflicts.

Run git log now and you will notice that the master also has 3 commits.

The Remote Git RepositoryUntil now, we have been working only in the local repository.

Each developer will work in their local repository but eventually, they will push the code into a remote repository.

Once the code is in the remote repository, other developers can see and modify that code.

Here we will be using GitHub it is like your Google Docs, except you can create & save your version of the code offline, before ‘pushing’ it to be saved online.

Alternatives such as Bit Bucket are available.

Setting up a Remote Repository.

Go to https://github.

com/ and create an account.

After registering in the GitHub homepage, click on Start a Project to create a new Git repository.

Give the repository a name and click “Create Repository”Give the name as git-sample-repo This will create a remote repository in GitHub.

Enter this command on the terminal and navigate to directory where local project is stored.

git remote add The git remote add command takes two arguments:A remote name, for example, originA remote URL, for example, https://github.

com/Jatin-8898/git-sample-repo.

git# Set a new remotegit remote add origin https://github.

com/Jatin-8898/git-sample-repo.

git# Verify new remotegit remote -vorigin https://github.

com/Jatin-8898/git-sample-repo.

git(fetch)origin https://github.

com/Jatin-8898/git-sample-repo.

git(push)FetchThe git fetch command downloads commits, files, and refs from a remote repository into your local repo.

Fetching is what you do when you want to see what everybody else has been working on.

git pull and git fetch commands are available to accomplish the task.

You can consider git fetch the 'safe' version of the two commands.

It will download the remote content but not update your local repo's working state, leaving your current work intactSyntax: git fetch <remote> <branch>Example: git fetch origin samplePullThe git pull command is used to fetch and download content from a remote repository and immediately update the local repository to match that content.

Merging remote upstream changes into your local repository is a common task in Git-based collaboration work flows.

The git pull command is actually a combination of two other commands, git fetch followed by git merge.

Syntax : git pull <remote> <branch>Example: git pull origin masterPushThe git push command is used to upload local repository content to a remote repository.

Pushing is how you transfer commits from your local repository to a remote repo.

Syntax : git push <remote> <branch>Example: git push origin masterIf you created a Readme.

md file while creating a New Repository then make sure you first pull then push.

Additional CommandsClonegit clone is a Git command line utility which is used to target an existing repository and create a clone, or copy of the target repository in your system.

git clone <url>ConfigThe git config command is a convenience function that is used to set Git configuration values on a global or local project level.

git config –global user.

email "your_email@example.

com"Remove the DirectoryTo remove the director from the project folder.

Use this commandgit remove -r dirResetTo Reset staging area to match most recent commit, but leave the working directory unchanged.

git resetReset staging area and working directory to match most recent commit and overwrites all changes in the working directory.

git reset –hardCongratulations!!!Now you know the basics of how to use Git, so go ahead and explore more!Feel free to connect with me on my LinkdIn account https://linkedin.

com/in/jatin-varlyani-127290150/You can also follow me on Github: https://github.

com/Jatin-8898.

. More details

Leave a Reply