Header Ad Section

GIT Basic Commands


Git is one of the popular Versioning Control Tool which used for tracking changes in any set of files when coordinating work among programmers collaboratively developing source code during the software development process with the intent of improve the speed, data integrity, and support for distributed, non-linear workflows.  

✱ Note:- If you interested in Versioning Control Tools, Please refer our article written on Version Control Systems - (VCS)

So, let's dig in to the basic commands of Git in case in purpose of Version Controlling.  Here are some of the most useful git commands you have to deal with.

  1. Git Clone
  2. Git Branch
  3. Git Checkout
  4. Git Status
  5. Git Add
  6. Git Commit
  7. Git Pull
  8. Git Push
  9. Git Revert
  10. Git Merge

So, let's have a quick look at the purpose of these commands and their usages.


1. Git Clone

Git clone command uses to clone a project from a remote repository to your local computer (your working directory). Basically git clone makes an identical copy of the latest version of a project in the remote repository and saves it into your computer (your working directory).

Command Syntax: git clone <repository-url>

In most occasions this Repository URL is available in both SSH and HTTPS.

Eg: 

    1. With SSH using GitLab: 

        git clone git@gitlab.com:git-training-sessions/sample-project.git

    2. With HTTPS using GitLab:

        git clone https://gitlab.com/git-training-sessions/sample-project.git


What is a Git Repository URL?

Git Repository URL is used for the Git clone process. Basically it may consists of following parts.

HTTPS Repository URL Syntax:
Syntax: https://<vcs-tool-domain-name>/<project-group-name>/<project-name>/<project-name>.git


2. Git Branch

Git branch command is another useful command working with git. Git branch command is used for different purposes such as creating a branch, view branches and delete branches etc...

2.1 Create a Branch

Below command is used to create a new branch in the local git repository. when we initiate a git project by default git creates a branch which is called as "master" branch in most version control tools and some other known as "main" branch. This branch is consider as the most protected branch which developers don't have access to modifications directly while only have access to the special users who have maintainer access.

Command Syntax: git branch <branch-name>

Eg: git branch dev

So, the above command will create a branch in your local repository. But your remote repository still doesn't available that branch you just created. So, you need to push that branch to the remote repository using the below command.


Command Syntax: git push -u <remote> <branch-name>

Eg: git push -u origin dev

2.2 View Branches

You may use one any of these following git branch commands to view the current existing branches of the git repository.

Command Syntax:
    1. git branch 
    2. git branch --list

2.3 Deleting a Branch

You may use the following command to delete the current existing branch from the git repository.

Command Syntax: git branch -d <branch-name>

Eg: git branch -d dev


3. Git Checkout

Git Checkout command is another most useful command working with git. This command is used to switch between git branches. 

For an example if you need to checkout from master branch to dev branch you may use this git checkout command to switch to the other branch you want to switch.

Before checkout to another branch, you must pay attention to important fact. That is you must stash all of your local changes (changes you've done locally but not committed yet).

1. Checkout to an already existing branch in Local Git Repository.

To use this command, the branch you are trying to checkout must exist in your local repository since you're trying to checkout to an already existing branch. 

Command Syntax: git checkout <branch-name>

Eg: git checkout dev


If your expected branch already exist in the remote repository you need to execute below command to fetch those branches to the local repository before using git checkout command.

Command Syntax: 
    1. git fetch <remote> - fetch all the branches and downloads all their files along with the commits 
    2. git fetch <remote> <branch-name> - same result of the above command but only restricted to specific         branch mentioned in the command
    3. git fetch --all


2. Create a New Branch in Local Git Repository and Checkout at the Same Time.

Command Syntax: git checkout -b <branch-name>

Eg: git checkout -b dev


If you need to push the locally created branch to the remote repository, you may use below command to push to the remote repository.

Command Syntax: git push -u <remote> <branch-name>
Eg: git push -u origin dev

4. Git Status

Git Status command may be used to view all the informations of the current branch. Git Status command will give informations about the current branch such as:

    1. Whether the current branch is already up-to-date or not
    2. Whether there are any files to be commit, push or pull.
    3. Whether there are files staged, un-staged or untracked.
    4. Whether there are files newly added, modified or deleted.

Command Syntax: git status

Post a Comment

0Comments