Useful git commands with examples

So before starting, let’s understand what is git and why is it so popular? GIT is the widely used free open-source version control system in the software industry. Using GIT you can track different versions of source code and help to collaborate with other team members. In this article, I will explain the most useful git commands which every developer must know and it’s frequently used while you are working with GIT.

Few worldwide used source code repositories:

Most used git commands:

git config

The git config command is used to set Git configuration values on a global or local project level.

Usage:

git config --global user.name "[name]"
git config –global user.email "[email address]"

This command sets the author name and email address globally to be used with your commits.

git init

The git init command creates a new Git repository. this is usually the first command you’ll run in a new project.

Usage: 
git init [repository name]

git clone

git clone is a Git command which is used to clone, or copy from an existing target repository.

Usage: 
git clone [repository url]

git status

The git status command displays the state of the working directory and the staging area or you can say that this command displays all the files that have to be committed.

usage: git status

created few files to check the git status command

git add

The git add command is used to add file to the staging area. This command can be used for one or multiple files.

Usage:
git add [FILE_NAME]       // for specific file 
git add *                         //  for all files
Add Single files
Add multiple files

git commit

git commit creates a commit, which is like a snapshot of your repository. It is used to record the changes in the repository. The commit command performs a commit, and the -m “message” adds a message.

Usage: git commit -m "YOUR_COMMIT_MESSAGE"

git remote

The git remote command create connections to the target repositories. Its more like bookmarks rather than direct links into target repositories.

Usage: git remote add [VARIABLE_NAME] [REPO_URL]

git branch

git branch command helps you to create, rename, list and delete branches

Usage:
git branch
git branch [BRANCH_NAME]
git branch -d [BRANCH_NAME]

git push

Usage:
git push [VARIABLE_NAME] [BRANCH_NAME]

git checkout

git checkout is used to switch or change the current branch to another branch.

Usage:
git checkout [BRANHC_NAME]
Usage
git checkout -b [NEW_BRANCH_NAME]  //create a new branch and also switch to it.

git merge

git merge command used to merges the specified branch’s into the current branch.

Usage
git merge [BRANCH_NAME]

git diff

git diff command is used to track the difference between the changes made on a file.

Usage:
git diff
Usage:
git diff [BRANCH_ONE] [BRANCH_TWO]   //Shows the differences between the two branches.

git stash

The git stash command save your uncommitted changes (both staged and unstaged) for later use.

Usage:
git stash save
gist stash list // lists all stashed changesets.
git stash drop  // discards the most recently stashed changeset.

git tag

git tag is used to tag your stable source code i.e code fridge.

1 thought on “Useful git commands with examples”

Leave a Comment