Home Git Top 20 Git Commands with Practical Examples

Top 20 Git Commands with Practical Examples

by Arun Kumar
Published: Updated:
git command (1)

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.

You may also like

1 comment

Alban September 4, 2020 - 3:52 AM

15. Git Merge
needs to be updated

Reply

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.