20 Git Commands You Should Know

Introduction

 
Git is a version control system developed in 2005 by Linus Torvalds, the creator of the Linux kernel. It helps you keep track of the code changes you've made to files in your project. It comes bundled with a large number of commands that you could use to efficiently manage your source code.
 
In this article, we'll go over the 20 most frequently needed Git commands that every software developer should know.
 

Check the git configuration 

  1. git config -l  
The above command displays a list of information about your git configuration including user name, email, default code editor, etc.
 

Configure your Git username 

  1. git config --global user.name "harshal"  
The above command could be used to configure your git user name. Please make sure to replace your git username with harshal.
 

Configure your Git email 

  1. git config --global user.email "harshal@sample.com"   
The above command could be used to configure your email address. Replace your email address with harshal@sample.com.
 

Initialize a Git Repository  

  1. git init  
The above command could be used to initialize i.e. creates a new git repository. It can be used to convert an existing project to a Git repository. The above command creates a new .git subfolder in your current working directory, which contains all of the necessary metadata for the new repository
 

Add a single file to the staging area 

  1. git add my_file  
The above command adds a file to the staging area. Make sure to replace my_file with the name of the file that needs to be added to the staging area.
 

Add all files to the staging area 

  1. git add .  
The above command adds all the files to the staging area.
 

Check Git status 

  1. git status  
The above command will display the status of the current repository including the current branch, list staged, unstaged, untracked files, etc.
 

Commit changes 

  1. git commit  
The above command commits the changes to head. When executed it will open a code editor in the terminal where you can write a commit message.
 

Commit changes with a message 

  1. git commit -m "Your commit message"  
This command lets you only specify a short summary for your commit message without opening the code editor. Replace "Your commit message" with your own commit summary which describes the changes in your commit.
 

Check Git history

  1. git log  
The above command displays a list of commit logs
 

Get branch list 

  1. git branch  
You could use the above command to display the list of all the created branches in the local repository.
 

Delete a branch 

  1. git branch -d my_branch  
The above command is used to delete a Git branch. Make sure to replace my_branch with your own branch name. Also, don't forget to add the -d flag. It tells the git that you wish to delete the specified branch.
 

Create a new branch 

  1. git branch my_branch  
The above command can be used to create a new branch. One thing that we need to keep in mind is Git won't switch to it automatically – you will need to do it manually with using the checkout command. (See #14)
 

Switch branches 

  1. git checkout my_branch  
You could use the above command to switch to a newly created or a different branch. 
 

Create a new branch in Git and switch to it immediately 

  1. git checkout -b branch_name  
You can create and checkout to a new git branch in a single command by adding the -b flag to the checkout command. 
 

Add a remote repository in Git 

  1. git add remote https://repo_url  
The command adds a remote repository to your local repository. Make sure to replace repo_url with your actual remote repo URL.
 

Push your changes to a remote repo in Git  

  1. git push  
You could use the above command to push your changes to the remote repository.
 

Pull changes from a remote repo in Git 

  1. git pull  
 You could use the above command to retrieve the latest changes made to the remote repository.
 

Stashing changes 

  1. git stash  
The stash command could be used to temporarily park (stash) your uncommitted changes (staged as well as unstaged), saves them away for later use.
 

Reverting stashed changes 

  1. git stash pop  
The above command can be used to re-apply the changes parked using the stash command.
 
And that's it. These were the 20 Git commands I use the most often.
 
I hope you found this article useful – Enjoy