GIT Introduction and Basic Commands
What is GIT?
Git is a free, open source distributed version control system tool designed to handle everything from small to very large projects with speed and efficiency. It was created by Linus Torvalds in 2005 to develop Linux Kernel. Git has the functionality, performance, security and flexibility that most teams and individual developers need.
Version Control is the management of changes to documents, computer programs, large websites and other collection of information.
There are two types of VCS:
- Centralized Version Control System (CVCS)
- Distributed Version Control System (DVCS)
Centralized version control system (CVCS) uses a central server to store all files and enables team collaboration. It works on a single repository to which users can directly access a central server.
Please refer to the diagram below to get a better idea of CVCS:
The repository in the above diagram indicates a central server that could be local or remote which is directly connected to each of the programmer’s workstation.
Every programmer can extract or update their workstations with the data present in the repository or can make changes to the data or commit in the repository. Every operation is performed directly on the repository.
Even though it seems pretty convenient to maintain a single repository, it has some major drawbacks. Some of them are:
- It is not locally available; meaning you always need to be connected to a network to perform any action.
- Since everything is centralized, in any case of the central server getting crashed or corrupted will result in losing the entire data of the project.
This is when Distributed VCS comes to the rescue.
These systems do not necessarily rely on a central server to store all the versions of a project file.
In Distributed VCS, every contributor has a local copy or “clone” of the main repository i.e. everyone maintains a local repository of their own which contains all the files and metadata present in the main repository.
You will understand it better by referring to the diagram below:
As you can see in the above diagram, every programmer maintains a local repository on its own, which is actually the copy or clone of the central repository on their hard drive. They can commit and update their local repository without any interference.
They can update their local repositories with new data from the central server by an operation called “pull” and affect changes to the main repository by an operation called “push” from their local repository.
The act of cloning an entire repository into your workstation to get a local repository gives you the following advantages:
- All operations (except push & pull) are very fast because the tool only needs to access the hard drive, not a remote server. Hence, you do not always need an internet connection.
- Committing new change-sets can be done locally without manipulating the data on the main repository. Once you have a group of change-sets ready, you can push them all at once.
- Since every contributor has a full copy of the project repository, they can share changes with one another if they want to get some feedback before affecting changes in the main repository.
- If the central server gets crashed at any point of time, the lost data can be easily recovered from any one of the contributor’s local repositories.
What Is Git?
GIT is the most widely used open-source VCS (version control system) that allows you to track changes made to files. Companies and programmers usually use GIT to collaborate on developing software and applications.
A GIT project consists of three major sections: the working directory, the staging area, and the git directory.
The working directory is where you add, delete, and edit the files. Then, the changes are staged (indexed) in the staging area. After you commit your changes, the snapshot of the changes will be saved into the git directory.
Everyone can use GIT as it is available for Linux, Windows, Mac, and Solaris.
Features of Git:
- Free and open source
- Supports non-linear development
- Easy Branching
- Distributed development
- Compatibility with existing systems or protocol
- git config
Used to set user-specific configuration values like email, username, file format, and so on. To illustrate, the command for setting up an email will look like this:
The –global flag tells GIT that you’re going to use that email for all local repositories. If you want to use different emails for different repositories, use the command below:
- git init
Creates new local GIT repository. The following Git command will create a repository in the current directory:
Alternatively, you can create a repository within a new directory by specifying the project name:
- git clone
Used to copy a repository. If the repository lies on a remote server, use:
Conversely, run the following basic Git command to copy a local repository:
- git add
used to add files to the staging area. For example, the basic Git following command will index the temp.txt file:
git add temp.txt
Below command adds a file to the staging area:
Below command adds one or more files to the staging area.
- git commit
Creates a snapshot of the changes and save it to the git directory.
Note that any committed changes won’t make their way to the remote repository.
- git diff
Used to show differences between files.
Below command shows the file differences which are not yet staged:
Below command shows the differences between the files in the staging area and the latest version present:
- git reset
Below command unstages the file, but it preserves the file contents.
This command undoes all the commits after the specified commit and preserves the changes locally:
Below command discards all history and goes back to the specified commit.
- git status
This command lists all the files that have to be committed.
- git rm
This command deletes the file from your working directory and stages the deletion.
- git log
This command is used to list the version history for the current branch.
Below command lists version history for a file, including the renaming of files also.
- git show
This command shows the metadata and content changes of the specified commit.
- git tag
This command is used to give tags to the specified commit.
- git branch
This command lists all the local branches in the current repository.
Below command creates a new branch:
Below command deletes the feature branch:
- git checkout
This command is used to switch from one branch to another.
Below command creates a new branch and also switches to it.
- git merge
This command merges the specified branch’s history into the current branch.
- git remote
This command is used to connect your local repository to the remote server.
- git push
Below command sends the committed changes of master branch to your remote repository:
Below command sends the branch commits to your remote repository:
Below command pushes all branches to your remote repository:
Below command deletes a branch on your remote repository:
- git pull
This command fetches and merges changes on the remote server to your working directory.
- git stash
Below command temporarily stores all the modified tracked files:
Below command restores the most recently stashed files:
This command lists all stashed changes.
Below command discards the most recently stashed changeset:
This command shows the graphical interface for a local repository. Simply run: gitk
- git gc
Cleans unnecessary files and optimizes the local repository.
- git prune
Deletes objects that don’t have any incoming pointers.
- git fsck
Performs an integrity check of the git file system and identifies any corrupted objects.
- git rebase
Used to apply certain changes from one branch to another.
You may also be interested in...
- Slack Integration with C#
- Xamarin Forms: Getting Started
- Part 1: Introduction to C#
- Part 2: Arrays and Function in C#
- Part 3: OOPs Concepts in C#
- Part 4: Constructor/Destructor in C#
- Part 5: Abstract class in C#
- Part 6: Indexer,Delegates, Anonymous Method, Lambda expression in C#
- Part 7: Collections in C#
- Part 8: Generics in C#
- Part 9: Attributes in C#
- Part 10: Sealed/Partial class in C#
- Part 11: MultiThreading in C#