Git: A beginner's guide
Tags: git, github, beginners, codenewbie
Published: 2022-04-15 17:28:24

Welcome to this week's article peeps 👋, where I would be talking about git, why it's important to you as a developer/engineer and how to get started with it.

## 🔰 <u>Introduction:</u>

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. *In short, git is a version control system*. A Version Control System (VCS) is a tool that helps software developers keep track of how their software development projects change over time 🔃.

Each snapshot or state of the files and folders in a codebase at a given time can be called a "version". Version control systems were created to allow developers a convenient way to create, manage, and share those versions. It allows them to have control over managing the versions of their code as it evolves over time. It also enable collaboration within a team of software developers, without losing or overwriting anyone's work (*it prevents intra-company fights and brawls 🔥*) . After a developer makes a set of code changes to one or more files, they tell the version control system to save a representation of those changes.

In this way, a history of code changes - or versions - is built up as the code evolves. This history is preserved so that all developers, even if they are working in totally different locations, have access to a consistent and up-to-date history of the project. A version control system can also be referred to as a source control system, source code management, version control software, version control tools, or other combinations of these terms.

> *Version control discussion sourced from [IC](https://initialcommit.com).*

## 🐱 <u>Github:</u>

First thing to note: **Github is not Git**! Github is a platform (*a service*) used by developers (*who use git*) to upload and download resources and source codes. Git is used to connect and interact with Github to download and upload resources. There are other platform aside from Github used for versioning, some of which are GitLab, GitBucket, etc. With over 73 million user base, Github is a very popular and a favorite of many. If you don't have an account yet, head over there and set one up now!

![Github Profile Page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zuyvbe66m89eogs9hgpd.png)
 > *A typical Github user's profile page.*

## 📩 <u>Installation:</u>

Head over to [Git.com](https://git-scm.com/downloads) to download git installer, it can also be installed via the command line but let's do it the easy way 😉.
![Git Download Page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3m8hb896r698l5wlc81.png)

After downloading and running the installer, the next thing to do is test that the git installation was successful. Navigate into any folder using the `cd` command. Run the command `git init`. If you get the message "Initialized empty gi...", voila! you've had a successful installation!

![git init](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tmqrn3en8wj613eqvmw8.png)

## 🔧 <u>Connecting Git to Github:</u>

Let's connect our git to github, first, let's configure our name (Github username) and email. Run these two commands:
```sh
git config --global user.name "Your name here"
git config --global user.email "your_email@example.com"
```
After that, run the command:
```sh
ssh-keygen -t rsa -C "your_email@example.com"
```
This creates two files, `~/.ssh/id_rsa` and `~/.ssh/id_rsa.pub`. On windows, head into your `C:/username` folder and look for a folder called `.ssh` (*You might need to enable "Show hidden files" to see it). On Linux, it would be in your "Home" folder (*also a hidden folder*). Navigate into that folder and open the one called `id_rsa.pub`.
![ssh file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dl03n5vxlyrl9inn4vhu.png)

You would see a long string of gibberish in there ending with your email, that's good. Head over to Github and go to your settings
![Settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/shdu5pwscd7zswxy2z20.png)
Head to the "SSH and GPG" keys and click "New SSH Key" in the top right corner. Give your ssh key a name and paste that gibberish in the `id_rsa.pub` file. Click "Create key" and head back to your terminal. Run the command:
```sh
ssh -T git@github.com
```
If you get a message like: "Hi username, ...". Then, mission successful! 

## 🔖 <u>Git Keywords:</u>

Let's discuss some of the terminologies you would find in the git ecosystem (*including GitHub*), after all, you need to be able to speak like a version control master 😄. *I would be using* `remix-pwa` *as an example here*.

- **Repository:** A Git repository (also called "repo") tracks and saves the history of all changes made to the files in a Git project. This information is used by online platforms like Github to store, track and display resources (*mostly source codes*) in a graphical format.

![Repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mwsv5jv7di8myj7wtzq2.png)
> *A repo example on Github. This can be used to view changes and if needed, reverse those changes. Plus, Github gives a lot more features.*

- **Branch:** A branch is a version of the repository that diverges from the main working project. Imagine a project, that has multiple versions that don't interfere with each other and these versions can change independent of the others. A git repo can have several branches.

![Branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fto5lk18syny0f858ueh.png)
> *A branch system in a repo, each of these branches contain different versions of code of a particular project. One benefit here is that I can compare across branches and edit a project's source code without "destroying" the project itself. Think of a project as a tree, and different versions as different branches.*
  
- **Fork:** Fork or forking is a process of duplicating someone else's project (or code) in order to make edits or changes. One of the ethics of being a developer or engineer is to **never** edit someone else's source code directly, if you wish to make a change, you duplicate that person's code as your own (*you fork that person's project*)

![Fork](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nch8ldizr3873wluwp5d.png)
> *If I wanted to make a change to this person's work, I would fork it then edit, and not edit it directly.*

- **Pull Request:** Pull requests (*also known as PR*) is the process of trying to <u>merge</u> to seperate code together. Imagine this scenario, we realized a mistake in someone's code (*could be a team member or just an [Open Source](https://dev.to/shafspecs/how-to-get-started-with-open-source-2ldj) work*) and we want to fix that bug 🐛. The steps we would take would be:
- Fork the project
- Make the changes and fix what we intended
- Open a PR (*request the owner of the original project to merge the two codes together*)

![Pull Request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jvo0fk5wkgavdijzop6m.png)
> *This is an example of a merged PR. We have two different versions (***two different branches in this case***) and we want to combine their content. We open a PR, and then if the change we want to merge is valid (has no bugs or issues), we merge.*

- **Staging:** Staging is a used to add files of a working tree (*directory*) to a snapshot. This is used to prepare the files for a <u>*commit*</u>.

- **Commit:** A commit is used to record the changes in the repository. The staging and committing are co-related to each other. Staging allows us to continue in making changes to the repository, and when we want to share these changes to the version control system, committing allows us to record these changes.
Commits are the snapshots of the project. Every commit is recorded in the master branch of the repository. We can recall the commits or revert it to the older version.

- **Clone:** Cloning is used to make a copy of the target repository locally or "clone it". If for example, we want a local copy of a repository from GitHub, this tool allows creating a local copy of that repository on your local computer.

- **Merge:** Merging is a process to put a forked history back together. It is simply just the merging of two seperate code history into one branch.

- **Issue:** An issue is a Github thing (*I don't know any other providers that offer it too 😅*) that is used to keep track of bugs, issues, requests and features in a repository. 

![Issues](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ied4ygkvf9avd5uew055.png)
> _An issue example, in this case, there is an issue with using this project that the developer hasn't fixed yet. A user can easily come here and contact the developers directly who can offer solutions, fixes or alternatives._

- **Push:** Pushing is the process of moving (*more like copying*) a local repo to a remote repo. A remote repo means a repo stored on the cloud, in a database somewhere. It is used for example, to move code from your computer to github.

That's ten popular keywords used in git and github for you guys 😄, there are a lot more you can read up on by yourself. If you know of anymore and want to share, let's hear it in the comments!

## 📝 <u>Example Time:</u>

That's enough talk! Let's do a miniature project and implement git in it. Open up your terminal and navigate to a directory (*as always, I would be using "Documents"*). Run the command:
```sh
mkdir git-example
```
The command `mkdir` means "*make directory*" and is used to create a directory in the folder we're in (in this case, *git-example*). Afterwards, use the `cd` command to navigate to the newly created folder. Let's now create the files we would upload to github, as a person with Javascript running in their veins, we would create a very simple js example (*don't worry if you don't know js*). Run the command:
```sh
touch example.js
```
The command `touch` creates that particular file in our current directory, in this case, `example.js`. Open up an editor and let's write a very simple conditional statement in JavaScript.
```js
const a = 12
let b = 13

b = 12

if (a == b) {
  console.log('a and b are equal')
} else {
  console.log('a and b are not equal')
}
```
this code initializes two variables, a and b with two different numbers (12 and 13). We later assigned 12 to the variable `b`. We then had an `if` statement, if a is equal to b, then log a message in the console, if not, log a different message. Okay! Let's push to Github!

Open up your github's repo page and click "New Repository". I would be calling mine "DEV-example", make sure the visibility is set to public (*other users would be able to view your source code*). Add a description for your repo and skip the remaining questions and boxes below, click "Create repository".

Head back to the terminal of your project and run the commands:
```sh
git init
git remote add origin <URL-of-the-repo>.git
git add .
git commit -m "Initial commit"
git branch -M main
git push -u origin main
```

Let's run through each command, `git init` initializes an empty, local repo in your project (*a `.git` folder*).
 
`git remote add origin <URL-of-the-repo>.git` is used to tell your local repo the location of the remote repo, where on the cloud are we connecting to, to store our repo.

`git add .` is used for staging, normally, we use `git add <name-of-file>` but as a shortcut, you can use `.` to stage every file in the directory (*except the `.git` local folder*). 

`git commit -m "Initial commit"` is used to commit our project. The `-m` part is called a "flag". Flags are used to pass arguments to a command, in this case, the flag `-m` stands for "message" (**Every commit must have a message**, *usually, this should contain a brief description of what your changes consists of or what they do*). The text between the double quotes is the message argument we want to pass to the commit command.

`git branch -M main` is used to change our branch (*also known as rebasing*). The `-M` flag here means "move" and what it does is "move our current folder content to the branch called 'main'".

`git push -u origin main` is used to push our local changes to our remote repo. The `-u` flag is used to track that repo changes (*useful when pushing to a remote repo for the first time*). It means "set up an upstream tracker to track changes". `origin main` is telling git where to push to, which branch should it push to, in this case, our branch "main".
![git](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xx4n0hdr3idw49osjzf1.png)
> _I forgot to authenticate my github and forgot to rebase my repo to main 😅😅_

Head back to your github repo and refresh the page, voila!
![done](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6kas0mhmd8qj7thpeylm.png)

--- 

That's it for this week's peeps, I would still try and write as often as I can but it would be an irregular writing schedule 😅. Keep the comments flowing with questions or observations, we would love to hear them. Remember to keep reading, learning, and coding. And don't forget to play now and then! Love you guys, cya in the next article 👋.