Git Commands Explained With Examples

Git Commands

Git is something really cool and everyone using git for the first time says, it’s really cool. But most of the time, beginners find themselves in trouble learning the right git commands for the right use at the right time. So, this article is for all those people who want to know any of the git commands that may be useful for different purposes. The flow of the article goes from most basic commands to advanced commands later on till the end.

List of Most Commonly Used Git Commands

Here’s the list of most of the useful and commonly used Git Commands that will help every beginner to quickly get started with git.

git config

You can specify the user name and user email for your git repository and that you can specify with the help of git config command as demonstrated below.

git config -–global "[name]"

git config -–global "[email address]"

An example of a git config command is shown in the screenshot given below.

Git Config Command Example

git init

To initialize the git, the git init command is used. You can use this command whenever you start working with the git. It can be used multiple times to re-initialize the git as well. If you might have already initialized git in a project directory or location, this command will automatically say that the git is re-initialized.

git init

You can also specify a name for your git by simply typing the desired name after the git init command.

git init [repository name]

Git Init Command

You can specify multiple gits with different names. Basically the name, you entered here will be the name of the repository as is specified in the path, shown in the above image. Every git initialized with a different name will have a different directory inside the current directory while initialized git without name with not make any directories and will initialize the git in the current directory.

git clone

If there’s already a repository, you can clone it your local machine using the git clone command.

git clone [url]

You’ve to specify the URL of the repository to obtain. This command may take some time to get all the repository data to your computer and it depends upon the speed of your network.

Git Clone Command Example

If you had already initialized some other git(s) and then cloning, any git cloned will have a different holder under this location.

git add

One of the most used commands is git add. It is used to add one or more files or directories to the staging area. It is often run before committing the changes to the repository. This command ensures that git tracks your files.

git add [file]

To add all files or directories, you can use the * symbol or the -A option.

git add *

git add -A

Git Add Command

git commit

It is certainly the most frequently used git command. When you have to specify a checkpoint in your project, then you use the git commit command. It simply stores your project as a snapshot in the git history. All the files with changes can be tracked with the commit message.

git commit -m "[Your Commit Message]"

Git Commit Command

The output of this command specifies how many files were changed with this commit from the previous staging point and also tells the number of insertions and deletions. Whenever you make another set of changes, you can commit again with a different message. Make sure you always run the add command before committing if you’ve added new files, otherwise, it will show you the warning of untracked changes.

Untracked Files Warning Git

The example of adding the untracked files using the git add command and then committing the changes is shown in the following screenshot.

Git Commit Command Usage Along With Git Add

In case, there are no changes and you’re still running the git commit command, it will say, that there are no changes and the working tree is clean.

Git Commit No Changes

git diff

A really interesting and useful command. git diff command simply shows you the changes that are not yet staged.

git diff

I’ve just made changes to the text file in the project and this command shows up the added two lines.

Git Diff Command

You can also see the differences between the files in the latest version in the history and the files in the staging area with the –staged option used with the git diff command.

git diff --staged

To check the difference between the changes of two different branches, you can specify the first and the second branch to be compared with the git diff command.

git diff [branch_1_name] [branch_2_name]

I’ve just added a new branch, checked out to it and changed the text file. After that, I compared the changes with the git diff command and it shows the following result.

Git Diff Branches Example

git branch

If you want to create a separate branch for the repository of a git, then you can use the git branch command. This command is often used when two or more developers want to work on a single git but also want to make sure that their changes won’t cause any conflicts while making major changes.

Therefore, a single project can be split into a kind of multiple gits. When there are two branches of a single git, the git actually remains the same, but each branch has its different comments and version histories. Branches can also be merged into a single branch. The main branch which is automatically created along with the creation of the git is known as the master branch.

git branch command simply shows the list of all existing branches of the current git.

Git Branch Command

A branch can be created using the following command.

git branch [branch_name]

Git Command To Create A New Branch

To delete a branch, simply use the –d option along with the git branch command.

git branch --d [branch_name]

Git Branch Delete Command

git checkout

To switch or move from one branch to another, the git checkout command is used.

git checkout [branch_to_move]

Git Checkout Command

git merge

To merge two different branches into a single branch, the git merge command is used. You have to just specify the branch which you want to merge with the current branch.

git merge [branch_to_merge]

Git Merge Command To Merge Branches

git reset

To reset the files or you can say, to unstage the commit changes, git reset command is used but it makes sure the changes are preserved locally.

git reset [commit-id]

Git Reset

git reset can also be used for the files.

git reset [file]

Git Reset Files

For a hard reset, you can use the hard option along with the git reset command. It will discard all the commit history and will do directly to the specified commit.

git reset --hard [commit-id]

Git Reset Hard

git status

This command is used to display the status of the current git. If there are any unstaged changes, it will show up them.

git status

Git Status Command

git rm

To remove one or more files from the git, the git rm command is used.

git rm [file]

Git Rm Command To Remove Files

git log

As the name specifies itself, the git log command is used to show you the log of your changes, or you can say to show the version history for your current git and current branch.

git log

Git Log Command

You can also choose to show the log of a particular file.

git log --follow [file]

It will also show you the log as a change if the file is renamed.

git show

This command is used to display the metadata or information about the changes made during a git. You have to specify the id of the commit after the command.

git show [commit-id]

Git Show Command

git tag

For tagging purposes, the git tag command can be used. For example, you want to add two or more commits of the version history into a software release tag to find the released versions of your project easily, then you can keep tag those commits with the git tag command.

git tag [commit-id]

Git Tag Command

git remote

There are a lot of remote repository platforms like GitHub that lets you host your git projects so that you can share it with anyone online publically or privately. The git remote command is used to do the tasks related to your local repository in relation to the remote repository.

git remote [variable_option] [variable_name] [url_of_remote_server]

To add the remote repository for a local git repository, you have to use the git remote add origin command and to remove the remote repository you can use the git remote remove origin command.

Git Remote Command

git push

Once, you have committed all the changes, locally, you can also push them to the remote repository using the git push command.

git push [variable_name] [branch_name]

Git Push Command

The following git command is used to push all the branches to the remote repository.

git push –-all [variable name]

Git Push Command To Push All Branches

You can also use git push command to delete a branch from the remote repository.

git push [variable name] : [branch_to_delete]

Git Push Command To Delete A Branch

git pull

It is the opposite command of git push. It simply pulls all the git data from the remote repository to the local repository.

git pull [remote_repository_url]

If multiple people are working on your repository and are making commits and pushing those commits to the remote repository, then to get the fresh and latest version of your project, you can pull the repository to your local repository anytime using the git pull command.

Git Pull Command

git stash

git stash can be used for different things related to the tracked files.

git stash save can be used to temporarily store all of the tracked files that are modified.

git stash pop is used to restore the stashed files that are most recent.

git stash list simply shows all of the stashed changesets.

The changeset that is most recently stashed can be removed with git stash drop command.

To find out more git commands, their usage and explanation, please check out the documentation of git.

Suggested Readings.

Subscribe To WTMatter

Leave a Reply

Your email address will not be published. Required fields are marked *