Basic Commands of Git
Published: 2022-02-16 07:50:48


## Git Congiguration

```
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.
 
<u>**To view the configuration**</u>

```
git config --global user.name
git config --global user.email
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7erhe7nodqgjvisgnu7f.png)
## Starting a project
Create a project on your computer
```
 mkdr [project name]
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e7996zucqwczhfet5orv.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/msw3cne3s404v6n6rbs9.png)
```
 cd [project name]
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eh0z5p911w4yyqmoew9n.png)
To initialize empty git repository inside the project
```
 git init
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dvdvq5v5cp7oo2f00wwm.png)

To view hidden files(.extension) inside the project folder

```
ls -a
```
To view inside the hidden .git file

```
ls .git
```

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

To create new empty file

```
touch [filename]
```

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqvcdj2aya2h9wv4dn7t.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/67orw9vruxocwqiyq2cx.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cjnlwdnjhlnpabgrtp77.png)
Adding all the files to the staging area from current directory down into the directory tree.
```
git add .
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2zz9z75e5r99czbqsek1.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/x9qxxkn7tqtkam6odhdv.png)

It shows history of commit of the current branch

```
git log
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b8dysd6nqrlhwdnw8tzj.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/mnpjsq4g7dw7d8qglvuo.png)


To remove a commit from the history of a project

```
git reset commit_hashnode
```

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

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

Put current changes in your working directory into stash for later use.
```
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/75shr3o5ihl8tc9fslv6.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/o1s0ql15f2527fnsrbot.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/v9almb1eodndf5enkav3.png)
```
git clone url
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e8wmyfjmoeizamco4p49.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/y9tw4epve5znc972of3r.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/p1i07l1x0eq4hrvmhhvq.png)

for viewing both url(origin and upstream):

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

create new branch
```
 git branch branchname
```

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

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

```
git checkout [branchname]
```

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

It shows the list of the branches

```
git branch
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zbt74se2eae3kg5l36g8.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 this blog!
[Follow me on twitter for more such contents](https://twitter.com/home?lang=en)