Frequently used GIT Commands.
Tags: tutorial, github
Published: 2020-12-21 04:11:51

GIT is an important part of a developer's life. For beginners too, it is of great importance as it helps develop good version control practices early.

**Prerequisite**: Basic understanding of how Git works.

Following is a list of git commands that can be used for quick reference to get started with Git & Github:

**1.Git clone**

This command essentially helps you download a copy of the source code written on any available public repository.

* The command is as follows:

`git clone https://name-of-the-repository-link`

To clone a repo from the source code you need to go to the repository you want to clone and then click on the green button that says "clone or download" and then replace it with " https://name.." as shown above.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nlch7f7z99raqedobvr6.jpg)

This will enable you to clone the repository you want.

**2.Git Branch**

This command helps several developers to work on the same project contributing individually ,such that you can create changes in the code of the project without really affecting the original copy or each other's code. Later, everyone's contribution is appended to the main branch like putting together the pieces of a puzzle to get the entire project together in main source code.

The commands are as follows:
**Creating a new branch**

`git branch branch-name`

* This helps you to create a branch **locally**.

* To push the new branch into the new repository:

`git push -u remote branch-name`

**Viewing Branches**

`git branch or git branch --list`

**Deleting a branch:**

`git branch -d branch-name`

**3.Git Checkout**

This is one of the most used git commands. We use Git checkout mostly for switching from one branch to another. It can also be used for checking out files and commits. 

`git checkout name-of-your-branch`

Some steps to successfully switch between branches is as follows:

* The changes in your current branch must be committed before you switch.

* The branch you want to know about should exist.

There is also a shortcut command that allows you to create and switch to a branch at the same time.

`git checkout -b name-of-your-branch`

A new branch in your local is created and checks the branch out to new as soon as it has been created.

**4.Git Status**
The command gives us all the necessary information about the current branch. 

`git status`

With the help of the command you can check:
* if the current branch is up to date.
* if there is anything to commit, push or pull.
* if there are files staged, un-staged or untracked.
* if there are files created, modified or deleted

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9bpdojwbgjnt6kolobxd.png)

**5.Git add**

If a file is created, modified or deleted then ,these changes occur in our local and will not be included in the next commit.(Unless the configurations are changed.)

we need to use this command to add files into our next commit.

**To add a single file**

`git add file` 

**To add everything at once**

`git add -A`

When you check the status with the Git status command you can see that there are files that are in red color.
These are un-staged files and will not be included in your commits.

**To include them , we need to use git add**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ordg9r80fvo69iyqnd6f.png)

Note: The Git add command doesn't change the repository and the changes are not saved until we use git commit.

**6.Git Commit**
It is one of the most-used command of git. Once we 
reach a certain point in out projects where we want to check if the changes made to the projects are implemented we use Git Commit command.

This command helps to provide a type of a checkpoint in the program or source code such that we can go back to the point where changes were last saved and  then we can continue from the last commit.

Good commit messages are essential. A good commit message tells the developer what changes were made, if there are  not any then does the developer need to add more feature etc. and so on which helps the developer in coding more efficiently. 

`git commit -m "commit message"`

**7.Git Push**

Git Push helps the coder to push the code, that is to upload the source code on git hub. Git push uploads your commit to the remote repository.

`git push remote branch-name`

Also if you branch is newly created, you also need to upload the branch with the following command:

`git push --set-upstream <remote> <name-of-your-branch>`

**or**

`git push -u origin <branch_name>`

Note: Git push only uploads changes that are committed.

**8.Git pull**

The Git pull command is used to get the latest updates from the remote repo. This command is a combination of git fetch and git  merge that is when we use this command it gets updates from remote repository (git fetch) and immediately applies the latest changes in your local(git merge).

`git pull remote`

This may cause conflicts that you need to solve manually.

**9.Git revert**

Sometimes we may want to undo certain changes that were not needed .This can be done in various ways locally or remotely(depending upon what we need),we need to use these commands carefully to avoid unwanted deletions.

A better way to ensure that we can undo are commits is by using  git revert.

To see our commit history , we first need to use 
**git log--oneline:**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zq0tbcu9ljfiu0n1gatn.jpeg)

Then we need to specify the hash code next to our commit that we would like to undo(for example):

`git revert 3321844`

After which you have to press shift+q to exit the program.

This will help to undo the given commit ,but will create a new commit without deleting the older one.

The main advantage is that it does not touch the commit history .This means you can still see all your commits in history.

Another advantage is that everything happens in our local system unless we push them to the remote repo.

This is why git is safer to use and is most preferred to undo certain changes in our commits.

**10.Git Merge**

When you have completed your project and it work fine,the final step is merging the branch with the parent branch(dev or master). This is done with `git merge` command.

It helps to integrate your feature branch with all of its commits back to the dev(or master)branch.Keep in mind that you need to be on the specific branch that you want to merge with your feature branch.

For example, when you want to merge your feature branch into the dev branch:

**First you should switch to the dev branch:**

`git checkout dev`

**Before merging, you should update your local dev branch:**

`git fetch`

**Finally, you can merge your feature branch into dev:**

`git merge branch-name`

IMPORTANT: Ensure your dev branch has the latest version before you merge your branches, otherwise you may face conflicts or other unwanted problems.

So these are the most common git commands that I have come across.There are many more useful git commands that are used.