Top 15 Git Commands You Should Know

Β·

5 min read

Hello World!

As a developer, we are using Git commands every day. Here is a list of top Git commands that you should know.

Before jumping into the commands, let's see an overview of Git.

What is Git?

Git is an open-source distributed version control system. In simple words, it is a DevOps tool to manage source code. With Git, we can track the history of the code base and it also allows multiple people to work on the same code base.

Git Commands

1. Init

The init command is used to initialize a repository. Executing this command will create an empty repository in the location where executed, which we can use for version control.

Syntax:

git init

To execute this command, we need to move to the location where we want to initialize the git repository. For example, if I need to initialize a repository in the path /home/sriram/projects/app1 , then I'll need to move to that particular path and execute the command.

cd /home/sriram/projects/app1
git init

2. Clone

The clone command is used to clone any remote repository into our local machine. Suppose we have hosted our app1 in any remote git service provider like GitHub and, we want to clone it in some other local machine, then we use the clone command

Syntax:

git clone <repository>

Example:

git clone https://github.com/sriram23/app1.git

3. Remote

The Git Remote command is used to manage the set of tracked remote repositories. Suppose we have initialized an empty git repository in our local machine and we need to push it to a remote repo. To specify which remote repository to push, we will be using the remote command.

Syntax:

git remote -v # To list the remote repositories that are being tracked
git remote add [name] [repository] # To add a new remote repository
git remote rename [old] [new] # To rename a remote
git remote set-url [repo url] # To update a repo url

Example:

Git remote example

4. Branch

The branch command is used to create, list and delete branches. The branch is nothing but an independent line of development, which is then can be merged with the master code through a Pull request / Merge request.

Syntax:

git branch # List the local branches
git branch <branch name> # Creates a new branch
git branch -d <branch name> # Delete branch post pushing to the upstream (Remote)
git branch -D <branch name> # Delete branch forcefully, even if not pushed to upstream

Example:

Git branch command

5. Checkout

The checkout command is used to switch from one branch to another.

Syntax:

git checkout <branch> # Checkout to another branch
git checkout -b <branch> # Will create a new branch and will checkout
git checkout -B <branch> # Will create a new branch if doesn't exist. If exsits, will be reset and checked out.

Example:

Git checkout command

6. Add

The add command is used to stage the file contents. Which is nothing but adding changes to the index.

Syntax:

git add . # To stage all the changes.
git add <file1, file2, ... filen> # To stage specific files.

7. Commit

The commit command is used to record the changes to the repository.

Syntax:

git commit -m <message>

Example:

git commit -m "Fixed issues with responsiveness"

8. Status

The status command is used to get the status of the working tree.

Syntax:

git status

Example:

Git status

9. Push

The push command is used to push our local commits to the remote repositories.

Syntax:

git push <remote name> <branch>

Example:

git push origin master

10. Pull

The pull command is used to fetch the changes from another branch

Syntax:

git pull <options>

Example:

git pull origin master

11. Reset

The Reset command is used to reset the current head to a specific commit. There are two commonly used modes for resetting. They are the soft reset and hard reset. The soft reset would stage the recent stage and moves the head to a specific commit. The hard reset would ignore the recent stage and moves the head to a specific commit.

Syntax:

git reset --soft <commit hash> # Soft reset
git reset --hard <commit hash> # Hard reset

12. Revert

The Revert command is similar to the Reset command. The difference is in Reset, the head is moved to the specified commit. In case of the revert, a new commit will be created with the changes of the specified commit. In other words, a new commit will be created to revert the changes of the specified commit.

Syntax:

git revert <commit hash>

13. Stash

The Stash is a command used to store the local modification in a separate space. The stashed changes can be retrieved later.

Syntax:

git stash # Will store all the changes, except the untracked files
git stash --include-untracked # Will store all the changes including untracked files
git stash list # To list the stashes in the repo
git stash clear # To clear the stored stashes
git stash save <message> # To stash with a message.
git stash apply <stash> # To apply a specific stash. Will apply stored changes to the file
git stash pop # Applies the latest stash and remove it from stash list

Example:

Git stash

14. Fetch

The fetch command is used to download the remote branches to our local.

Syntax:

git fetch # Will fetch the current remote ref
git fetch origin # Will fetch branches from origin
git fetch --all # Will fetch from all remote refs

15. Merge

The merge command is used to merge two or more branch histories. For example, if we are in a dev-branch branch and we need to sync the history of master branch, then we will execute the merge command in the dev-branch referring to the master.

Syntax:

git merge <target branch>

Example:

# In the dev-branch
git merge master

The above are a few important commands that we use in our everyday work. I'd recommend referring to this doc for the complete list of git commands.

Hope this article is helpful for you. Looking forward to your suggestions and feedback. Thank you!