DEV Community

Cover image for Git Basic Commands
Himanshu Gupta
Himanshu Gupta

Posted on

Git Basic Commands

Here is Git basic Commands which every developer should know. It Will Save your time.

git config
Usage: git config –global user.name “[name]”

Usage: git config –global user.email “[email address]”

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

Image description

git init
Usage: git init [repository name]

This command is used to start a new repository.

Image description

git clone
Usage: git clone [url]

This command is used to obtain a repository from an existing URL.

Image description

git add
Usage: git add [file]
This command adds one or more to the staging area.

Image description

Usage: git add *
This command adds one or more to the staging area.

Image description

git commit
Usage: git commit -m “[ Type in the commit message]”

This command records or snapshots the file permanently in the
version history

Image description

Usage: git commit -a

This command commits any files you’ve added with the git add command and also commits any files you’ve changed since then.

Image description

Usage: git commit -a

This command commits any files you’ve added with the git add command and also commits any files you’ve changed since then.

Image description

git diff
Usage: git diff

This command shows the file differences which are not yet staged.

Image description
Image description

Usage: git diff –staged

This command shows the differences between the files in the staging area and the latest version present.

Image description

Usage: git diff [first branch] [second branch]

This command shows the differences between the two branches mentioned.

Image description

Usage: git diff –staged

This command shows the differences between the files in the staging area and the latest version present.

Image description

Usage: git diff [first branch] [second branch]

This command shows the differences between the two branches mentioned.

Image description

git checkout
s a new branch and also switches to it.

Image description

git merge
Usage: git merge [branch name]

This command merges the specified branch’s history into the current branch.

Image description

git remote
Usage: git remote add [variable name] [Remote Server Link]

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

Image description

git remote
Usage: git remote add [variable name] [Remote Server Link]

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

Image description

git push
Usage: git push [variable name] master

This command sends the committed changes of master branch to your remote repository.

Image description

Usage: git push [variable name] [branch]

This command sends the branch commits to your remote repository.

Image description

Usage: git push –all [variable name]

This command pushes all branches to your remote repository.

Image description

git pull
Usage: git pull [Repository Link]

This command fetches and merges changes on the remote server to your working directory.

Image description

git stash
Usage: git stash save

This command temporarily stores all the modified tracked files.

Image description

Usage: git stash pop

This command restores the most recently stashed files.

Image description

Usage: git stash list

This command lists all stashed changesets.

Image description

Usage: git stash drop

This command discards the most recently stashed changeset.

Image description

git branch
Usage: git branch

This command create new branch and other commend for check history

Creating a new branch

git branch

git branch fix-division-by-zero-bug2
git branch fix-property-call-on-undefined-object2# Listing all branches
git branch
git branch# Listing remote branches
git branch -a# Renaming an existing branch
git branch -m
git branch fix-division-by-zero-bug2 fix-division-by-zero-bug
git branch call-on-undefined-object2 call-on-undefined-object#
Deleting an existing branch
git branch -d
git branch fix-division-by-zero# Deleting an existing branch even when the branch has not fully been merged in its upstream branch
git branch -D
git branch -D fix property-call-on-undefined-object

Checkout from main branch to a feature-branch

git checkout
git checkout feature-branch# Checkout a branch that is non-existent

You can the flag -b on git checkout to checkout from your current

branch into a new branch

git checkout -b
git checkout -b feature-branch-v2
git reset
Usage (i): git reset Code on git

Move every staged files back into your working tree

git reset# Move only specific staged files back into your working tree
git reset [, , , ...]# Move only staged files in a given folder
git reset
git log
Usage (i): git log

This command is used when we want to check the log for every commit in detail in our repository.

Note: It will show the log of the branch we are in. We can check the last three logs by giving the command: git log -3

Image description

Usage (ii): git log –graph

Image description

Usage (iii): git log –graph –pretty=oneline

Image description

git branch
Usage (i): git branch [name-of-the-branch]

So far, we saw how we can work on Git. Now, imagine, multiple developers working on the same project or repository! To handle the workspace of multiple developers, we can use branches. To create a branch (say, the ‘name-of-the-branch’ is ‘branch1’), we use this command:

Image description

Usage (ii): git branch -D [name -of-the-branch]

Similarly, to delete a branch, we use the git branch -D command:

Image description

Top comments (0)