bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat

Overview of Git 

Be it a Designer, be it a Creator or be it a Developer, we all deal with projects and files every day. Our primary work-cycle revolves around creating a file, saving it, editing or making required changes and saving it again. In this Git Tutorial, we will be focusing on the life of a Developer, and therefore understand What is Git.

Tutorial Git

Tutorial Git

Git is a small, yet very efficient version control tool. Git helps both programmers and non-programmers keeping track the history of their project files by storing different versions of their project files.

Prepare yourself for the Top DevOps Interview Questions And Answers!

Watch this Git Tutorial Video

Git

Git has become significantly popular among the developers in last few years. If you are a part of developer’s world it is expected that you know Git. You must have come across the term Git many times before as well. Don’t worry if you have never used Git, because in this tutorial you will be guided through:

But before diving deep into the understanding of Git, let us discuss little bit about the basics of version control systems and why we need them in the very first place.

Git helps developers by keeping track of the history of their codes files by storing them in versions on its own server repository i.e. GitHub. Git has the functionality, performance, security and flexibility that most teams and individual developers need.

Git

Below given graph depicts the rate of popularity that Git has gained over the years (from 2004-2018):

Git graph

Now let us go ahead in this Git tutorial and learn why Git is used and what are the factors that make Git so unique.

Become Master of DevOps by going through this online DevOps training in London.

Why Git Version Control?

There are some facts which makes Git so popular:

  • Work offline: Git provides users the very convenient options of working both online and offline. With other version control systems like SVN or CVS we have to have access to internet to connect to the central repository.
  • Undo your Mistakes: Git allows you to undo commands for almost every situation. You get to correct the last commit for minor change, also you can revert a whole commit for unnecessary changes.
  • Restore deleted commits: This feature is very helpful while dealing with large projects, while trying out experimental changes.
  • Secure: Git provides protection against secret alteration of any files and helps maintaining an authentic content history of the source file.
  • Performance: Being distributed version control system it has an optimized performance due to its feature like committing new changes, branching, merging and comparing past versions of the source file.
  • Flexibility: Git supports different kinds of nonlinear development workflows, for both small and large projects.

How Git works?

Now that we have learnt what is git in this git tutorial, we will move ahead and discuss how Git works. Let us start learning the git basics, starting with the lifecycle of Git.

Git Lifecycle:

  • Local working directory: First stage of a Git project lifecycle is the local working directory where your project resides, which may or may not be tracked.

git lifecycle

  • git lifecycle
    • Initialization: To initialize a repository we give the command Git init. With this command, we have made the Git aware of the project file in our repository.
    • Staging area: Now that our source code files, data files, configuration files are being tracked by Git we will add the files that we want to commit to the staging area by Git add command. This process can be also called as indexing, we can also say that index constitutes of files added to the staging area.
    • Commit: Now that our files that need to be committed are ready, we will commit them using Git commit -m “your message”.

We have successfully committed files to our local repository. But how does it help in our projects? The answer is, when we will be doing collaborative projects, files may need to be shared with our team members. That is when the next stage of Git lifecycle occurs, which is GitHub, where we publish files from local repository to remote repository. And how do we do that? We do that by using Git push command.

Commit

Commit

Here, we have discussed the life cycle of Git project, where we came across some commands like Git init, Git add, Git commit, Git push etc. Further in this Git Tutorial, we will be explaining these common commands in more details.

But before that lets go through the Git Installation and Configuration of Git.

Go through the Best DevOps Course in New York to get clear understanding of DevOps.

Install Git

For installing git on windows, click on the link given below:

Windows

For Git installation in mac, click on the link given below:

Mac

For Git installation in Linux, click on the link given below:

Linux

We have successfully installed Git in our system. Before discussing common Git commands in this Git Tutorial let us set up the environment in our systems.

Environment Setup:

  • Create a GitHub account
  • Configuring Git
  • Creating a local repository

Create a GitHub account:

  • Go to https://Github.com
  • Create one GitHub account
  • Login

After following these steps your GitHub page should look like this

Create a GitHub account

Create a GitHub account

Configuring GIT:

To tell Git who you are, run the following two commands:

Configuring GIT

Configuring GIT

Creating a Local Repository:

To begin, open a terminal and move to where you want to place the project on your local machine using the cd (change directory) command. For example, if you have a ‘projects’ folder on your desktop, you’d do something like:

Creating a Local RepositoryCreating a Local Repository
We can go to the Git folder and see the new directory that we have just created.
Git folder
Git folder
Now we are all set to start working on the Git commands.

Learn more about DevOps in this DevOps training in Sydney to get ahead in your career!

Git Commands:

We will divide the Git commands into two primary categories.

  • Local- Git init, Git touch, Git status, Git add, Git commit, Git rm
  • Remote- Git remote, Git pull, Git push

let us discuss the local commands first.

Local Commands:

Git init: Use the Git init command to initialize a Git repository in the root of the folder:

Gitinit

Git touch: To add files to a Project Git touch can be used. Let us see how we add a file to the Git repository we have just created

Step-1: First, we will create a new file with command touch.

Step-2: Then, we will see the files present in our master branch.

Git touch

Git status: After creating the new file, you can use the Git status command and see the status of the files in the master branch.

git status

git statusGit add: Now we will add the humble.txt file to the staging environment by using Git add before going ahead to the staging and see the change using Git status.

git add

git add

Git commit:
There you go you have successfully created your first commit.

git commit

git commit

If you have any doubts or Queries related to DevOps, get it clarifies from DevOps Experts on DevOps Community.

Git clone:

Git cloneGit clone

Before we move on to Branching and Merging operation, let us discuss the remote commands like remote, push, pull in Git, but for that, we will have to create a new repository in our GitHub account:

Step-1: Go to your GitHub account.

Step-2: Create a new repository.

Create new repository

Create new repository

Once you are done with filing up the new repository form you should land on a page like the following.

humble

Now we are ready to operate remote commands in our repository that we have just created.

Interested in getting an industry-recognized certification in DevOps? Enroll in Intellipaat’s DevOps Course in Bangalore now!

Remote Commands:

To push an existing repository to remote repository from a command line follow the commands given below.

Git remote:

git remote

git remote

After that give the following command:

command

command

Push:

Git push origin EnterBranchName

Don’t get confused by the word origin. Let me tell you what exactly that command is referring to:

Instead of writing the whole Git URL, like: Git push Git@Github.com:Git/Git.Git yourbranchname we can give the following command: Git push origin yourbranchname.

push

push

Now let us look at our repository:

look at our repository

look at our repository

Pull:

When it comes to syncing remote repository pull command comes in handy.

Let us take a look at the steps that leads to pulling operation in Git.

  • remote origin
  • pull master

Refer the steps given below:

pull

pull

Now that we have learnt the workflow of Git with the help of common commands, let us take a look at Branching, Merging and Rebasing and, also how and when to use them.

pull 2

pull 2

Branching:( Git checkout, Git branch)

Now that we are a bit familiar with let’s take a step further and discuss one of the most important operations of Git branching.

branching

branching

Every node in the figure above represents commits.

Note: Remember that for “y”, “x” is the base.

Let us say that you want to modify something but don’t want to make any change in the main project that is where we take a branch out of the master branch.

For instance, if you are on the master branch and want to create a new branch to add a feature in the main project.

Step-1:  Run Git checkout -b <new branch name>

Step-2: Use Git branch now to confirm that your branch is created

run git

run git

You can see from the above image that we have created a branch called branch1 and we automatically got landed in the new branch. Again, just to see which branch we are currently in we sent another command, Git branch (* mark before the branch name defines the current branch).

Note: Now, you must be wondering how to change branch in git to master branch right? Use the following command:

git checkout master

Now, you know the most important skill set of a DevOps Engineer. But, do you know, DevOps Engineers are among the highest paid professionals in the technology domain? so join DevOps training in Hyderabad!

Merging: (Git checkout, Git add, Git log, Git merge, merging conflicts, rebasing):

Now that we have learned how to create a branch and work on a branch, let us take a look at merge feature of Git by merging a branch into the master branch.

Further in this git tutorial, let’s understand merging with an example. Say, we have a master and a feature branch.

Merging git

Merging git

The merge commit represents every change that has occurred on feature since it branched from master.

merging git 2

Note: Even after merging we can go on with our work on both the master and feature branch independently.

Let us see how to perform merging using Git bash:

Step1: Create a new branch called branch2

branch2

Step2: create a new file in that branch.

new file in that branch

Step3: Add changes from all tracked and untracked files

Note: refer the following command Git add attributes

Git add attributes

In our case, we have given the command as Git add -A and after that, we will commit one sentence as shown below in this git tutorial.

git a

git a

Step4: Check the last 3 logs by giving the command as Git log -3

step 4
We have created another branch on our master branch. Now we will see how to perform merging.

  • But before that we let us get inside the master branch, by giving the following command

Git checkout master.

  • After that, we will perform merging by

Git merge branch2

git merge

Now we have successfully merged the branch into master. Let us take a look at how it looks inside the master branch using the following command.

Get in touch with Intellipaat for a comprehensive DevOps Training and be a certified DevOps Engineer!

Git log –graph:

git log graph

See the graph one the left. But why the graph is linear?

In our ProjectGit master branch we did branching and committed as well. But after branching master branch had not encountered any commits. So, after merging we have seen the linear.

Let us perform Git log –graph with more than one commits in both master and developers branches that we have created.

git log graph 2

Here you can see the graph with commits on both master and developers branch. Red parts of the graph indicate merging operation.

Merging Advantage:

  • Merging allows parallel working in a collaborative work.
  • Saves the time of manual merging

Merging Disadvantage:

  • Merging conflicts may occur.

Now that we have successfully learned to branch and merge with Git and GitHub. Now in this git tutorial, let us look at another very important Git operation i.e. rebasing.

Git Rebase:

When the project becomes relatively large, the commit log and history of the repository becomes messy. So, we use rebasing. Rebasing will take a set of commits, copy them and store them outside your repository. That helps us maintain a linear sequence of commits in our repository.

Now in this Git Tutorial, , let us take the same example of master and feature branch. Here, we will rebase master branch. And see what happens.

Rebasing

Note: In rebasing the base of feature branch gets changed and the last commit of the master branch becomes the new base of the feature branch.

rebasing 2

Now let us perform rebasing in Git bash.

rebasing image

Advantage of rebasing:

  • cleaner project history

Disadvantage of rebasing:

  • For collaboration workflow re-writing project history can be potentially catastrophic.

Now that we understood what merging and rebasing is in this git tutorial, let us see where to use merge and where to use rebasing.

Golden rule of Git merging and rebasing:

When to use git merge:

  • Use merge while working on public branches

When to use git rebase:

  • Use rebase while working on local branches

As we have mentioned earlier in this git tutorial, one major disadvantage of merging is merge conflicts, which can be a backbreaking work during a team project. Let us understand how merge conflict occur and how to resolve this issue.

Git Merge Conflict:

This drawback of merging operation in Git can be explained with a simple example shown below.

merging

Say, we have two branches of the master branch, branch1, and branch2. And two developers working on these two branches independently on the same code file.

As we have shown in the figure above, they have made below mentioned modifications:

  • Developer-A added a function called function2 in our main code.
  • Developer-B added a different function called function3 to the same code file.

How to merge these two modifications now? That is where our merging conflict occur.

Solving merging conflicts:
  • Manually resolve it in merging tool.
  • File locking doesn’t allow different developers to work on the same piece of code simultaneously. It helps to avoid merge conflicts, but slows down development.

Similarly rebasing has rebasing conflicts as well, which can also be solved with the help of Git merging tool.

Let us look at a few Git workflows briefly, before we end this tutorial, so that you get an idea which Git workflow to choose for your team project.

Comparing Git Workflows:

Comparing Git Workflows

Why is it important to choose the right Git workflow for team project?

Depending upon the team size, choosing the right Git workflow is important for a team project to increase the productivity.

Now in this git tutorial, let us look at different Git workflows:

different Git workflows

Centralized workflow:

In Git centralized workflow, only one development branch is there, which is called master and all changes are committed into that branch itself. Below given figure  in this git tutorial depicts Centralized Workflow:

Feature Branching Workflow:

In feature workflow, feature development takes place only in a dedicated feature branch. Below given figure in this git tutorial depicts Feature Branching Workflow:

Feature Branching Workflow

Git Workflow:

In case of Git workflow, instead of a single master branch, this workflow uses two branches. Here, master branch stores the official release history whereas our develop branch acts as an integration branch for features.

Below given figure depicts Git Workflow:

Git Workflow

Git Workflow

Forking Workflow:

In case of forking workflow, contributor has two Git repositories: one private local and one on public server-side.

This brings us to the end of the Git tutorial. In this git tutorial, we have gone through Version Control Systems and its different types, basics of Git, terminologies related to Git, Git installation in windows, Linux, mac environment, setting up and working on GitHub repository. We have also discussed workings of some important operations in Git by the end of this git tutorial.

Git and its popularity:

Git and its popularity

Git and its popularity
Git is one of the most important version control systems that has experienced a significant growth rate and popularity over the years. Git plays a very important role in both developers and the non-developers world.  If you are a part of soft developer’s world, it is expected that you know Git. So, having a good grasp of Git is very important.

Even Microsoft has acquired Git very recently. This opens up more possibilities for developers’ community. So, this is the right time to learn Git.

We hope you have understood Git and got the answer to what is Git and what can it do.

To have a more deeper level of understanding of different Version Control Systems with hands-on    exercise you can also refer to this DevOps Certification Training Course by Intellipaat.

Previous Next

Download Interview Questions asked by top MNCs in 2019?

"0 Responses on Git Tutorial - Learn Git"

    Leave a Message

    100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
    top

    Sales Offer

    Sign Up or Login to view the Free Git Tutorial - Learn Git.