An Introduction to git

dilshan ukwattage
6 min readMay 14, 2021

What is Version Control?

Version control is a system that keeps track of changes to a file or group of files over time so that you can go back in time and recall specific versions later. So ideally, we can place any file in the computer on version control. A Version Control System (VCS) allows you to restore files to a previous state, the entire project to a previous state, review modifications over time, see who was last modified something that might be creating a problem, who implemented an issue and when, and more. If you use a VCS, you will usually restore your files if you make a mistake or lose them.

Centralized vs Distributed

The main difference between these two is that Centralized VCSs keep the history of changes on a central server from which everyone requests the latest version of the work and pushes the latest changes to. On the other hand, on a Distributed VCS, everyone has a local copy of the entire work’s history.

What is git?

Git is by far the most commonly used modern version control system nowadays. Git is a mature, regularly maintained open source project developed by Linus Torvalds, the prominent developer of the Linux operating system kernel, in 2005. Git is used for version control in a huge variety of software projects, both commercial and open source. Git is an example of a distributed version control system (DVCS) since it has a distributed architecture. Rather than having a single repository with the entire version history of the software, as is the common for once-popular version control systems like CVS or Subversion (also known as SVN), Any developer’s working copy of the code is also a repository in Git, which can contain the complete history of all changes. In addition to being distributed, Git has been designed with performance, security and flexibility in mind.

GitHub is a cloud-based hosting service that lets you manage Git repositories. Bitbucket is also a Git-based source code repository hosting service owned by Atlassian. GitLab is a web-based DevOps lifecycle tool that provides a Git-repository manager providing wiki, issue-tracking and continuous integration and deployment pipeline features, using an open-source license, developed by GitLab Inc.

Getting started with git

1. How to install git

Installing Git it is very simple. You have to got to the git official website https://git-scm.com/ and have to download the setup based on your operating system. Then you can install it. Then if you want you can use git — version to check your version.

# Check version
$ git -- version

2. Create a local git repository

When creating a new project on your local machine using git, you’ll first create a new repository (or often, ‘repo’, for short). git init command is used to start a new repository.

# make directory a git repository
$ git init

3. Adds a file to the staging area.

So what is staging area?

Our working area is where files that are not handled by git. These files are also referred to as “untracked files.” Staging area is files that are going to be a part of the next commit, which lets git know what changes in the file are going to occur for the next commit. The staging area is like a rough draft space, it’s where you can git add the version of a file or multiple files that you want to save in your next commit (in other words in the next version of your project).

# adding one file
$ git add <file name or directory>
# adding more than one file
git add *

4. git Commit

The git commit command captures a snapshot of the project’s currently staged changes. Committed snapshots can be thought of as “safe” versions of a project — Git will never change them unless you explicitly ask it to.

git commit -m "Commit message in quotes"

5. git status

The status command is used to display the state of the working directory and the staging area. It allows you to see which changes have been staged, which haven’t, and which files aren?t being tracked by Git.

git status

6. git log

The Git Log command allows you to view information about previous commits that have occurred in a project. The simplest version of the log command shows the commits that lead up to the state of the currently checked out branch

git log

7.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.

git push -u origin maingit push <remote_URL/remote_name> <branch>

8. 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. Merging remote upstream changes into your local repository is a common task in Git-based collaboration work flows.

git pull <branch_name> <remote_URL/remote_name>

9.git clone

To create a local working copy of an existing remote repository, use git clone to copy and download the repository to a computer. Cloning is the equivalent of git init when working with a remote repository. Git will create a directory locally with all files and repository history.

git clone <remote_URL>

10.git branch

A branch is a version of the repository that diverges from the main working project. It is a feature available in most modern version control systems. A Git project can have more than one branch. Usually we create a new feature branch for a new feature. Also when we do bug fixing also we create a new branch.

To determine what branch the local repository is on, add a new branch, or delete a branch.

# Create a new branch
$ git branch <branch_name>
# List all remote or local branches
$ git branch -a
$ git branch --list
$ git branch
# Delete a branch
$ git branch -d <branch_name>
# Rename a branch
$ git branch -m <old branch name><new branch name>

11.git switch branch

Git allows you to switch between the branches without making a commit. You can switch between two branches with the git checkout command.

$ git checkout<branch name>

12.git merge

The git merge command lets you take the independent lines of development created by git branch and integrate them into a single branch.

# Merge changes into current branch
$ git merge <branch_name>

Version control systems are all about managing contributions between multiple distributed authors ( usually developers ). Sometimes multiple developers may try to edit the same content. If Developer A tries to edit code that Developer B is editing a conflict may occur. So then we need to decide what changes are we keeping and what changes we are not keeping(whether developer A changes keep or developer B changes keep). Once we decide that we can solve the merge conflict.

So I hope this blog will give you some basic idea about git and git commands.

--

--