Top 20 Git Commands with Practical Examples

Git is a distributed version control system used to manage a project in source code history. Here are top git commands that come in handy in your projects.

If you are here reading this post, there is a high probability that you have heard or interacted with Github, and you now want to learn Git. Before we continue with showing you some of the cool Git commands, let’s understand the difference between Git and GitHub.

Git and Github

Let’s first discuss Git. Putting it in the simplest way possible, Git is a distributed version control system used to manage a project in development source code history. Git is like a collaboration tool that enables several developers to contribute to a single project.

Git Logo.

Suppose you have a team of developers all working on developing an Information Management System. Some will work on the GUI, others on the Database, and some on the system functionality. Managing such a project would need the use of USB drives to share code between developers. However, with Git, developers can add their code to the project without overwriting any part of the project.

Github Logo

Github, on the other hand, is an online platform used to host the Git repository. While Git is a command-line utility, Github has a Web interface that comes with even more additional features to manage a project.

Top 20 Git commands

In this post, I will show you the top 20 git commands that will come in handy when managing your projects.

1. Git Init

git init

This command initializes a git repository in your directory and creates a .git folder. As a [dot] file, the .git directory is hidden, and you might need to disable the feature in Windows or use the ls -a command in Linux to see it. However, it’s better to leave it as that and not tamper with this folder’s contents.

Git Init.

2. Git Config

git config –global user.name “Your Username Here”
git config –global user.email “user@domain.com”

This command configures the username, email, file formats, etc., with those of Github or the online platform hosting your repository.

Git Config.

3. Git Clone

git clone "url.git"

This command downloads/clones a repository from a remote source to your local machine. For example, let’s clone the reaver repository to our local machine. The Reaver is an open-source tool used in Wireless penetration testing.

git clone https://github.com/t6x/reaver-wps-fork-t6x.git

Git Clone.

By running the ls command, we see the Reaver folder cloned successfully.

4. Git Status

git status

The Git Status command shows you all information you need to know about your local repository. It includes the branch you re working on, files present in index and untracked files, and modified files.

Git Status.

5. Git Add

git add

The Git Add command adds untracked files present in your working directory to index ready for committing to our remote repository. In the previous example, “git status,” we see our index.html is an untracked file.

Let’s add it to the index for committing using the git add command. See the image below.

git add index.html

Git Add.

By running the ‘git status’ command again, we see the index.html file added to the index and ready for committing.

6. Git Remove

git rm --cached [file-name]

The Git Remove command removes files added from the working directory to index. Let’s remove the index.html that we said in the previous example.
git rm index.html

git rm --cached index.html

Git Remove

7. Git Commit

git commit

The Git Commit command saves your changes to the local repository, ready to be pushed to the remote repository. This command takes three arguments;

  • -m (message)

Enables you to leave a note of your changes so that colleagues can understand what happened. See the example below.

git commit -m "Improved Design on Contact Page."
  • -a

this argument Takes all modifications performed on the tracked files.

  • –amend

This argument updates the most recent commit with any other staged changes or any last commit message.

Git Commit -m command.

8. Git Diff

git diff

The Git Diff command lists all untracked changes. For example, let’s add some Html code to our index.html file then run the git diff command. See the output in the image below.

Git diff command.

9. Git Reset

git reset

The Git Reset command un-stages your file and put your working directory and index in the state of your last commit. It preserves any changes done to the file.

Git reset.

10. Git Log

git log

This command lists the version history of the branch you are currently working on.

Git Log.

You can use the ‘git log’ command with the ‘-follow’ parameter to list the version history of the file. For example;

git log -follow index.html

Git Log -follow.

11. Git Show

git show

This git command lists the changes made on a file and metadata of the particular commit.

Git Show.

12. Git Tag

git tag

This command lists tags to a specified commit.

Git Tag.

13. Git Branch

git branch

The git branch command lists all the branches present in your local repository.

Git Branch to list all branches
Git Branch to list all branches

To create a new branch, use the syntax below.

git branch [new-branch-name]

Create a Git Branch new branch.
Create a Git Branch new branch.

Note, you will only be able to create your first branch after making your first commit.
To delete a branch, use the syntax below.

git branch -d [branch-name]

Delete a branch.
Delete a branch.

14. Git Checkout

The Git checkout command enables you to switch between the branches present in your local repository.

git checkout [branch-name]

Git Checkout

If the branch you are specifying is not there, add the -b parameter to create the branch.

git checkout -b [branch-name]

Git Checkout -b.
Git Checkout -b

15. Git Merge

git branch [branch-name]

This command merges the history of the specified branch into the current working branch.

Git Merge.

16. Git Remote

This command links your local repository on your computer to the remote repository.

git remote add [variable-name] [Remote-Repo-Serve-Link]

Git Remote.

17. Git Push

git push [variable-name] [branch-name]

This command pushes/sends your committed changes to the remote repository on Github or Bitbucket.

Git Push.

To push all your branches on the local repository to the remote repository, use the syntax below.
git push -all [variable-name]

Git Push -All

18. Git Pull

git pull [remote-repo-link]

This Git command fetches the changes of the remote repository and merges them with your local repository.

Git Pull.

19. Git Stash

git stash save

The above Git Stash command stores all you tracked files temporarily.

Git Stash Save.

git stash pop

This git command restores any recently stashed files.

Git Stash Pop

20. Git fsck

git fsck

The Git File System Check (fsck) checks the integrity of the Git file system in your local repository by identifying corrupted objects.

Git fsck

Getting to know how different Git commands work is useful, especially if you will be using it to manage your projects. Luckily, most of these commands are easy to understand, and you might not need to use every single of them.

Arun Kumar
Arun did his bachelor in computer engineering and loves enjoying his spare time writing for FOSS Linux. He uses Fedora as the daily driver and loves tinkering with interesting distros on VirtualBox. He works during the day and reads anything tech at night. Apart from blogging, he loves swimming and playing tennis.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,536FansLike
365FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to install CouchPotato on Ubuntu

Downloading movies and copying them over to your home server can get frustrating, especially if you are doing it daily! What if you have an option to download videos automatically, quickly, and above all with excellent quality. CouchPotato allows you to download movies easily once they are available and released automatically.

Top 20 Rsync Examples in Linux

The Rsync (remote sync) command is a Linux/Unix utility used to synchronize and copy files and directories either locally or remotely. Most Linux geeks use Rsync to mirror, backup or migrate data across folders, across disks and networks. One notable feature with the Rsync command is that it uses the "delta transfer algorithm."

Setting up NFS Server on Ubuntu Server

We have put together a detailed step-by-step tutorial that will guide you on how to install and set up NFS Server on Ubuntu so you too can start sharing data with other client systems. It is divided into sections, the first part discussing how to set up the host server and the second part showing you how to set up the client-side. For this tutorial, we will be using Ubuntu 20.04 LTS.

Crontab in Linux Explained With Examples

Crontab is the Linux equivalent of the Window's Task Scheduler. It can help you set up a task to run automatically at a regular...

The 10 Best Linux Server Distros For Home And Businesses

By the year 2020, it is estimated that there are close to 600 Linux distributions in the market. It includes both servers and Desktop versions; therefore, if you are looking for lightweight Linux distribution for your old PC or a reliable desktop version for employees in your organization, you may be overwhelmed with the number of choices for finding one for your use.

How to list the installed Packages on Ubuntu

It is only natural to start installing more and more software on your Ubuntu PC once you start using it. Similarly, it is also natural to lose track of the different packages that you have installed on your system. Eventually, you may be overwhelmed with the number of packages in your system and with the ones you hardly used.

MUST READ

Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

10 top reasons to switch to Manjaro Linux

Manjaro is Linux distro based on Arch-Linux which follows a rolling release model. Is this distro good for you? Let's find out the main reasons for using Manjaro.

Installing an updatable Manjaro Linux on a USB Flash Drive

In this tutorial, we will show step by step how to install a Manjaro on a USB flash. For those new to this distro, it is a light-weight distro that is based on Arch Linux. Arch Linux is known for its simplistic approach and is typically made for Linux pros, but Manjaro bridges the gap between user-friendliness and rock-solid performance.

10 Reasons to use Cinnamon as your Desktop Environment

With the release of Gnome 3 in 2011, there was quite a mixed reaction from users and developers. Most of them preferred the original Gnome that got forked, and one of those forks was Cinnamon. Since the release of Cinnamon 2.0, Cinnamon has evolved to become a desktop environment by itself.

10+ Best Python IDEs for Linux

Python programming language is applied in so many areas of computer technology, i.e., Scripting, GUI development, Website development, Artificial Intelligence (AI), Machine Learning, Data Science, Computer Networking, and Network Automation, and Cyber Security.