Important git commands

In our day to days coding activities we git use heavily , so it is essential to understand and use git properly. You can use sourcetree or can use git CLI.

There are various commands you can use, mastering Git takes time. But some commands are used more frequently (some daily). I will explain some of them

Git Clone

Git clone basically makes an identical copy of the latest version of a project in a repository and saves it to your computer. For example if you want to download some code from Bitbucket use the following command

git clone <https://name-of-the-repository-link>

Git branch

Branching is very important when you are working using git. We recommend to create separate branch during development. Git branch command is using for creating, listing and deleting branches.

Creating a new branch:

git branch <branch-name>

this will create a local branch and will not have any effect until you push this branch into remote serve. You can push this branch using

git push -u <remote> <branch-name>

Viewing branches:

Deleting a branch:

Git checkout

To start working in a branch, first you need to switch to it. We use git checkout mostly for switching from one branch to another. 

To switch from one branch to another you have to make sure no un-committed code reside in current working branch.

You can use the following command to create and switch to the branch at the same time

Git status

To get the full status of the current work branch such as is the current branch up to date, is there any un-committed, unstated or untracked files, where any file created, modified or deleted. You can use the following command

Git add

To include any local changes to next commit you have to add those changes before commit.

To add a single file:

To add everything at once:

Git commit

After we finish developing certain part of a feature we used to commit that code. Git commit save those changes only locally. Ensure that you add message with each commit

Git push

After committing changes next part is to push those changes from local machine to remote server. Git push uploads your commits to the remote repository.

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

Git pull

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

Git revert

To undo our commits we use git revert. To see our commit history, first we need to use git log -- oneline, Then we just need to specify the hash code next to our commit that we would like to undo:

just press shift + q to exit from the screen

Git merge

Git merge basically integrates your feature branch with all of its commits back to the develop (or master) branch. It's important to remember that you first 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 develop branch:

  1. switch to the develop branch:

  2. update your local develop branch

  3. merge your feature branch into develop:

 

Those are most used git commands that we are utilizing frequently in our daily programming.