Introduction to Git [Part 1]
Tags: git, opensource, tutorial
Published: 2021-10-28 03:48:53

### What is Git?
Git is a free and Open Sourced [Version Control System](https://www.geeksforgeeks.org/version-control-systems/), ie, allows programmers to keep track of any changes with files involving collaborative software development. 

### Some Basic Git Commands
1) `git init`
2) `git clone`
3) `git pull`
4) `git add`

// I will be using VS Code for these examples.//

Before you can use Git in VS Code, you first need to clone a repository. Open Github and the repository that you want to clone, fork the repository. Once the forked Repository opens, copy the URL. Open VS Code and in the "Get Started" tab, under "Start" Click on "Clone Git Repository" and paste the URL of your forked Repository. After creating files for the repository click on View -> Terminal or shortcut: ^` for Mac.

1) 'git init' : This command is used to initialize a new local repository. To create one: 
- First, open your command line and navigate to the folder where you want to initialize it. If one does not exist you can create one using the ```mkdir folder_name``` command.
- Now, initialize the folder using the command ```git init``` and the following should appear.

```
Initialized empty Git repository in /Users/user_name/Documents/test/.git/
```

2) `git clone`: This command is mostly used by users who want to clone the remote repository to their local system through the command line tool. 

`git clone` as the name suggests clones a Github repository to a new location (on your local system in this example). The syntax for the same would be 
```
git clone <url>
```
To clone a Github Repository:
- First, go to the Repository's Github Site, then on the main page, Click on the Green `Code` button -> copy the HTTPS URL.  
- Now, on your command line navigate to the folder where you want to clone the repository, use the git clone command and the following should appear.

```
Cloning into 'test'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
```

Now you successfully cloned the repository. I've created a test.c file in my test repository, so after cloning, if I use the `ls` command (MacOs), the name of the file shows, indicating I've cloned the repository and all its data.

3) `git pull`: The command fetches the data from the remote repository and updates your local repository. 
After cloning, many files would've been added/removed/modified by other contributors. 

If you want to contribute another file, it would be great if your cloned repository was updated to match with the remote repository, to avoid clashes and creating avoidable inconveniences. 

This is why the `git pull` command is used. To pull from the main branch of the remote repository use the following command 
```git pull origin main``` 

and this should be the output.

```
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 606 bytes | 151.00 KiB/s, done.
From https://github.com/AaronJohnson02/test
 * branch            main       -> FETCH_HEAD
   0beb25d..703275f  main       -> origin/main
Updating 0beb25d..703275f
Fast-forward
 test2.c | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 test2.c
```

Now you've updated the cloned repository to match the remote. To verify this, if you reuse the command, you should see:

```
 * branch            main       -> FETCH_HEAD
Already up to date.
```

4) "`git add`" - The command is used to add files to the [staging area](https://stackoverflow.com/questions/49228209/whats-the-use-of-the-staging-area-in-git) and it's syntax is "git add filename.extention". 

Let us take an example, where we have created a file called "text.txt". When you open the terminal in VS code type in "git add text.txt". To check if the file has been staged, use the "git status" command and the following should appear.

---

On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   text.txt

---

In the terminal untracked files will appear in red and tracked as green.



Here are a few sources for those who are interested in learning Git. 

1) [Atlassian - Version Control with Git (Paid)](https://www.coursera.org/learn/version-control-with-git)
2) [Google - Introduction to Git and GitHub (Paid)](https://www.coursera.org/learn/introduction-git-github)
3) [Atlassian (Website)](https://www.atlassian.com/git)
4) [freecodecamp (Website)](https://www.freecodecamp.org/news/learn-the-basics-of-git-in-under-10-minutes-da548267cc91/)
5) [geeksforgeeks (Website)](https://www.geeksforgeeks.org/ultimate-guide-git-github/)


I hope this helps, I am too learning git and if you have any feedback on how I can improve or any changes to make, please feel free to reach out!