Top 12 Git Commands Every Developer Should Know

Top 12 Git Commands Every Developer Should Know

Photo by Praveen Thirumurugan

What is Git?

In this blog, we will learn Git, a free and open-source distributed version control system designed to handle everything from small to large projects quickly and efficiently. In simpler words it lets you manage and keep track of your source code history. Don’t mistake Git for GitHub, GitHub is a cloud-based hosting service that lets you manage Git repositories. Even I and millions of people use GitHub. Before we begin, let's download Git on our computer Git - Downloads (git-scm.com)

Are you excited? No? But I am and you should be too because every interviewer will ask you about it and in the future, you will do the same. I have serially listed the git commands below.

git init

Git init creates an empty Git repository or reinitializes an existing one.

Syntax:

git init

git config

Git config commands to set your username and email. Git requires that a username and email be established before you can leverage all of its capabilities.

Syntax:

git config -- global user.name “John Doe” - This set the username.

git config -- global user.email “johndoe@example.com” - This set the email address.

git add

The git add command adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit.

Syntax:

git add <file_name> - This adds a specific file.

git add <directory_name> - This adds a specific directory.

git add . - This adds all the unstaged files.

git commit

The git commit command is used to keep track of our progress and changes as we work, git considers each commit change point or save point. It is a point in the project you can go back to if you find a bug, or want to make a change.

Syntax:

git commit -m “ message” - This git commit command performs a commit, and the -m “message” adds a message.

git push

The git push command uploads local repository content to a remote repository. Remember Github? It is also a remote repository. Contents on the GitHub repository can be shared with friends and even they can contribute to your project or vice versa.

Syntax:

git push origin master - This pushes the content to the “master” branch of your remote repository

git status

The git status command displays the state of the working directory and the staging area, it lets you see which changes have been staged, which haven’t, and which files aren't being tracked by Git. Remember this will not show the committed project history.

Syntax:

git status - This displays the state of the working directory.

git branch

The git branch command is used to create a branch, delete a branch, and list all the branches that have been created or are available in the repository.

Syntax:

git branch -a - This will get all the branches that are available in our repository.

git branch <branch name> - This will create a new branch.

git branch -d <branch name> - This will delete a branch.

git checkout

The git checkout command is used to go to switch between the branches.

Syntax:

git checkout <branch name> - This switch to an existing branch.

git checkout -b <branch name> - This will create a new branch and switch to this new branch.

git merge

The git merge command is used to integrate one branch into another branch. Usually, we integrate the feature branch into the main branch of a repository.

Syntax:

git merge <branch>

git clone

The git clone command is used to create a local copy of an existing remote repository.

Syntax:

git clone <URL>

git pull

The git pull command is used to get the latest changes made to a remote repository.

Syntax:

git pull <branch name>

git stash

The git stash command is used to store the changes made in the local repository which was not committed. This is very useful when you want to pull some changes from the remote repository which was committed by your friend and want to merge it with your source code.

Syntax:

git stash - This will save modified and staged changes.

git stash list - This will list out our stashed file changes.

git stash pop - This will bring back our temporarily stored files.

git stash clear - This will temporarily delete stored, modified, and tracked files.

Conclusion

We now know some of the most crucial Git commands to increase productivity. Use these Git commands in your regular coding activities. If you have learned all 12 of the aforementioned instructions, there are many more. Here is a link to the official Git - Reference webpage where you may learn about the other commands. Hope to see you in my next blog. Thank you.