DEV Community

Cover image for From Local Folder to Github: Setting Up my First Project With Git and Github: A Guide
Njoroge Peter
Njoroge Peter

Posted on

From Local Folder to Github: Setting Up my First Project With Git and Github: A Guide

Introduction

Managing files locally or manually in your own pc gets out of hand very fast. You might find that you have a file named project_final_1 and project_final_2 which are about the same project but named differently depending on the day the projects were updated or modified.
Git Bash is a command line tool that records detailed history of your code as it changes over time
GitHub is an online tool where the changes in your code are stored remotely. GitHub does not use folders instead it uses repositories. The repositories make it easier for you to store your work, have a back-up of your work and also showcase your portfolio.

Step 1:Installing Git

First you have to install git depending on your operating system.
Once installed open git bash and you have to tell Git who you are. The code below will help you set up git for the very first time

git config --global user.name "put your name here"
git config --global user.email "put your email here"
Enter fullscreen mode Exit fullscreen mode

After configuration you have to verify your installation with the code below

git --version
Enter fullscreen mode Exit fullscreen mode

Adding SSH Key

For your git to be connected to git hub you have to genarate an ssh key. The key allows your git to access your github without much stress. First you open your terminal or git bash terminal and run the code below to generate the SSH key.

ssh-keygen -t ed25519 -C "enter your email here"
Enter fullscreen mode Exit fullscreen mode

This code will prompt you on your terminal to save the key on its default location, press enter.
Next you will be prompted to enter a passphrase, for easy access and to elimate the chances of forgetting the passphrase you entered just press enter twice.

Now you have a public key saved in your pc, use the command below to copy the key.

cat ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

Adding the Key to GitHub

  1. Login into github, click on your profile picture in the top left corner and select settings.
  2. in the left navigation bar click SSH and GPG keys.
  3. Click the New SSH key button.
  4. Give a description of your key (eg .my work laptop), in the drop down menu make sure your key type is aunthentication key and paste your public key into the key field.
  5. Click Add SSH key.

Test the SSH Connection

If you have completed all the above steps above the next step should be to verify your coonection has been successful. To test the connection run the code below;

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

After running the command above you should a get a message in your terminal
Hi you have successfully auntheticated, but Github does not provide shell access
If you have gotten this message you have successfully connected your git and github.

Step 2:Making the first commit
Create the project folder and navigate to it using the code below

mkdir Kenya-health-records-analysis
cd Kenya-health-records-analysis
Enter fullscreen mode Exit fullscreen mode

now that you are in the working folder now add a README.md file and a data file

mkdir data
touch README.md 
Enter fullscreen mode Exit fullscreen mode

from here i proceed to copy the Kenya_Health_Records.csv dataset and pasted it into data.To confirm that i have correctly copied i run the command below in gitbash

ls data
Enter fullscreen mode Exit fullscreen mode

I the described the project in the README.md file. For this i used the echo command

echo "# An analysis of Kenya health records" > README.md
Enter fullscreen mode Exit fullscreen mode

Then i initialized git so that it can track the changes i do in the file.

git init
Enter fullscreen mode Exit fullscreen mode

now git will track all the changes made to each file.
From here we check how git is tracking my project

git status
Enter fullscreen mode Exit fullscreen mode

Running the command above shows the README.md and datafolder in red. This means git notices these files exist in my working directory but they haven't been staged yet. So that stage the files so we can commit them we need to run the command below

git add .
Enter fullscreen mode Exit fullscreen mode

the dot after add means that git is staging all the files.If now you type git status you will see the files turn green under changes to be committed.

At this stage i was ready to commit the changes and i run the command below

git commit -m "initial commit"
Enter fullscreen mode Exit fullscreen mode

the -m shows i want to put a message describing what changes were made.

Step 3: Connecting to Github
This step ensures that the changes commited can be saved on a repository on Github
the process is quite simple;

  1. log into github click the + icon on the top right corner and select New Repository here i created a repository called Kenya-health-records-analysis.
  2. I made my repository public. It is important not to check any other boxes.
  3. Click Create Repository
  4. On the repository setup page select the ssh tab and copy the url.

Back now in git bash I used the url i copied suing the command below

git remote add origin git@github.com:PNjoroge-DataEng/Kenya-health-records-analysis.git
Enter fullscreen mode Exit fullscreen mode

Step 4: Pushing code to GitHub
Once i established connection to Github all i needed was to upload to github.To do i used the command below

git push -u origin main
Enter fullscreen mode Exit fullscreen mode

The -u flag links the local main branch to the main branch on github.

Summary **
Setiing up version control was hectict at first but i managed to set it up perfectly.After setting it up i have used these commands more frequently;
**git init
-to initialize git in my working folder
git status -to check which files are modified
git add -to stage changes to be committed
git commit -m "message" -to save the changes made
git push -to upload the commit to Github

NOTE git init should be run once you are in the working directory.

Top comments (0)