Git Git, What is it?

What is Git?

Git is a management tool for a folder.

It is an open source project, initially developed by Linus Torvalds to help him tackle with maintenance and collaboration issues. It keeps track of changes and helps in merging updates from different collaborators. With features like commits and rollbacks, it also makes interaction between developers via projects easier.

What issues does Git solves?

Assume you have a folder of maths assignments. Some questions in are either unattempted or answered wrong. Your friend Ramesh volunteers to help and you send him a copy of your assignments folder. He later sends you back the with corrections.

Now you need a way to:

  • Know which questions were solved by Ramesh.
  • Compare your solution with his and learn about corrections he made.
  • Approve those corrections and merge them in your assignment.
  • Credit Ramesh for his help.

Maintaining updates from a single person looks like too much work right? All of this can be conveniently done by using Git.

An entry tour to Git

Git is a command line tool. That means you interact with it using your console. Various platforms like github, bitbucket, gerrit or gitlab utilize this tool to provide a collaboration platform. Let us learn about git, the backbone of all these platforms.

A Repository

A folder managed by Git is called a Git Repository.

A git repository consists of a .git folder. Git keeps all of its backups and changelogs there. You shouldn't manually create or edit it. Deleting this converts your repository back to a plain folder.

How do we create it?

Navigate to your project folder in console and write git init.
This initializes the .git folder and now your folder is a git repository.

gitinit.png

The Git Building

Once you are inside a git repository, multiple commands and features unlock. Before we talk about them, it is important to understand a few concepts.

Assume the git repository to be a fancy building. It has three floors.

  • The ground floor is called the working area. It's where the workers live. They work on the project features and hand over the updates to the guys on the first floor.
  • The first floor is the staging area. Here the guys collect the changes made by the ground floor. They group the changes, label them and finally send it to the second floor.
  • The second floor is the repository area. It contains the final project along with the history of changes and labels (commit messages) from the first floor guys.

gitfloors.png

  • git add is used to move a file from working to staging area.
  • git commit is used to move a file from staging to repository area.

When you modify a file, you need to stage it and tell git to track the changes. Git can now track the changes done to the file and revert them in case something happens. Once you are happy with the changes made you can commit them. A commit finalizes the changes and keeps a record of it.

There are multiple flags available to add messages, revert commits, etc which you can read in detail.

Git Push, Pull and Clone

What's above a git building? Clouds!

What you were working with till now was the local version of the project. To make it accessible to the others, you need to upload it to a server. This is where git pull and push come in.

Git has ability to sync its changes with the server copy of it. The server copy of the local repository is called an upstream.

  • To push the changes to the upstream git push is used. It updates the remote repository with any commits made locally to a branch.
  • git pull is used to updates your local repository with changes made to the upstream.
  • git clone creates a local copy of a project that already exists remotely. The clone includes all the project’s files, history, and branches.

gitpushpull.jpg

KeyNote: With the git building perspective, push and pull simply sync commits from the second floor with the cloud or the upstream.

Branches

With git you can work with multiple versions of the same project without explicitly creating many folders. Switching between multiple versions becomes seamless.

Multiple versions of a git repository in parallel are called branches. You can work on them individually without affecting the other.

With branches you unlock powerful features such as merge.

  • git branch shows all your branches.
  • git branch BranchName creates a new branch BranchName.
  • git checkout BranchName switches to that branch.
  • git branch -d BranchName deletes that branch.

This is an introduction to git for beginners and not a tutorial. I suggest you to go through the git book if you want to be proficient in it.

Start using git if you haven't yet, all it took was a try to convince me :)