Git Tutorial: Architecture, Commands, and More

People generally have this misconception that programmers are reclusive introverts who only sit in front of their monitors and write codes. They think programmers don’t interact with each other unless it’s necessary. Well, that’s not the case, at a given point of time, many programmers are working on a single code. This is where they need to communicate with each other to ensure everyone is updated with the proceedings.

While you may think that it is easy, it’s not. There are situations when programmers find it very difficult to communicate with each other due to various reasons. This is where tools like Git can come to their rescue. This article will educate you about Git and how it is useful for the programmers.

Learn all about Git from the Version Control System to Github and more with the Git Training Course. Enroll now!

Things that are covered in this Git tutorial include:

  • Version Control System
  • Distributed Version Control System
  • What is Git?
  • Git vs. GitHub
  • Git Architecture
  • Fork and Clone
  • Collaborators
  • Branch, Merge and Rebase
  • Commands in Git

So, let’s start.

Version Control Systems

There is a chance that you may already be using version control systems in your environment, like tools such as Microsoft's Team Foundation Services. But primarily the use of the version control system allows the people to have files that are all stored in a single repository. So, if you are working on developing a new program, such as a website or an application, you will store all the version-controlled software in a single repository.

Now, if somebody wants to make some changes to the code, they will check out all the codes present in the repository. Once the changes in the code are done, they are added to a new version. So, you keep adding on versions of the file whenever you edit the codes. The bottom line is that eventually, you will have people use your code, which will be saved in a centralized location. However, the challenges that you might face with these are when you are working with a large group that works simultaneously within a project

Distributed Version Control

In a distributed version control environment, you can be able to share the code across a team of developers. So, if two or more people are working on a software package, they need to share the code among themselves so that they are always in communication about what’s being done with the package. Here all the coders have the entire code on their local systems, and they try to keep it updated all the time. 

It is the role of a Distributed VCS server to ensure each client and developer have the latest version of the software, and each person can share the software and its updates in a peer-to-peer approach. So, if any change is made to the code, it can be shared with all of the development teams. Git is a tool that can be able to do a productive, distributed VCS environment. 

Now that you know what distributed version control systems are, and also that Git is a tool that aids distributed version control systems, you might be interested in knowing more about it. If that’s the case, let’s start with the basics.

What Is Git?

Git is a distributed version control tool used for source code management. GitHub is a remote server for the source code management, and your development team can connect the clients to that remote server. Here are some uses of the Git tool:

  • Git is used to tracking changes in the source code.
  • It allows multiple developers to work together.
  • Git supports non-linear development because of thousands of parallel branches.
  • It can handle large projects efficiently.

Git vs. GitHub

Let’s take a look at the differences between Git and GitHub:

Git-vs-github

Git Architecture

The working directory is the folder where you are currently working on your Git project. You start with a working directory where all your files are stored. Then add your files to the staging area where ideally ready to connect your files back to the main branch on your Git project. Once that’s done, you would want to push all the changes to a local repository where it will commit those files and make them ready for synchronization with the service. Then push your services out to the remote repository. An example of a remote repository is GitHub. 

Later when you want to update your code, you can pull the latest changes from the remote repository so that your copy of the code is always the newest version of the software that the rest of the team is working on. One of the things that you can do is, as you are working on new features within the project, you can create branches. You can merge those branches with the main line code which will ensure that your code is of the highest quality and allows you to add new features to it without breaking the core code. 

Git Concepts

Now that we have understood Git and it’s architecture let’s learn some Git concepts.

  • Fork and Clone  

    Both of these terms are quite old when it comes to development, but forking goes way back, long before we had distributed systems. To fork a piece of software is to take a project and create a copy of that project and then associate a new team around the project so that it becomes a separate project in entirety. On the other hand, a clone is identical with the same team and the same structure as the main project, so when you download the code, you are downloading the exact copy of that code with all the same security and access rights as the main code. You can again check the code back in since your code could potentially become the main line code in the future. 
  • Adding Collaborators

    With Git, you can add collaborators that can work on a project that’s essential for huge projects that have large teams. This works well when you have product teams where the team themselves are self-empowered. 
  • Branch in Git

    For instance, if you are working on a new feature, and you want the feature and the main version of the code to work simultaneously. Here you can create a branch of your code so that you can work on the new feature, whereas the rest of the team can continue to work on the main branch of the project. Once both are done, you can merge them.
  • Pull from Remote

    This concept of Git allows for pulling the program when the team is working from a remote location.
  • Git Rebase

    It is the concept of being able to take a project and re-establish a new start from the project. Once you choose to Rebase your project, anybody working on a separate branch will not be able to branch their code into the main-line branch. 

Now let’s a more in-depth look at each of these concepts. 

Fork and Clone

If you want to propose some changes to your project, you can fork it. When you fork a project or fork a repository, you take the main line branch and make it entirely offline to a local pool for you to be able to work from. You can receive the main line code and you can then work on a local version of the code that’s separate from the main line branches. 

Collaborators

It is the ability to have team members working on a project together. Let’s assume that if someone is working on a piece of code and you see some errors in the code. This is when you can add collaborators who can check and mend the codes. For adding collaborators, you can provide them permission in GitHub. You can choose the amount of permission you want to deliver to your collaborators. Sometimes it may be limited, while other times you can provide full control permissions. 

Learn all about collaborating between local and remote repository with the Git Training Course. Enroll now!

Branch in Git

Suppose you are working on an application and you want to add a new feature to it. To do that you can create a new branch and build the new feature on that branch. So, you will have your main application on the master branch, and then you can create a sub branch that runs in parallel and has your newly added feature. 

Once you are done developing the feature, you can merge them whenever required. There can be many branches in Git, which means that you can add various new features to your code in separate branches until you merge them all with the main branch.

Git Merge

Now let’s take a look at the merge process.

Git merge

As shown in the picture above, you start with the master branch, which is the blue line here and then you can also see a separate branch which has the new features. So, if we are to look at the process, the base commit of the feature branch is b. F is going to merge back into the master branch. It must be said that there might be too many divergent branches, but eventually, you can get everything merged into the master branch. 

Git Rebase

Suppose we have a similar situation where we have a branch that’s being developed parallel to the master branch, and we want to do a Git Rebase.

Git rebase

You are at stage c and what you have decided that you will reset the project so that everything from here on else is the standard product. However, this means that any work has been done in parallel as a separate branch will be adding a new feature along with this new rebased environment. It helps you reduce the amount of storage space that is required when you have many branches. 

Git Rebase is the process of combining a sequence of commits to a new base commit. The primary reason for rebasing is to maintain a long project history. When you rebase, you unplug a branch and replug it on the tip of the master branch. The goal of rebasing is to take all the commits from a feature branch and put it on the master branch. 

Pull from Remote

Suppose there are two developers working together on an application, the concept of having a remote repository allows the code to be accessed for the two developers. The remote repository will enable them always to stay updated with the latest version of the code. You can pull any changes that have been made from your forked remote repository to the local repository. You can use $ git pull<RemoteName><BranchName> command to be able to do that.

Commands in Git

Provided below are some of the commands used in Git and their purposes: 

  • git init - Initialize a local Git repository
  • git add/git add. - Add one or more files to the staging area
  • git commit –m “commit message” - Commit changes to head but not to the remote repository
  • git status - Check the status of your current repository and list the files you have changed
  • git log - Provides a list of all the commits made on your branch
  • git diff - View the changes you have made to the file
  • git push origin <branch name> - Push the branch to the remote repository so that others can use it
  • git config –global user. name “Roger” - Tell Git who you are by configuring the author-name
  • git config –global user.email roger.1@gmail.com - Tell Git who you are by configuring the author email id
  • git clone - Creates a Git repository copy from a remote source
  • git remote add origin <server> - Connect your local repository to the remote server and add the server to be able to push to it
  • git branch <branch name> - Create a new branch
  • git checkout <branch name> - Switch from one branch to another
  • git merge <branch name> - Merge a branch into the active branch
  • git rebase - Reapply commits on top of another base tip

Now that you know the Git commands used, check out the video below to get a basic understanding on how to start using these Git Commands -

There you go, these are some of the important and common commands used in Git. If you want to see how Git works, you can click here, which will redirect you to a video that teaches you how you can use Git.

Now you have an idea about what git is, its components, its commands, and how to go about learning them. It is imperative that you implement your knowledge in real life. If you are interested to know more about Git and learn more about it, you can visit Simplilearn’s Git Training Course, and master all the skills required for Git.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.