Git Basics

Malisha Tharindu
5 min readMar 10, 2020

Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Git was developed by Linus Torvalds for managing Linux kernel developments.

Nowadays Git is widely used as a modern distributed version control system. This helps in tracking the changes done to code in a software project on its progress.

The following are the basic Git commands with the explanation that are heavily used.

Setting up Git
To set up Git to your pc you have to download the Git installation file from the below Git official site. Then, follow the installation steps as you install Git using the installer. You can find more details about installing Git at https://git-scm.com/book/en/v2/Getting-Started-Installing-Git. This document lists several ways of installing Git on various platforms.

After installation, you can following command to verify the success of the Git installation and Git version.

git  --version

once we verify git is installed we have to configure a couple of global identical parameter of the username and email as follows.

Open a cmd window or terminal on your computer and use following commands for configuration.

git config --global user.name "<name>"

This defines the author's name to be used for all commits in the current repo. Developers commonly use — global flag to set config option s for the current user.

git config —-global user.email <email >

To ensure this information has configured we can use the following command in cmd/terminal and see the information.

git config —-list 

So this the way of setting up the git on our pc.

Basic Git Commands

2. Git init

git init

This creates empty Git repo in the specified directory.
Either you can place the repository name and it is not necessary.

git init <directory>

3. Git Clone

git clone <repo>

Clone repo located at <repo> onto local machine. Original repo can be located on the local filesystem or on a remote machine via HTTP or SSH.

So when you need to download the repository in the GitHub, this is the terminal that could be used.

git clone <repo url>

example: git clone https://github.com/malisha/musicReactApp.git

4.Git status

git status

This lists which file are staged, unstaged, and untracked.
In other words, this will list all the files that you have to commit before pushing them.

5. Git Add

you can use mainly two commands as your need.

git add <file>

you can add the files separately using this command.

if you want to add all files to a single commit use following git add all command

git add .

Also you can

6. Git Commit

git commit -m “<message>”

This is the command that uses for committing the changes.
use <message> as the commit message

After doing commenting, again we can use git state to check whether the changes have been applied.

7. Git Log

git log

Display the entire commit history using the default format.

8. Git Diff

git diff

Show unstaged changes between your index and working directory.

Git Undoing changes

1. Git Revert

git revert <commit>

This creates a new commit that undoes all of the changes made in <commit>, that apply to the current branch.

2. Git Reset

git reset <file>

This removes <file> from the staging area, but leave the working directory unchanged. (this unstages a file without overwriting any changes )

3. Git Clean

git clean -n

Shows which file would be removed from the working directory.
To execute the clean you can use following git command

git clean -f

Rewrite Git History

1. Git Commit — amend

git commit — amend

This use for replacing the last commit with the staged changes and last commit combined. Use with nothing staged to edit the last commit’s message.

2. Git Rebase

git rebase <base>

Using this we can rebase the current branch onto <base>.
This <base> can be a commit ID, branch name, a tag or a relative reference to HEAD.

3. Git Reflog

git reflog

This shows a log of changes to the local repository's HEAD
To show date info use git reflog — relative -date flag
To show all the refs use git reflog — all

Git Branches

1. Git Branch

git branch

List all the branches in your repo.
To create a new branch we can add <brach > with the name <branch> as the following command.

git branch <branch>

2. Git Checkout

git checkout -b <branch>

This command will create and check out a new branch named <branch>

Drop the -b flag to checkout an existing branch.

3. Git Merge

git merge <branch>

Merge <brach> into the current branch.

Remote Repository

1. Git Remote Add

git remote add <name> <url>

This creates a new connection to a remote repo.
<name > is used as a shortcut for <url> in other commands.

2. git fetch

git fetch <remote> <branch>

Fetches a specific <branch>

3. Git Pull

git pull <remote>

this fetches the specified remote’s copy of the current branch and immediately merges it into the local copy.

4. Git Push

git push <remote><branch>

Use this command when you want to publish the local changes to the remote. So this pushes the branch to <remote>, along with necessary commits and objects. If there <branch> doesn't exit it will be created a branch named as <branch>

By default, the cloned repository has a remote link called ‘origin’. So use the ‘git push <remoyte> <branch>’ command in case you have multiple remotes and multiple branches (by default you can use the command git push origin master).

Also ‘git push — all <remote>’ command can be used, when you need to push the changes in every branch to a given remote link.

Connect it to GitHub

To upload your local git repo to remote, we can use GitHub, BitBucket etc.

So this is how we can upload our project into GitHub.

  1. Go to GitHub.

2. Log in to your account.

3. Click the new repository button in the top-right.

4. Click the “Create repository” button.

To push an existing repository you can follow these commands by the opening terminal or cmd prompt in the root folder of the project.

$ git remote add origin git@github.com:username/new_repo
$ git push -u origin master

Actually, the first line of the instructions will say

$ git remote add origin https://github.com/username/new_repo

But I use git@github.com:username/new_repo rather than https://github.com/username/new_repo, as the former is for use with ssh (if you set up ssh as I mentioned in “Your first time”, then you won’t have to type your password every time you push things to Github). If you use the latter construction, you’ll have to type your Github password every time you push to GitHub.

Thank You :)

--

--

Malisha Tharindu

Entrepreneur | iOS Developer | Web Developer with more than 5 Years of Experience. Founder of Learn with Malisha