Getting Started with Git and GitHub

davidadediji

David Adediji

Posted on February 12, 2022

Getting Started with Git and GitHub

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
Enter fullscreen mode Exit fullscreen mode

When you see an output like this

Git version

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
Enter fullscreen mode Exit fullscreen mode

Then use the command

$ sudo apt install git
Enter fullscreen mode Exit fullscreen mode

to install git

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

$ git --version
Enter fullscreen mode Exit fullscreen mode

Now, you should have an output.

Run the commands

$ git config --global user.name "your choice username with no spaces"
Enter fullscreen mode Exit fullscreen mode
$ git config --global user.email "your email address"
Enter fullscreen mode Exit fullscreen mode

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 official website.
    Git official website

  2. Look for the Download button to download a .exeextension.

  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

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

Github account

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"
Enter fullscreen mode Exit fullscreen mode

and tap enter to get this prompt

Enter file in which to save the key (/home/david/.ssh/id_ed25519): 
Enter fullscreen mode Exit fullscreen mode

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

  • Next up, start the ssh-agent on the background with the command:
eval "$(ssh-agent -s)"
Enter fullscreen mode Exit fullscreen mode

You get an output like this:

Agent pid 32602
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • Then it time to add your public key to your GitHub account. Use the command
cat ~/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

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

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

GitHub settings

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

SSH-Key

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

Add Key
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
Enter fullscreen mode Exit fullscreen mode

create a directory

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
Enter fullscreen mode Exit fullscreen mode

Initialise empty git repository

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
Enter fullscreen mode Exit fullscreen mode

create file

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
Enter fullscreen mode Exit fullscreen mode

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

$ git add file1 file2 file3 ...
Enter fullscreen mode Exit fullscreen mode
$ git add .
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

git commit

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
Enter fullscreen mode Exit fullscreen mode

git log

This command is quite different from

$ git status
Enter fullscreen mode Exit fullscreen mode

git status

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>
Enter fullscreen mode Exit fullscreen mode

The command

$ git branch
Enter fullscreen mode Exit fullscreen mode

git branch

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>
Enter fullscreen mode Exit fullscreen mode

switch to new branch

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
Enter fullscreen mode Exit fullscreen mode

git checkout master

Then use the command:

$ git merge <new_branch_name>
Enter fullscreen mode Exit fullscreen mode

git merge

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
  2. In order to access your account, do click on sign in button
    Sign in to Github

  3. Type your email and your password
    Sign in to GitHub

  4. Click on the sign in button
    Sign in

  5. Once you are on the page, click on the 'Create New' + icon
    GitHub homepage

  6. Click on New repository
    New Repository

  7. Type the name of Repository
    Repository Name

  8. As an option, you can give your repository a new description
    Repository Description

  9. Then click on 'Create repository' button
    Create Repository

  10. Click on Copy icon to copy the respository link to your clipboard
    Copy Repository Link

  11. Go back to your terminal and type the command

$ git remote add origin [repository url you copied]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

That's because GitHub doesn't recognise master as a default branch.

  1. Once that's set use the command:
$ git push -u origin main
Enter fullscreen mode Exit fullscreen mode

push to remote main branch

To push your code to the remote main branch

Other commands

$ git pull origin main
Enter fullscreen mode Exit fullscreen mode

git pull is used to pull the latest changes from the remote repository into the local repository

$ git clone [Link to repository]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

from which I cloned to my remote repository using command.

$ git clone https://github.com/davidadediji/Password-generators.git
Enter fullscreen mode Exit fullscreen mode

Git clone

  • With the repo cloned, next turn is to create a new branch
$ git checkout -b new_branch
Enter fullscreen mode Exit fullscreen mode

Image description

  • 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
Enter fullscreen mode Exit fullscreen mode

with the command

$ git remote add upstream https://github.com/chryz-code/Password-generators.git
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • With the command
$ git status
Enter fullscreen mode Exit fullscreen mode

I was able to keep track of what has changed in my working directory.

Git status

  • The file modified "password-generator.py" is added to the staging area with command:
$ git add password-generator.py
Enter fullscreen mode Exit fullscreen mode
  • 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"
Enter fullscreen mode Exit fullscreen mode

git commit

  • Then push all changes to the remote new branch with command
$ git push -u origin new_branch
Enter fullscreen mode Exit fullscreen mode

Push to new branch

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

compare and make pull request

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

comment on pull request

  • And Voila! made a successfull pull request.

Successful pull request

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.

💖 💪 🙅 🚩
davidadediji
David Adediji

Posted on February 12, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Getting Started with Git and GitHub
devops Getting Started with Git and GitHub

February 12, 2022