Getting Started with Git and GitHub
Tags: devops, git, github, codenewbie
Published: 2022-02-12 22:49:38

For many developers, Git and GitHub are essential tools for pushing and documenting code.

They play an essential role in helping developers:
- contribute to open source projects 
- develop products for software companies in a collaborative manner.
 
In order to get started in a new software role or participate in open source, you should understand their basic use. 

This post explains what Git and GitHub are and how to use them. 
Do Read on..

## Git
Git is a version control system that enables people and teams to work collaboratively on projects. It is free, easy to use and openly distributed. Git allows developers working as a team to keep track of every update in their code base or content.

## GitHub?
What is GitHub if Git is a version control system? 
It is important to note that Github is a Remote server that stays on the cloud and all items executed by each member of a development team are stored together on that server. 
In addition to GitHub, other cloud repositories like BitBucket, GitLab and others keep content, code, and document repositories on the cloud.

## Benefits of using Git and GitHub
The use of Git serves many benefits, which includes but not limited to:

1. Branching capabilities: For every code or content change you want to make, Git allows you to create isolated environments in form of branches which you can merge later.

2. Fast-paced open source collaboration: Being a free and Decentralized distribution system, it is quite easy for anyone, anywhere in the world to make project contribution. All they need is fork a specific repository, make there changes and send a pull request.

3. Fast Release cycle and Deployment: Git allows software teams to adapt to agile processes. With a tool like git software developers can make changes to their code frequently and revert such changes when there are issues. This is quite vital for continous integration and continous development. 

4. Track content changes: With Git each member of a team can keep track of whoever makes changes to a content or code. The same goes for time when a change was made.

## Installing Git on your Local machine

Git is compatible with a variety of operating systems. These include Windows OS, Linux OS, and Mac OS. Each of these operating systems has a unique installation process.

### Linux(Ubuntu)
To install Git on Linux you need to use the terminal. Just a sequence of commands and you can start using Git on your Linux distro. 
Most Linux distros comes pre-installed with Git. 

To cross-check run 

```
$ git --version
```
When you see an output like this 

![Git version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2590gt6jdvssl7lxv1ff.png)

Then you have Git installed
If this is the case you can move on to configuration. 

However if you got no output, you have to run the following commands.

The first command involves you updating application packages. That is 

```
$ sudo apt update
```
Then use the command

```
$ sudo apt install git
```
to install git

Once you are done with the package installation, check the version once again

```
$ git --version
```
Now, you should have an output.

Run the commands

```
$ git config --global user.name "your choice username with no spaces"
```

```
$ git config --global user.email "your email address"
```
### Windows
You install Git on Windows as Git Bash. With Git Bash you can make use of commands similar to Linux terminal. To download and install:

1. Go to [Git's](https://git-scm.com/) official website.
![Git official website](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lugvdphpm55cinrdgmll.png)

2. Look for the Download button to download a `.exe`extension.

3. Once downloaded on your local machine, run the .exe file and follow each prompt for installation.

4. After installation, you can open Git Bash for configuration (which is exactly as described for linux).

## Setup GitHub account
You need to create an account on GitHub to access it's functionalities. To create an account:

- Visit GitHub official website and click on sign-up

![Github webpage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/09mwwg8gim3z2sr596ea.png)

- Add details such as your name, email address and password.

![Github account](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mm368c0kkjukttufm8xu.png)

Then you are good to go. 

However, you could get a fatal error when pushing your code from your local repo to remote GitHub repo. To avoid such error you need to add and configure an SSH key.

To generate SSH key:

- Open Terminal

- Then enter the command 

```
$ ssh-keygen -t ed25519 -C "davidadediji@gmail.com"
```
and tap enter to get this prompt

```
Enter file in which to save the key (/home/david/.ssh/id_ed25519): 
```
and tap enter key.

Note: you can leave it empty and use the default .ssh/id_ed25519 file

- On tapping enter, you did be prompted for a passphrase. You can either add one or leave it empty. On enter you get a response like this

![enter passphrase](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hdpgj1usjgazzzx72gdt.png)

- Next up, start the ssh-agent on the background with the command: 

```
eval "$(ssh-agent -s)"
```
You get an output like this:

```
Agent pid 32602
```

- Then Add your SSH private key to the ssh-agent. use .ssh/id_ed25519 if you left it as the default. However, if you created your key with a different name, or if you are adding an existing key that has a different name, replace id_ed25519 in the command with the name of that private key file.

```
$ ssh-add ~/.ssh/id_ed25519
```

- Then it time to add your public key to your GitHub account. Use the command

```
cat ~/.ssh/id_rsa.pub
```
to get your public key, once public key has been generated, copy it and Login to your GitHub account.

- On opening your GitHub account, click on your profile avatar and navigate to settings. 

![Github Profile Avatar](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxju1vwm2e80rfu2qfy0.png)

- On settings Page, click on SSH and GPG Keys at the left hand side.

![GitHub settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwn1146bfl7iogdgd60m.png)

- Click on New SSH Keys for the SSH key section. 

![SSH-Key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/np5r9xfo8xeuxwmkx2s6.png)

- On the New page, Add a title in the title field and add the public key you copied into the Key field.

![Add Key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s3toyyes8qa3k696459x.png)
then click on Add New Key button

- You get redirected to a password confirmation page. Do confirm your password and that's all.

## Interacting with Git and GitHub Repo

To get started pushing content or code to Github from Git, create a directory on your computer. 
Do Note: Local Repository refers to Git and Remote Repository (one on the cloud) refers to GitHub

#### Local Repository
On your computer's working directory, use the command:

```
$ mkdir git-github-demo && cd git-github-demo
```
![create a directory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jihwiqrmckxfrvi4vrqb.png)   
The above command allows you to create a diretory and navigate to it immediately.

The next step is to initialise a git repo in the working directory with command 

```
$ git init
```
![Initialise empty git repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j5mht7l1h92jn1c69i6q.png)
 
This command creates a .git file (hidden) within the repo. In turn, it serves as an identity and let's you track frequent changes within the folder. 

You can move on to create a new file (any extension like .txt, .py, .html). For example, you can create an index.html file with command

```
$ touch index.html
```
![create file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gdevpd9lbqfbunuwermn.png) 
 
you can add changes to the file then move on to add the file to a staging area with the command

```
$ git add index.html
```

In case you have multiple files to add to staging area, use command

```
$ git add file1 file2 file3 ...
```

```
$ git add .
```

to add all the files to a staging area from the root.

Once you are done with adding files to the staging area, the next step is to commit these files. Use the command

```
$ git commit -m "commit message here"
```
![git commit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ez1s9a9bsk04ex0z0srt.png)
 
all file in your staging area get added to the commit history with their commit messages. Do ensure you add a relevant commit message. 

To see all your commit history, use the command

```
$ git log
```
![git log](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p04hbvy2jrfilg9k1jts.png)
 
This command is quite different from 

```
$ git status
```
![git status](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8rygm85gt8rh065057m3.png)
 
while `git log` allows you to view your commit history, `git status` keeps track of which files is in your staging area and changes you make on each file. 

Most times you did find yourself, it did be helpful to create branches. Branches to do aid parallel development and creating a new one usually points to the lastest commit. By default Git comes with a master branch. 

To create a new branch use the command 

```
$ git branch <branch_name here>
```

The command

```
$ git branch
```
![git branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xqmq9xq1wxsj9jgzlkmf.png)
 
shows a list of all branches you create and in case you want to work on the new branch, you have to switch from master using the command 

```
$ git checkout <some branch name here>
```
![switch to new branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mnmrad3wmad8etva9350.png)
 
From here you can add new files and make some commits.
However, this new branch to which you have added new changes will be up ahead of the master branch. 

To move every change you made on the new_branch to the master branch you need to use the git merge command. 

But first you need to check out to master branch with command

```
$ git checkout master
```
![git checkout master](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9b1j9lc5xkobtsi2zptb.png)
 
Then use the command:

```
$ git merge <new_branch_name>
```
![git merge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tfrnzh4u1c738rah27r8.png)
 
In some cases, there may be conflicts when you make similar changes to different branches. Resolving merge conflicts requires some skill, experience and grit especially when working with a large code base.

_I do hope to cover this in a latter article._

#### Remote Repository
The Remote Repository here is known as GitHub (currently owned by Microsoft). To make use of the commands and instructions listed here, ensure you create your GitHub account. 
Do visit the section on Setting up your GitHub account. 

So to create your Remote respository, follow this process:

1. [Go to GitHub's official web page](https://github.com/)
2. In order to access your account, do click on sign in button
![Sign in to Github](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fkjm80p5ct925igfkdck.png)

3. Type your email  and your password
![Sign in to GitHub](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9h7gbd9h2me8t031a4q1.png)

4. Click on the sign in button
![Sign in](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7v6olfjrns7trg29p3ye.png)
5. Once you are on the page, click on the 'Create New' + icon
![GitHub homepage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uq0yhiwd3wfk5goo3blc.png)
6. Click on New repository
![New Repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ngoq9kvpinze57j7phpu.png)
7. Type the name of Repository
![Repository Name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/esgspszxus2j0nmjljxr.png)
8. As an option, you can give your repository a new description
![Repository Description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xb4vq9dbtsd9u6ig9zhf.png)
9. Then click on 'Create repository' button
![Create Repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o59yutd2qmg190nqal09.png)
10. Click on Copy icon to copy the respository link to your clipboard
![Copy Repository Link](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9w37di7dq2zkbzozb18l.png)
11. Go back to your terminal and type the command
```
$ git remote add origin [repository url you copied]
```

However before you move to the next step you need add the default remote branch main through your local repo with the command:

```
$ git branch -M main
```
That's because GitHub doesn't recognise master as a default branch.
12. Once that's set use the command:

```
$ git push -u origin main
```
![push to remote main branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yoehd5hhtjvm6cvj6wjb.png)
 
To push your code to the remote main branch

## Other commands 

```
$ git pull origin main
```
git pull is used to pull the latest changes from the remote repository into the local repository
```
$ git clone [Link to repository]
```
git clone is used to clone an existing remote repository into your computer
## Making a Pull Request
Making a Pull request allows to contribute to other people's project. All you have to do is 
copy to your repo, clone it, make changes and send it as a pull request. 
Here's how the process goes more explicitly:

- Find a project you want to contribute to. For me i got a password generator project repo
chryz-code/Password-generators,which I forked. The fork button exists on the top right corner.

- Forking this repo creates a copy into my remote repository. 
The Repository then looks like this

```
davidadediji/Password-generators
```
from which I cloned to my remote repository using command. 

```
$ git clone https://github.com/davidadediji/Password-generators.git
```

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

- With the repo cloned, next turn is to create a new branch 

```
$ git checkout -b new_branch
```

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

- Connect a new remote for the upstream repo. The upstream repo is the original repo
i forked from. That is 

```
https://github.com/chryz-code/Password-generators.git
```
with the command 

```
$ git remote add upstream https://github.com/chryz-code/Password-generators.git
```

- Then I open the repo's working directory to make changes to some of the file, then add to my staging area with command.

```
$ git add file
```

- With the command

```
$ git status
```
I was able to keep track of what has changed in my working directory.

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

- The file modified "password-generator.py" is added to the staging area with command:

```
$ git add password-generator.py
```

- Then it's time to commit the changes i made from staging area to the new branch with command:

```
$ git commit -m "modified a file"
```

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

- Then push all changes to the remote new branch with command

```
$ git push -u origin new_branch
```

![Push to new branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ltif7lz6ll7xz2kfyi5w.png)

- Go to the original remote repo you will see a new button " compare and pull request"

![compare and make pull request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gtj6tu1yuprztw9vkstu.png)

- Click on it and you be directed to a page where you write comment and finally Create Pull Request

![comment on pull request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/si15acw54kq5gyq8g44b.png)

- And Voila! made a successfull pull request.

![Successful pull request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v7a3rehd5ywuvgeq053p.png)

## Conclusion
Here I have explained the basics of Git and GitHub to make you get started in making contributions. 
I did be publishing some advance concept in Git quite soon. 
Do stay tuned.