Getting Started with Git and GitHub
David Adediji
Posted on February 12, 2022
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:
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.
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.
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.
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
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:
Go to Git's official website.
Look for the Download button to download a
.exe
extension.Once downloaded on your local machine, run the .exe file and follow each prompt for installation.
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
- Add details such as your name, email address and password.
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
- 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.
- On settings Page, click on SSH and GPG Keys at the left hand side.
- Click on New SSH Keys for the SSH key section.
- On the New page, Add a title in the title field and add the public key you copied into the Key field.
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
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
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
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"
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
This command is quite different from
$ 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>
The command
$ 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>
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
Then use the command:
$ git merge <new_branch_name>
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:
- Go to GitHub's official web page
As an option, you can give your repository a new description
Click on Copy icon to copy the respository link to your clipboard
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.
- Once that's set use the command:
$ git push -u origin main
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
- With the repo cloned, next turn is to create a new branch
$ git checkout -b new_branch
- 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.
- 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"
- Then push all changes to the remote new branch with command
$ git push -u origin new_branch
- Go to the original remote repo you will see a new button " compare and pull request"
- Click on it and you be directed to a page where you write comment and finally Create Pull Request
- And Voila! made a successfull 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.
Posted on February 12, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.