Top 10 Git Commands which help you!

Pasan Kamburugamuwa
6 min readApr 10, 2022

--

Git is a free and open-source distributed version control system that can handle everything from tiny to very large projects with ease. Git tracks the changes you make to files, so you have a record of what has been done, and you can revert to specific versions need to.

And also you can save a copy of the files and their revision history using internet hosts (such as GitHub or Bitbucket). This will lead to cooperate more readily with other developers if you have a central location where you can publish your changes and download changes from others. Git can merge changes automatically, so two or more people can work on different areas of the same file and then integrate their changes without losing each other’s work!

So let’s have a look at what 10 git commands that we are going to discuss in this article.

1. git init

2. git add

3. git commit

4. git status

5. git merge

6. git push

7. git pull

8. git clone

9. git branch

10. git-checkout

Let’s go through them one by one.

1. Git Init

Git init command is used to initialize a blank repository. It creates a git folder (.git folder) in the current working directory.

After executing the following command,

command: git init

there will create a .git folder and it is a hidden folder and that folder can only be seen by enabling the setting of viewing the hidden files. If we get into this folder, we can see a bunch of directories and configurations. As you can see, whenever a git repository is created for the first time, it creates a branch called master.

git init command and it creates a master branch as the default branch.

2. Git Add

The command which is used before the git commit command is the git add command. The git add command is used to add the files to the staging area. When the git adds a command with the full stop, then all the files in the repository will add to the staging area. or else you can add one file by file by selecting only the file which you need to add by pasting the name of the file in front of the git add command.

command: git add .

As from the above example, you can add preferred files to the staging area by the following command.

command: git add <specific-file-you-need-to-add>

3. Git Commit

The git commit is used to save the changes to the local repository. The command helps you keep a record of all the changes made. The changes that are made are the ones that are added to the staging area. So this depends on us which file we want to commit.

4. Git Status

Git status command is used to display the state of the current repository and the staging area. It lets us see which changes haven’t been staged and which files aren’t being tracked yet. The git status command simply shows what is happening with git add and git commit. The status messages include relevant instructions for staging and upstaging the files.

So the following image shows, what happens when we have added the git status command.

5. Git Merge

The git merge command is used to integrate different branches into a single branch. This also can be defined as a way of putting a forked history back together.

Let’s take the independent lines of development created by the git branch and integrate them into a single branch.

6. Git Push

The git push command is used to upload the content from the local repository to the remote repository. After the local repository has been modified, the push command is used to share the modifications with the remote team members.

Command: git push

This will apply the local changes done to the repository into the remote repository that you are currently in.

7. Git Pull

The git pull command is used to fetch the new commits and merge them into the local branch. The git fetch command downloads content from the required remote repository and the git merge command combines multiple sequence commits into a single branch.

The git pull the command is used to fetch and download content from a remote repository and immediately update the local repository to match that content. Merging remote upstream changes into your local repository is a common task in Git-based collaboration workflows. The git pull command is actually a combination of two other commands, git fetch followed by git merge. In the first stage of the operation git pull will execute a git fetch scoped to the local branch that HEAD is pointed at. Once the content is downloaded, git pull will enter a merge workflow. A new merge commit will be-created and HEAD updated to point at the new commit.

Command: git pull

8. Git Clone

The git clone command is used to create a copy of the target repository or create a clone in a new directory at a new place. This is simple as once you create the copy, it will add a .git folder to your local branch also and only you need to deal with versioning of your program (git commands).

Command: git clone <repository link>

9. Git Branch

A branch refers to an independant line of development. The git branch command is used to create, list, rename, and delete branches.

git branch

List all of the branches in your repository. This is synonymous with git branch --list.

git branch <branch>

Create a new branch called <branch>. This does not check out the new branch.

git branch -d <branch>

Force delete the specified branch, even if it has unmerged changes.

git branch -m <branch>

Rename the current branch to <branch>.

git branch -a

List all remote branches.

10. Git Check-out

The git checkout command works together with the git branch command. The command enables navigation between the branches.

Think you are in the master branch, then you need to go to branch1 and do some changes or research there. Then you have to put the below command.

command: git checkout branch1

If you want to go to the master branch again, you apply the following command.

command: git checkout master

So these are the most essential and beginner commands in git. These commands are highly essential when developing software with version control systems like Github, GitLab, or bitbucket. Hope you understand the tutorial well and hope to see you in the next tutorial. Until then, bye.

--

--