Getting started with Git
Published: 2022-04-28 10:00:45

**Pre-requisites**
- Basic Linux commands
- Able to navigate directories using terminal 

## So what is Git?

- Git is a DVCS that stores the data in a filesystem made up of snapshots. These snapshots are created when the commit command is used.

### What is a DVCS?
   
- Version Control snapshots the present code along with the changes when committed. These snapshots can later be retrieved in case any errors creep in.

- In Distributed Version Control System, the same snapshots created by the Version control, are distributed among the authenticated users. 

- When the need arises to merge everything to a single codebase, these snapshots can be merged fast and effortlessly.

![Happy minions](https://i.ytimg.com/vi/BdoaiCdqE64/maxresdefault.jpg)
<br/>
## How to Git??

- Git, natively is a set of command line utility programs that are designed to execute on Unix based Operating Systems.

- There are various other implementations of GIT as GUI, but the the full functionality of GIT is only through the CLI.

### Installing Git

#### Linux/Unix users

- Open your terminal, and install [Homebrew](https://brew.sh) with the following command


![Homebrew installation Command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ic2ry493600y53dnzbu.png)

- Now install Git

![Install Git - Homebrew](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g4guhciacy2s9tfv9hba.png)

#### Windows users

- Go to [Git Downloads](https://git-scm.com/download/win), download the portable or the executable.

- After the installation is complete, open Git Bash.
<br/>
- From now on, it's the same for both platforms.

<br/>
<br/>

### Working with Git on a CLI
<br/>
- Executing `git` will list out all the available git commands which can be permutated accordingly.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w6n2ctm4h0go6ugjn7on.png)

#### git init
- **Initialising a Git repository**
- Now, let us tell Git who we are
<br/>
![Git Config details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tbij4wd8gv3j223du7t0.png)
- Here,`Avinash` and `avinasharutla@gmail.com` you can place your name and email address. These parameters are used in commit history, and we will understand about commits further.
<br/>

![Git Config list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ikt29ysihgghetz8k5oj.png) 
- The above command will list all the globally defined parameters in Git.

<br/>
- Now lets initialise a git repository
- I chose a folder named `git_chicha` for creating a git repository.
![Git init](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ksey0a8qefz7eo969tz0.png)

<br/>

- Ohh, was that a mistake, now we'll remove git from the folder

![Git remove](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y07r3jzrfz3ods35kz5m.png)

<br/>

- Now, we'll will create a file inside the git repository

![First file creation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1m5gmmu97611daq4f8i.png)

- We can use a text editor or vim for the editing of the file.

![first file in vim](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jcvs58mnz6t2j3vwatfc.png)
<br/>
- Now, after we enter the information in the file, lets stage it.
- When a particular file is staged, Git makes the file ready for committing into the Git timeline.

#### git add
![Git add](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4yum8eryxiu7qjq28z9o.png)

- Now, lets check the status of the git repository
![Git status](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvix9pb4sno8ggkhyeak.png)

<br/>

#### git commit
- After the file is staged, we need to commit the file into the Git timeline
- Committing is like taking a snapshot of the staged files with all the changes made.
- The command is as follows

![git commit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4y6wwa3jkshbjeorxdn9.png)

- When the above command is used, vim editor is opened![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/381di3o2xkoykrw9m8j3.png)
 .
&nbsp; &nbsp; **I** -> insert mode
&nbsp; &nbsp; **:wq** -> exit vim

![Commit in vim](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ts7nsqy8r6pw6pcp91fh.png)

- We can also use the ```git commit -m "commit message"``` to skip using vim.

<br/>
<br/>

- So let's consider a case where you pushed a commit which is bad for the pipeline. In this case, we need to revert the commit.

<br/>

#### git log
- Let us use the `git log` command to list all the previous commits inside the repo.

![git log](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o67nm703acjkdo2nb8dd.png)
<br/>
![git log realtime](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/14y963drtjy9jrrw1vg3.png)
<br/>

- In the above screenshot, one can see all the metadata about the commit, like the name and username. This data was entered through the `git config --global` command.

![git revert screen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zygxkk3ebh0zf0ula4pf.png)

- Following is the screen when we reverted to the original commit.

![git revert vim](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bz60lfpclnre699b5q7k.png)

- Its amazing how version control works right?

- Now, lets connect this offline repository to a online one. <br/>
- For demonstration, here I'll be using GitHub.

####Configuring Remote Repository

1. Let's first Visit [GitHub](https://wwws.github.com).

2. Now, let's Sign in. I have an account, Also take a look at [GitHub Student Developer Pack](https://education.github.com/pack).

3. After signing in, on the leftmost part of the screen, let's click on the **New** button.
![GitHub first image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/65xkm088m84wn9ezuhjj.png)

4. Now, let's choose a repo name which is catchy and relevant
![New Repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19kzrybpfmlpn26emm7s.png).

5. Now, head over to the end of the page, and click on "**Create Repository**".

6. After the creation of the repository, we can add files through browser upload or we can use Git for pushing the files.
![Git repo created](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kh6r61ixucx9fdz9gnll.png)

7. Lets use git here.

####Using Git to upload files to origin

- Let's head over to the terminal, and type the following command
`git remote add origin https://github.com/burpcat/git_chicha.git`

![git remote add origin](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5mrbrlxqh3jn8nke99qu.png)

- Now to see the linked remote repo's to your local repo, we can use the command

![git remote list carbon](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/179e5py91s7woayn3rgs.png)

![git remote list terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nm5w76nbi62du150puqs.png)

- Now since everything like commits are wrapped up at our local repository, we hence put forth the legendary command!

![GPOM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vmn06ithqti07vbxhuof.png)

- Note : Here if you can use your own [Personal Token](https://github.com/settings/tokens) to access Git, or sign in if that option exists

- Note : The default branch name vary from user to user, some use master, some may use main, some may use default. You could refer this [article](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-branches-in-your-repository/changing-the-default-branch).
 
- After the command is used, this is how our GitHub Repo looks like.

![GitHub repo page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/or8sxfxgfg2vfb2m2uax.png)

- There are Git Branches again, which I will discuss in an another article.