Essential Git commands for everyday work!

Nivedha Duraisamy
2 min readAug 27, 2019

I have been self-studying react.js for a while now and fortunately, got a job offer as Frontend Developer. But soon I realised that there were a lot more git commands that I have to use than I knew when it comes to a real-time project. So here are some of the essential git commands I would like to share with you.

Clone a repository:

To copy code from version control to your local computer:

$ git clone <repository_path>

The repository path could be cloned either through HTTPS or SSH.

Branching:

We create a new branch to diverge from the main code (usually the master branch) and continue to do work without messing with it.

Create Branch:

$ git checkout -b <new_branch_name>

By convention, the branch name is the feature you want to work in or the bug you would like to fix.

List all branches:

$ git branch

Switch between branches:

$ git checkout <target_branch_name>

Delete a branch:

$ git branch -d branch_name
$ git branch -D branch_name

-d is short for — delete which deletes only if the branch is already merged with its upstream branch. -D, which stands for — delete — force, deletes the branch irrespective of its merged status.

Get Latest:

To ensure you have all the latest changes to your project, use

$ git pull <REMOTE> <branch_name>

REMOTE is usually “origin”.

Remember, spaces are not allowed in branch names, so use _ or -.

View changes:

$ git status

All the modified changes, including the addition and deletion of files, are listed.

Compare changes:

Once you are ready to push your changes, it is better to check the difference between the local changes and the original repository. This is recommended to avoid any unintended changes from getting pushed to the main repo.

$ git diff

Add changes:

Once you have verified your changes using git status, you can either add all the changes using ‘.’ or mention the specific file that you like to add.

$ git add .
$ git add <file_name>

Commit:

$ git commit -m "describe_the_changes"

Uncommit:

$ git reset HEAD~1

It is important to be careful not to commit unnecessary changes. But as humans, mistakes are common and we have the above command to undo the most recent commit.

Push:

To push changes to GitLab or GitHub.

$ git push <REMOTE> <branch_name>

Delete:

To delete all local changes:

$ git checkout .

Merge:

As we work in separate branches, it is important to merge changes from all the branches with the parent branch, which is the master.

$ git merge master

Save for later:

You cannot switch branches if we have some local changes in a branch. stash saves the local changes in a branch and resets the working directory to match the HEAD commit.

$ git stash

Stashed list:

$ git stash list

Restore stash:

$ git stash apply

With this, we come to the end of all the important git commands I wanted to share. I will make sure to keep this blog updated.

Do let me know if more details are required on any of the commands above.

Cheers!

Niv

--

--