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.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,275FansLike
384FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
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.

All about Ubuntu editions and which version should you use?

Ubuntu is one of the most popular Linux distributions developed and released by Canonical, and not without reason. It has very enriched repositories, with support for all the programs you could ever need.

6 cool and fun Linux commands you don’t want to miss

The real purpose of this article is to help Linux newbies get comfortable and confident with the Linux command-line.  While knowledge and comfort of the Linux GUI allow great power, it is the mastery of the command line, or CLI, affords the Linux user unlimited power and certifies them as a Linux power user well on their way to becoming an expert.

The 10 Best Linux Games [2020 Edition]

Not too long ago, people had to use Wine or run virtual machines like VMware to play Windows Games on their Linux PCs. Not only was this inefficient, but it also consumed a lot of extra resources and resulted in a sub-optimal gaming experience. But standing in 2020, gaming on Linux is like a long-time dream come true.

6 Essential Command-Line Utilities Every Linux User Should Know

Last week, we shared with you several "cool and fun" commands to get comfortable and confident with the Linux command-line. In our quest to further aid Linux users with mastery of the command line, or CLI, we present you with a variety of command-line utilities essential for all Linux users, regardless of proficiency level.