Essential Git Commands
Published: 2022-02-16 08:04:37

**Git Configuration:**

```
git config --global user.name=“Your Name”
```
Set the name that will be attached to your commits and tags.

```
git config --global user.email “you@example.com”
```
Set the e-mail address that will be attached to your commits and tags.


**To view the configuration:**
 

```
git config --global user.email
git config --global user.name
```

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


**Create a project on your computer:**

```
mkdir [projectname]
```

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

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


```
cd [projectname]
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/24yv2i9uun3mxqec9sua.png)

**to initialize empty Git repository inside your project folder:**

```
git init
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ftncwe8uqzgp750klxgr.png)
**to view files inside your project folder:**

```
ls
```
 It will show list of files.

```
ls -a
```
 To view hidden files(.extension-file) inside project folder.

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

```
touch (filename)
```
 To create new/empty file.

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


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

It allows us to see the tracked, untracked files and changes.
```
git status
```

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

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

Add a particular file to the staging area.
```
git add [filename]
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ir4hi4xhdj9m08zoyarf.png)
```
git add .

```

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

Create a new commit from changes added to the staging area.
The commit must have a message!

```
git commit -m [message]
```

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

It shows the list of commits of current branch.
```
git log

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

To unstage your file/Removing changes from staging area

```
git restore --staged [filename]

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

To remove a commit from the history of a project.
```
git reset [commit hascode]
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hjtrudgav83hr691kecq.png)

Git has an area called the stash where you can temporarily store a snapshot of your changes without committing to the repository. 

```
git stash
```

Apply stored stash content into working directory, and clear stash.

```
git stash pop
```

Delete a specific stash from all your previous stashes.

```
git stash drop
```

Connect remote repository to local repository by using this command

```
git remote add origin [url]
```

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

pushing local changes to remote repository:

```
git push origin master/ git push -u origin master 

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

A fork is a rough copy of a repository. Generally forking a repository allows us to experiment on the project without affecting the original project
Reason for forking the project
1.propose changes to someone else's project
2.Use an existing project  as a starting point.

Cloning the forked project to local
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/otbvzh21tr5cv3ezzf7d.png)

```
git clone [url]
```

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

After cloning you will be able to see clone project inside your project folder.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nm8oauwitgqnxz1qe08q.png)

upstream : upstream generally refers to the original repository that you have forked from other git repositories.

```
git remote add upstream [url]
```

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

For viewing both url (origin and upstream):

```
git remote -v
```

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

Create new branch: 

```
git branch [branchname]
```

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

The git checkout command switched branches or restores working tree files. It allows switching between multiple branches in a single repository.

```
git checkout [branchname]
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y3tujaleyft6o70wyugo.png)

It shows the list of branches.

```
git branch

```

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

**Why we create new branch?**
=>Because we want to open a new pull request and we can only open a new pull request with a new branch. In simple language one pull request means one branch.
Never commit on main branch and create our pull request first.

## Conclusion 

That's a wrap! You've made it to the end of the blog! 
link mentioned below for other blog about the introduction of the git.
[Introduction to Git](https://ojhanidhi036.hashnode.dev/git-is-lit-which-makes-you-hitboom-in-the-project-environment)

[Follow me on Twitter for such contents](https://twitter.com/89NidhiOjha1)