# What is Git and How Does It Work?

# what is git

> Git is the version control system which keeps the track of the changes which you have made in the present file from previous file (but when you initiated it )
> 
> ---

# Why Git is used

> Git is used for the convenience Purpose to track - the change which has been made by different person working on the same project (or in simple word changes made by whom ----&gt;(Fancy word for whom ---collaborator) and when including what the change ).
> 
> ---

# Learn Git Basic

### How to use git and what are the basic command in git

* **Step 1** ----&gt; Please install the git and make a account on it
    
* **Step 2** ----&gt; make a file with any Extension (recommended .txt)
    

### git init

> It initialize the git for our file so that it can make a file to keep the change (.git file)

```plaintext
C:\Users\ranja\Desktop\git demo> git init
```

\*\*Output :——\*\*Initialized empty Git repository in C:/Users/ranja/Desktop/git demo/.git

### git status

> *This command of git show is there any changes from the previous commit which has made by you or collaborator which is added for commit.*

```plaintext
C:\Users\ranja\Desktop\git demo> git status
```

\*\*Output :——\*\*No commits yet Changes to be committed: (use "git rm --cached ..." to unstage) new file: page1.txt new file: page2.txt

### git add

* **git add &lt;file name&gt;**
    

> Add the file for commit on which you want to save the change in <mark>(term Staging area)</mark>
> 
> ```plaintext
> C:\Users\ranja\Desktop\git demo> git add page2.txt
> ```

* **git add .**
    

> Add all the changes which you have made in the folder In different files of same folder 📁 for commit in <mark>(term Staging area)</mark>
> 
> ```plaintext
> C:\Users\ranja\Desktop\git demo> git add .
> ```

**Output :——** \[master 753ba4b\] commit for page2 1 file changed, 1 insertion(+)

### git commit

* **git commit &lt;file name&gt; - m “message”**
    

> it save the change along with message for the particular file which name is taken as (file name).
> 
> ```plaintext
> C:\Users\ranja\Desktop\git demo> git commit page2.txt -m "commit for page2"
> ```

**git commit . - m “message”**

> It save all the files with single message that you have added for commit.
> 
> ```plaintext
> C:\Users\ranja\Desktop\git demo> git commit . -m "commit all at once"
> ```

**Output :——**\[master 4ed24b1\] ***commit all at once*** 1 file changed, 2 insertions(+), 2 deletions(-)

### git log

* **git log**
    

> this log all the changes make till the last commit in the repo
> 
> ```plaintext
> C:\Users\ranja\Desktop\git demo> git log
> ```

> Remember your terminal may get stuck to exist from this situation <mark>just click (q)</mark>
> 
> it means it opens the output in pager ( **fancy name** terminal is inside **less)**

* **git --no-pager log**
    

> ```plaintext
>  C:\Users\ranja\Desktop\git demo> git --no-pager log
> ```

### git diff

* **git diff**
    

> Where (in which file) and what changes has made but not commited
> 
> ```plaintext
> C:\Users\ranja\Desktop\git demo>  git diff
> ```

* git diff
    

> it also give the different between &lt;previous node hexacode&gt; &lt;next node hexacode&gt;
> 
> ```plaintext
> C:\Users\ranja\Desktop\git demo>  git diff  0946f9291  753ba4b
> ```

### git revert

* git revert &lt;hexacode of node where you want to go back&gt;
    

> It will remove the last change but you need to commit your changes( means you head node do not change yet)
> 
> ```plaintext
> C:\Users\ranja\Desktop\git demo> git revert 4dc2b6e2f6
> ```

**Output :——** Please commit your changes or stash them before you merge.

### git reset

* git reset -- hard &lt;hexacode till where you want to remove &gt;
    

> It will remove the all change till that node make the head pointing to that node **<mark>so be careful as it remove every thing till there</mark>**
> 
> ```plaintext
> C:\Users\ranja\Desktop\git demo> git reset --hard  753ba4ba3d
> ```

**Output :——** HEAD is now at 753ba4b commit for page2
