Popular git commands used every day.

Below are some popular Git commands which are being covered in this article:

git init:

The first step to create a git repository is git init. It initializes an empty git repository to a new or existing project. Run the below command to the root directory of your project.

$ git init

git config:

After creating a git repository you might want to configure your name and email in the main configuration file. This is one time configure and by running the below two commands you can do it easily.

$ git config --global user.email "PUT_YOUR_EMAIL_HERE" 
$ git config --global user.name "PUT_YOUR_NAME_HERE"

git status:

This command will list all the file status you have newly added, modified, and deleted before adding to the staging area for Git.

$ git status

git add:

This command will add all your newly added, modified, and deleted files to the staging area for git.

# To add all the files not staged: 
$ git add .
# To stage a specific file:
$ git add index.js
# To stage an entire directory:
$ git add images

git commit:

This command commits all files you’ve added with the git add command and sets it to a new commit object for your remote.

# Adding a commit with a message 

git branch:

This command will list all the local branches in the current repository.

# Create a new branch 
$ git branch <branch_name>
# List all remote or local branches
$ git branch -a
# Delete a branch
$ git branch -d <branch_name>

git checkout:

This command helps to switch to different branches.

# Switch to another branch 
$ git branch <branch_name>
# Create a new branch and also switches to it.
$ git branch -b <branch_name>

You may also like: MongoDB commands and methods

git merge:

This command merges the specified branch into the current branch.

$ git merge <branch_name_you_want_to_merge>

git remote:

This command is used to connect your local repository with the remote repository.

$ git remote add <remote_name> <remote_server_url>

git push:

This command helps to send the committed changes from the master branch to the remote repository.

# Example: git push origin master 
$ git push <remote_alias_name> <branch_name>
# Push all local branches to the remote repository
$ git push --all <remote_alias_name>
# To delete a branch from remote repository.
$ git push <remote_alias_name> :<branch_name>

git pull/fetch:

This command helps to fetch/pull and merge changes from the remote repository to the local working directory.

# Example: git pull origin master 
$ git pull <remote_alias_name/remote_url>

git log:

This command helps to list the commit history in the current branch.

# Show entire git log 
$ git log
# Show git log with date pameters
$ git log --<after/before/since/until>=<date>
# Show git log based on commit author
$ git log --<author>="Author Name"

git reset:

# Unstages the file but preserves the file contents 
$ git reset <file>
# Undoes all the commits after the specified commit and preserves the changes locally
$ git reset <commit>
# Discards all histories and goes back to the specified commit
$ git reset --hard <commit>

git clone:

This command helps to copy a git repository from the remote source, also sets the remote to original source so that you can pull it again.

$ git clone <remote_URL>

git rm:

This command removes files or directories from the working index (staging area)

# To remove a file from the working index (cached): 
$ git rm --cached <file_name>
# To delete a file (force):
$ git rm -f <file_name>
# To remove an entire directory from the working index (cached):
$ git rm -r --cached <directory_name>
# To delete an entire directory (force):
$ git rm -r -f <file_name>

git stash:

This command helps to save all the modified files temporarily.

# To save modified files temporarily: 
$ git stash save
# To restores the most recently stashed files:
$ git stash pop
# To lists all stashed changesets:
$ git stash list
# To discards the most recently stashed changeset:
$ git stash drop

git show:

This command helps to show the metadata and content changes of the specified commit.

$ git show <commits/tags>

git diff:

This command helps to shows the differences between the files, commits, or branches that are not yet staged.

# To print out the differences between the working directory and the latest commit: 
$ git diff
# To compare two commits:
$ git diff <first_commit_hash> <second_commit_hash>
# To compare two branches:
$ git diff <first_branch> <second_branch>
# To compare specific file across branches:
$ git diff <first_branch> <second_branch> <file_path>

git tag:

This command helps to give tags to the specified commit.

$ git tag <commit_ID>

You may also like: Top 10 Web Securities

Originally published at https://www.codinghub.net.

Coding Hub — Learn & Build Your Career

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store