5 Minutes Guide for GIT Basics and its Commands

What is GIT?

GIT is one of the most popular open-source version control systems. It is very fast and is the most used Version Control System. It allows programmers to track changes at different stages of the project.

GIT is a distributed version control system that enables a team of developers to collaboratively work on a single project; a copy of the project is made available on everyone’s local computer.

How Does GIT Work?

GIT allows us to track changes. When using GIT the code moves through 3 stages:

  1. Working Directory(local) is where you do all your changes to the file.
  2. A staging area is where you can track changes made in the working directory.
  3. Repository (Usually Remote) is where GIT stores all the changes permanently.

Installing GIT

To install GIT you can follow the instructions at: https://git-scm.com/downloads

Else you can check if GIT is already installed by running “git version”

Configuring GIT

You can check the current configurations by running git config --global --list

Additionally, you can configure the username and email.

GIT Lifecycle

  1. You can clone the GIT repository as a working copy or initialize an empty repository​.
  2. You can modify the working copy by adding/editing files.​
  3. If necessary, you can also update the working copy by taking other developers' changes.
  4. You can review and commit changes. If everything is fine, then you push the changes to the repository.​

GIT Basic Commands

  • git init

The git init command creates a new GIT repository. It can be used to convert an existing, unversioned project to a GIT repository or initialize a new, empty repository.​

Making Changes

Add something to the repository or create a new file.

  • git add

git add or git add <filename> adds the changes to the staging area to be committed.​

  • git commit

​git commits –m 'commit message' creates a commit out of the changes that have been added. ( commit is the term used for saving changes)​

  • git status

The git status command displays the state of the working directory and the staging area.​ It lets you see what changes have been staged, what haven't, and which files haven’t been tracked by GIT.​ The status output does not show you any information regarding the committed project history.​

You can optionally add the -sb flag to view the short form of the same output

  • git log

The git log tool allows you to view information about previous commits that have occurred in a project.​ These commits are shown in reverse chronological order (the most recent commits first).​ Use git log --oneline to list a summary of all commits.

Creating Branches

A branch in GIT is simply a lightweight movable pointer to one of these commits.​ The default branch name in GIT is master which allows one to try out new ideas. If the idea doesn’t work, throw away the branch no need to undo changes to master.​ If the idea works, then it can be merged with the master branch. There is only ONE working directory​.

  • git branch

This lists all the branches and the current branch you are on.

  • git branch <branchName>

This creates a new branch with the given name.

Use GIT checkout -b <branch> to create a new branch with the specified name and switch to it.

  • git checkout <branchName>

This switches the branch to the given name.

  • git diff <branchName>

git diff is a multi-use GIT command, when it is executed it runs a diff function on GIT data sources. The git diff command is often used along with GIT status and GIT log to analyze the current state of a GIT repo.​ It displays the changes that were introduced. It compares the current branch with the given branch name.

  • git merge

Merging is GIT's way of putting a forked history back together again. The git merge command lets you take the independent lines of development created by the git branch and integrate them into a single branch.​

Remote Repositories

  • git clone

git clone is a GIT command-line utility that is used to target an existing repository and create a clone or copy of the target repository.

​ ​

  • git push

The 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.​ Remote branches are configured using the git remote command.​

Syntax - git push origin <branchName to push to remote

  • git pull

The git pull command is used to fetch and download content from a remote repository and immediately update the local repository to match that content.​ Once the content is downloaded, git pull will enter a merge workflow. A new merge commit will be created and HEAD updated to point at the new commit.​

Resolving Merge conflicts - GitLive

When two or more people are working on the same file there can be cases where merge conflicts can happen. There are lots of ways and tools to resolve such conflicts. GitLive is an extension that allows programmers to resolve conflicts even before they occur. GitLive is supported across most IDEs. There are two ways to resolve the conflicts using GitLive:

  1. Live: See others' local changes in the gutter of your editor and get notified the moment you make a conflicting change.
  2. Patch: View diffs of other contributors' local files and cherry‑pick individual lines, files, or complete working copies.

GIT offers other features as well like codeshare where you can make voice and video calls directly from your editor, and codeshare to see each other's cursors. You can also see when your fellow developers are online and which repos, branches, and files they are working on.