Git Basic Commands
Tags: git, github, serverless, beginners
Published: 2023-02-06 06:41:25

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nw6z88x2of7jzf1zdzva.png)

**git init**
Usage: git init [repository name]

This command is used to start a new repository.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/th9ysg62ljk3ihmp2quj.png)

**git clone**
Usage: git clone [url]

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r18af5hrkqj2qlwjaccg.png)


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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/88ndg9y3qjq8mqgzsyxs.png)

Usage: git add *
This command adds one or more to the staging area.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihp3ecrnixeovcjovyce.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/49cj6z62t9ehtkn0w0gi.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xexbytf7376kn6behrt6.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ijtm47xw6m03hnzte26l.png)

**git diff**
Usage: git diff

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4uewgp1phlwpe3q41iw0.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ret8cxzw2ii0fg4hc0s.png)


Usage: git diff –staged

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vcg38fs63n53ycy59uzu.png)

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

This command shows the differences between the two branches mentioned.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c47f8saz4tmltwnkedas.png)


Usage: git diff –staged

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w9bl2744mqjzapig3ezd.png)

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

This command shows the differences between the two branches mentioned.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6466pm8tb7erpslleunt.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3j61o1nhfvu2es20yrvs.png)

**git merge**
Usage: git merge [branch name]

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kdniga65v2ccg2uz71oa.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v0pk3qxtnkvbu3oxdj12.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ey9xok52p5jo1nkt6yo.png)

**git push**
Usage: git push [variable name] master

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gouda8c2mugmfpdgeoi3.png)

Usage: git push [variable name] [branch]

This command sends the branch commits to your remote repository.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jjzcncl7h8llyu18w9ds.png)

Usage: git push –all [variable name]

This command pushes all branches to your remote repository.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/twtwfiqrcupleg1c0rs0.png)

**git pull**
Usage: git pull [Repository Link]

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zkgyl2sjszwuzwb96koz.png)

git stash
Usage: git stash save

This command temporarily stores all the modified tracked files.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m705wsww8blxqzam60j2.png)

Usage: git stash pop

This command restores the most recently stashed files.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lflcy00sytjft6pw772f.png)

Usage: git stash list

This command lists all stashed changesets.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rmb7p27tq50vc0rng6wh.png)

Usage: git stash drop

This command discards the most recently stashed changeset.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/90yjefvij5v3dvvjnbcv.png)

git branch
Usage: git branch

This command create new branch and other commend for check history

# Creating a new branch
git branch <new-branch-name>
>> 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 <old-branch-name> <new-branch-name>
>> 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 <branch-name>
>> 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 <branch-name>
>> git branch -D fix property-call-on-undefined-object
# Checkout from main branch to a feature-branch
git checkout <branch-name>
>> 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 <new-branch>
>> 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 <filename>[, <filename2>, <filename3>, ...]# Move only staged files in a given folder
git reset <folder-name>
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z3m6wtqggpbj50yf57d8.png)

Usage (ii): git log –graph


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a7pfq4oqclcf6zxvaxqv.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/myew028trigldhq13ipg.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/onfwo7f61ggqrncw2x9y.png)

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

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8av20i89y9ncd8232e4b.png)