DEV Community

Cover image for Most used git commands:
Sobhan Dash
Sobhan Dash

Posted on

Most used git commands:

Git is a really important tool in the developer world. I sometimes ponder upon the thought that if there was no git how hard would it would be keep track of our codes and it's security.
Surely we have got an magic wand with us to just fling and boom your code is on the internet safe from any flood or fire that may try to destroy your precious code.

So today I'm writing some of the most used commands of git. These will help me remember them and any other beginner who stumbles upon it.

1- git init:

In simple terms it converts a directory into an empty repository. Only after running this command we can do other commands such as commit and add.

git init

or

git init [project name]

2- git add:

This command adds all the files into a staging area so that you can commit it later at once. It can be done by a number of ways, you can either add entire directories or a single file or all un-staged files.

git add <temp.txt>

3- git commit:

Commit is just like a track keeper. It keeps the records of changes that are made in the local repository. That is why you might notice that you need to provide a message along with the commit. It helps a third person to easily understand what changes you have made and where you have made it.

git commit –m “Message to go with the commit here”

4- git status:

It's pretty much self explanatory but what it does is return the current state of the repository.

git status

5- git branch:

Branching is probably the most important part of git. It helps collaborative development and is used most after commit. Git branch helps us adding a new branch, determine which branch we are in, and even delete a branch.
Adding:

git branch <branch_name>

Deleting:

git branch -d <branch_name>

Listing all branches in the remote or local repository:

git branch -a

6- git merge:

Once you create a branch you can go ahead and make changes that do not affect the main branch. But once you're done you need to merge both branches. And that's where git merge comes in. It combines two branches and any changes done in one of them.
More on branching in another blog.

git merge <branch-name>

7- git clone:

Git clone is used for creating a local repository for an existing remote repository. It downloads and copies the remote repository to your computer. It's equivalent to git init for a remote repository. You use init for creating a remote repository and clone to create a local repository.
For remote server:

git clone username@host:/path/to/repository

For copying a local repository:

git clone /path/to/repository

8- git pull:

Pulling something from the remote repository requires the git pull command. It pulls the latest version of the remote repository to your local repository.

git pull

9- git push:

And finally git push. Its essentially the opposite of git pull. It pushes your local repository to your remote repository.

git push origin <master>

I've listed some of the most used git commands, there are still some advanced commands that you should definitely check out.

Top comments (0)